diff --git a/chrome/_locales/en_US/messages.json b/chrome/_locales/en_US/messages.json index 1270f90..6c7181c 100644 --- a/chrome/_locales/en_US/messages.json +++ b/chrome/_locales/en_US/messages.json @@ -12,16 +12,16 @@ "description": "Cast notification failure title" }, "message_cast_success": { - "message": "Casted the $1 stream to the Twitched app.", + "message": "Casted to the Twitched app.", "description": "Cast notification success message" }, "message_cast_fail": { - "message": "Failed to cast the $1 stream to the Twitched app.", + "message": "Failed to cast to the Twitched app.", "description": "Cast notification failure message" }, "message_find_stream_fail": { - "message": "Failed to find a live stream on the page.", - "description": "No live stream was found. Should happen on a VOD or page that does not have a stream." + "message": "Failed to find a live stream or video on the page.", + "description": "No live stream or video player was found." }, "message_ip_not_set": { "message": "A Roku IP is not set.", @@ -39,7 +39,7 @@ "message": "Saved", "description": "IP set" }, - "message_roku_conntect_fail": { + "message_roku_connect_fail": { "message": "Failed to connect to the Roku.", "description": "Roku connection failed" }, diff --git a/chrome/manifest.json b/chrome/manifest.json index 84fd77b..2d84dbd 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -1,7 +1,7 @@ { "name": "Twitched", "description": "__MSG_description__", - "version": "1.0.1", + "version": "1.1", "manifest_version": 2, "author": "Rolando Islas", "homepage_url": "https://www.twitched.org", diff --git a/chrome/src/background.js b/chrome/src/background.js index fe3a184..3ad0889 100644 --- a/chrome/src/background.js +++ b/chrome/src/background.js @@ -36,10 +36,12 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { /** * Send a stream deep link to the Roku - * @param login + * Provide only one of login or videoId + * @param login streamer login to use for deep link + * @param videoId video id to deep link * @param callback param - error associative array containing title and message if there is an error or null on success */ -function sendDeepLink(login, callback) { +function sendDeepLink(login, videoId, callback) { chrome.storage.local.get("rokuIp", function (values) { if (values.rokuIp === null || typeof(values.rokuIp) === "undefined") { chrome.runtime.openOptionsPage(); @@ -62,10 +64,28 @@ function sendDeepLink(login, callback) { callback() }; try { - var url = "http://{0}:8060/launch/{1}?contentId=twitch_stream&mediaType=live&twitch_user_name={2}"; + var url = "http://{0}:8060/launch/{1}?contentId={2}&mediaType={3}"; + var contentId; + var mediaType; + if (login !== null && typeof(login) !== "undefined") { + contentId = "twitch_stream_{0}".replace("{0}", login); + mediaType = "live"; + } + else if (videoId !== null && typeof(videoId) !== "undefined") { + contentId = "twitch_video_{0}".replace("{0}", videoId); + mediaType = "special"; + } + else { + // noinspection ExceptionCaughtLocallyJS + throw "Missing login/videoId"; + } request.open( "POST", - url.replace("{0}", values.rokuIp).replace("{1}", appId).replace("{2}", login), + url + .replace("{0}", values.rokuIp) + .replace("{1}", appId) + .replace("{2}", contentId) + .replace("{3}", mediaType), true ); request.send(""); @@ -134,17 +154,14 @@ chrome.pageAction.onClicked.addListener(function() { var title = chrome.i18n.getMessage(success ? "title_cast_success" : "title_cast_fail"); var message; if (success) - message = chrome.i18n.getMessage("message_cast_success", - response.streamer.displayName !== null && typeof(response.streamer.displayName) !== "undefined" ? - response.streamer.displayName : - response.streamer.login); + message = chrome.i18n.getMessage("message_cast_success"); else message = chrome.i18n.getMessage(response.error); // Send deep link if (success) { showNotification(chrome.i18n.getMessage("title_cast_in_progress"), chrome.i18n.getMessage("message_cast_in_progress")); - sendDeepLink(response.streamer.login, function (status) { + sendDeepLink(response.streamer.login, response.video.id, function (status) { if (status !== null && typeof(status) !== "undefined") { title = status.title; message = status.message; diff --git a/chrome/src/extract.js b/chrome/src/extract.js index 6c2b275..5928ebb 100644 --- a/chrome/src/extract.js +++ b/chrome/src/extract.js @@ -17,13 +17,17 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { } // Get login name var login; + var videoId; var videoPlayers = document.getElementsByClassName("video-player__container"); if (videoPlayers.length > 0) { var videoPlayer = videoPlayers[0]; login = videoPlayer.getAttribute("data-channel"); + videoId = videoPlayer.getAttribute("data-video"); + if (videoId !== null && typeof(videoId) !== "undefined") + videoId = videoId.replace("v", ""); } - // Verify - if (login === null || typeof(login) === "undefined") { + // Extract login/video id from URL + if ((login === null || typeof(login) === "undefined") && (videoId === null || typeof(videoId) === "undefined")) { console.log("Failed to get login from video player attributes."); console.log(videoPlayers); var path = document.location.pathname; @@ -33,7 +37,8 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { path = path.substring(0, path.indexOf("#")); var pathSplit = path.split("/"); console.log(pathSplit); - if (pathSplit.length !== 2) { + if ((pathSplit.length === 3 && pathSplit[1] !== "videos") || pathSplit.length < 2 || pathSplit.length > 3 || + (pathSplit.length === 2 && (pathSplit[1] === "directory" || pathSplit[1] === ""))) { console.log("Failed to get login from path."); console.log(document.location.pathname); console.log(pathSplit); @@ -41,7 +46,10 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { error: "message_find_stream_fail" }); } - login = pathSplit[1]; + if (pathSplit.length === 2) + login = pathSplit[1]; + if (pathSplit.length === 3) + videoId = pathSplit[2]; } // Get the display name var displayName; @@ -50,6 +58,9 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { streamer: { login: login, displayName: displayName + }, + video: { + id: videoId } }); }); \ No newline at end of file