Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
Added watch later functionality and changed the popup's upload search
Browse files Browse the repository at this point in the history
For now, the watch later feature works when you click on the watch later
button in the notification.
  • Loading branch information
Wassup789 committed Aug 1, 2016
1 parent 5e3ae99 commit 2907f37
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 38 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
Changelog
=====
## v1.1.1.0 (04-15-2015)
## v1.1.X.X (XX-XX-2016)
* Altered the popup's uploads search to include video titles
* Implemented watch later feature

## v1.1.1.0 (04-15-2016)
* Reverted to old video uploads method due to quota exhaustion
* Added four additional API keys
* Implemented search functionality
* Added subscription importing

## v1.1.0.5 (02-02-2015)
## v1.1.0.5 (02-02-2016)
* Changed channel updater to use the search API instead of the channel's playlist API (This allows more up-to-date updating)
* Fixed bug where channels would stop updating
* Added locales/languages
Expand Down
6 changes: 5 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,16 @@
},
"background_notificationWatch": {
"description": "",
"message": "Watch Video"
"message": "Play Video"
},
"background_notificationClose": {
"description": "",
"message": "Dismiss"
},
"background_notificationWatchLater": {
"description": "",
"message": "Watch Later"
},
"background_notificationLogCheck": {
"description": "",
"message": "Checking YouTube User: "
Expand Down
4 changes: 4 additions & 0 deletions _locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@
"description": "",
"message": "Cerca"
},
"background_notificationWatchLater": {
"description": "",
"message": "Ver más tarde"
},
"background_notificationLogCheck": {
"description": "",
"message": "Comprobación de YouTube del usuario: "
Expand Down
4 changes: 4 additions & 0 deletions _locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@
"description": "",
"message": "Près"
},
"background_notificationWatchLater": {
"description": "",
"message": "À regarder plus tard"
},
"background_notificationLogCheck": {
"description": "",
"message": "Vérification YouTube utilisateur: "
Expand Down
Binary file added img/ic_watchlater.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/ic_watchlater.psd
Binary file not shown.
177 changes: 165 additions & 12 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var wyn = {};
"notification_watch_icon": "img/ic_play.png",
"notification_close": getString("notificationClose"),
"notification_close_icon": "img/ic_close.png",
"notification_watchlater": getString("notificationWatchLater"),
"notification_watchlater_icon": "img/ic_watchlater.png",
"notification_main_icon": "img/ic_youtube.png",
"notification_log_check": getString("notificationLogCheck"),
"notification_log_new": getString("notificationLogNew"),
Expand Down Expand Up @@ -64,24 +66,47 @@ if(localStorage.getItem("settings") == null)
},
updated: {
enabled: false
},
watchlater: {
id: ""
}
}));

fixItems();
function fixItems(){
var settings = JSON.parse(localStorage.getItem("settings"));
if(typeof settings.addBtn === "undefined"){
if(typeof settings.notifications === "undefined" || settings.notifications.enabled === "undefined" || settings.notifications.volume === "undefined"){
settings.notifications = {
enabled: true,
volume: 100
};
localStorage.setItem("settings", JSON.stringify(settings));
}
if(typeof settings.tts === "undefined" || settings.tts.enabled === "undefined" || settings.tts.type === "undefined"){
settings.notifications = {
enabled: false,
type: 1
};
localStorage.setItem("settings", JSON.stringify(settings));
}
if(typeof settings.addBtn === "undefined" || settings.addBtn.enabled === "undefined"){
settings.addBtn = {
enabled: true
};
localStorage.setItem("settings", JSON.stringify(settings));
}
if(typeof settings.updated === "undefined"){
if(typeof settings.updated === "undefined" || settings.updated.enabled === "undefined"){
settings.updated = {
enabled: false
};
localStorage.setItem("settings", JSON.stringify(settings));
}
if(typeof settings.watchlater === "undefined" || settings.watchlater.id === "undefined"){
settings.watchlater = {
id: ""
};
localStorage.setItem("settings", JSON.stringify(settings));
}
}

chrome.notifications.onClicked.addListener(onNotificationClick);
Expand Down Expand Up @@ -147,7 +172,7 @@ $(function(){
sendResponse(settings.addBtn.enabled);
break;
case "receivedToken":
sendResponse(onReceivedToken());
sendResponse(onReceiveImportToken());
break;
case "importApproved":
sendResponse(onImportApproved());
Expand Down Expand Up @@ -421,7 +446,7 @@ function removeYoutube(type, name, refresh, fromContentScript){
* @param {string} id The name of the channel's ID
* @param {number} index The index for an element (for injected.js)
*/
function doesYoutubeExist(id, index){
function doesYoutubeExist(id, index){
var channels = JSON.parse(localStorage.getItem("channels"));
for(var i = 0; i < channels.length; i++){
if(channels[i].id == id)
Expand Down Expand Up @@ -562,6 +587,9 @@ function checkYoutube(num, refresh, batch) {
buttons: [{
title: wyn.strings.notification_watch,
iconUrl: wyn.strings.notification_watch_icon
}, {
title: wyn.strings.notification_watchlater,
iconUrl: wyn.strings.notification_watchlater_icon
}, {
title: wyn.strings.notification_close,
iconUrl: wyn.strings.notification_close_icon
Expand Down Expand Up @@ -758,13 +786,24 @@ function onNotificationClick(ntID){
*/
function onNotificationButtonClick(ntID, btnID){
if(typeof ntID.split("-")[4] !== "undefined") {
if(btnID == 0){
var channels = JSON.parse(localStorage.getItem("channels"));
createTab("https://www.youtube.com/watch?v=" + channels[ntID.split("-")[4]].latestVideo.id);
console.log("User clicked on \"" + wyn.strings.notification_watch + "\" button; NTID: " + ntID);
console.log("Sending user to https://www.youtube.com/watch?v=" + channels[ntID.split("-")[4]].latestVideo.id);
}else if(btnID == 1){
console.log("User clicked on \"" + wyn.strings.notification_close + "\" button; NTID: " + ntID);
switch(btnID) {
case 0:
var channels = JSON.parse(localStorage.getItem("channels"));
createTab("https://www.youtube.com/watch?v=" + channels[ntID.split("-")[4]].latestVideo.id);
console.log("User clicked on \"" + wyn.strings.notification_watch + "\" button; NTID: " + ntID);
console.log("Sending user to https://www.youtube.com/watch?v=" + channels[ntID.split("-")[4]].latestVideo.id);
break;
case 1:
var channels = JSON.parse(localStorage.getItem("channels")),
data = channels[ntID.split("-")[4]].latestVideo;
data.index = ntID.split("-")[4];

requestExtendedToken(data);
console.log("User clicked on \"" + wyn.strings.notification_watchlater + "\" button; NTID: " + ntID);
break;
case 2:
console.log("User clicked on \"" + wyn.strings.notification_close + "\" button; NTID: " + ntID);
break;
}
}
chrome.notifications.clear(ntID);
Expand Down Expand Up @@ -814,6 +853,9 @@ wyn.testNotify = function(){
buttons: [{
title: wyn.strings.notification_watch,
iconUrl: wyn.strings.notification_watch_icon
}, {
title: wyn.strings.notification_watchlater,
iconUrl: wyn.strings.notification_watchlater_icon
}, {
title: wyn.strings.notification_close,
iconUrl: wyn.strings.notification_close_icon
Expand Down Expand Up @@ -869,6 +911,9 @@ wyn.forceNotification = function(id) {
buttons: [{
title: wyn.strings.notification_watch,
iconUrl: wyn.strings.notification_watch_icon
}, {
title: wyn.strings.notification_watchlater,
iconUrl: wyn.strings.notification_watchlater_icon
}, {
title: wyn.strings.notification_close,
iconUrl: wyn.strings.notification_close_icon
Expand All @@ -890,7 +935,7 @@ wyn.resetVideos = function(){
/**
* Ran when the subscription import has been approved
*/
function onReceivedToken() {
function onReceiveImportToken() {
chrome.identity.getAuthToken({ interactive: false }, function(access_token) {
if(chrome.runtime.lastError)
return;
Expand Down Expand Up @@ -991,4 +1036,112 @@ function importChannelsPost(){
setTimeout(function(){
chrome.extension.sendMessage({type: "refreshPage"});
}, 10*1000);
}

/**
* Requests the user to approve the extended OAuth request
*/
function requestExtendedToken(videoInfo) {
chrome.identity.getAuthToken({
interactive: true,
scopes: [
"https://www.googleapis.com/auth/youtube.force-ssl"
]
},
function(token){
if(!chrome.runtime.lastError){
onReceiveExtendedToken(videoInfo);
}
}
);
}

/**
* Ran when the extended token is approved
*/
function onReceiveExtendedToken(videoInfo) {
chrome.identity.getAuthToken({
interactive: false,
scopes: [
"https://www.googleapis.com/auth/youtube.force-ssl"
]
}, function(access_token) {
if(chrome.runtime.lastError)
return;

var settings = JSON.parse(localStorage.getItem("settings"));

if(settings.watchlater.id == "") {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true");
xhr.setRequestHeader("Authorization", "Bearer " + access_token);
xhr.onload = function(){
var data = JSON.parse(this.response),
watchLaterPlaylist = data.items[0].contentDetails.relatedPlaylists.watchLater;

settings.watchlater.id = watchLaterPlaylist;
localStorage.setItem("settings", JSON.stringify(settings));

onReceiveExtendedTokenPost(access_token, videoInfo);
};
xhr.send();
}else
onReceiveExtendedTokenPost(access_token, videoInfo);
});
}

/**
* Ran after we receive the access token and watch later playlist
*/
function onReceiveExtendedTokenPost(access_token, videoInfo) {
var settings = JSON.parse(localStorage.getItem("settings")),
xhr = new XMLHttpRequest(),
requestData = {
snippet: {
playlistId: settings.watchlater.id,
resourceId: {
kind: "youtube#video",
videoId: videoInfo.id
}
}
};

xhr.open("POST", "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet");
xhr.setRequestHeader("Authorization", "Bearer " + access_token);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function(){
var data = JSON.parse(this.response),
title = "Video added to Watch Later",
message = "\"" + videoInfo.title + "\" has been added to the Watch Later playlist.";

if(data.error) {
if(data.error.code == 409) {
title = "Video already exists in Watch Later";
message = "\"" + videoInfo.title + "\" already exists in the Watch Later playlist.";
}else{
title = "Error Code: " + data.error.code;
message = data.error.message
}
}

var ntID = rndStr(10) + "-" + rndStr(5) + "-" + rndStr(5) + "-" + rndStr(5) + "-" + videoInfo.index;
var options = {
type: "basic",
priority: 0,
title: title,
message: message,
iconUrl: videoInfo.thumbnail,
buttons: [{
title: wyn.strings.notification_watch,
iconUrl: wyn.strings.notification_watch_icon
}]
};

chrome.notifications.create(ntID, options, function(){
wyn.notificationSound.volume = parseInt(JSON.parse(localStorage.getItem("settings"))["notifications"]["volume"])/100;
wyn.notificationSound.play()
notifyTTS(options);
});
};
xhr.send(JSON.stringify(requestData));
}
Loading

0 comments on commit 2907f37

Please sign in to comment.