Skip to content

Commit

Permalink
Add support for channel videos
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandoislas committed Jan 12, 2018
1 parent bb2a24c commit fb46f52
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
10 changes: 5 additions & 5 deletions chrome/_locales/en_US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
35 changes: 26 additions & 9 deletions chrome/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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("");
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions chrome/src/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,15 +37,19 @@ 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);
return 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;
Expand All @@ -50,6 +58,9 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
streamer: {
login: login,
displayName: displayName
},
video: {
id: videoId
}
});
});

0 comments on commit fb46f52

Please sign in to comment.