-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
197 lines (184 loc) · 6.67 KB
/
background.js
File metadata and controls
197 lines (184 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var tabid;
var apiKey = "AIzaSyDFU2ViycjJgbrpgxYQF5aVrnL7l9vQ9Mw";
// Start by setting the queuetab to none and storing it
saveTabInfo(null);
// Add the video in 'info' to the queue
function addNext(info, tab) {
var videoqueue;
// Get the queue so we can change it correctly
chrome.storage.local.get({
'queue': []
}, function(result) {
videoqueue = result.queue;
// Get the video id of the video to be queued
var videoId = info.linkUrl.slice((info.linkUrl.indexOf("?v=") + 3), (info.linkUrl.indexOf("?v=") + 14));
// Get the video title from google
var url = "https://www.googleapis.com/youtube/v3/videos?id=" + videoId + "&key=" + apiKey + "&fields=items(snippet(title))&part=snippet";
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
// Save the video title and video link :D
videoqueue.push([data.items[0].snippet.title, info.linkUrl]);
chrome.storage.local.set({
'queue': videoqueue // Save under the name 'queue'
}, function() {});
});
});
}
// Save the tabid to the local storage
function saveTabInfo(newtabid) {
chrome.storage.local.set({
'tab': newtabid
}, function() {});
}
// Save the PIP state to the local storage
// function savePIPInfo(newState) {
// chrome.storage.local.set({
// 'pip': newState
// }, function() {});
// }
// CONTEXT MENUS
chrome.contextMenus.create({
id: "playnext",
title: "Play next",
contexts: ["link"],
targetUrlPatterns: ["https://www.youtube.com/watch*"]
});
// LISTENERS
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId === "playnext") {
addNext(info, tab);
}
});
// if the queue tab is closed
chrome.tabs.onRemoved.addListener(function(closedtabid, removed) {
if (closedtabid == tabid) {
chrome.tabs.query({ url: "https://www.youtube.com/*" }, function(tabs) {
if (tabs.length > 0) {
saveTabInfo(tabs[0].id);
} else {
saveTabInfo(null);
}
});
}
});
// Update current tabid if the selected tab leaves youtube
chrome.tabs.onUpdated.addListener(function(updatedTabid, changeInfo, tab) {
if (updatedTabid == tabid && changeInfo.url) {
if (!changeInfo.url.match(/youtube\.com/)) {
chrome.tabs.query({ url: "https://www.youtube.com/*" }, function(tabs) {
if (tabs.length > 0) {
saveTabInfo(tabs[0].id);
} else {
saveTabInfo(null);
}
});
}
}
});
// if the background script receives a message (probably from the content script)
chrome.runtime.onMessage.addListener(
function(msg, sender, sendresponse) {
if (msg.type == "newtab" && !tabid && tabid != "paused") { // New tab opened
saveTabInfo(sender.tab.id);
} else if (msg.type == "next" && sender.tab.id == tabid) { // Video ended in a tab
next(tabid);
} else if (msg.type == "forcenext") { // User clicked play next in one of the tabs
next(sender.tab.id);
} else if (msg.type == "playnexthere") { // User clicked the "Q here" button in one of the tabs
if (tabid == sender.tab.id) { // if the tab is already selected, pause the queue
saveTabInfo("paused");
} else { //else queue to this tab
saveTabInfo(sender.tab.id);
}
} else if (msg.type == "check") { // One of the content scripts is asking if he is the selected tab
if (tabid == sender.tab.id) {
sendresponse({
response: "selected"
});
} else {
sendresponse({
response: "notselected"
});
}
} else if (msg.type == "openurl") { // Popup wants background script to open a new tab with url
chrome.tabs.create({
url: msg.newurl
}, function(tab) {
chrome.storage.local.set({
'tab': tab.id
}, function() {});
});
} else if (msg.type == "playing") { // One of the content scripts is asking if he is the selected tab
if (tabid == sender.tab.id) {
if (msg.state) {
chrome.storage.local.set({
'playing': true
}, function() {});
} else {
chrome.storage.local.set({
'playing': false
}, function() {});
}
}
// else if (msg.type == "pip") { // User clicked picture-in-picture
// if (tabid == sender.tab.id) {
// savePIPInfo(msg.state);
// }
// }
}
});
// Play next when video ends
function next(tabtoupdate) {
var videoqueue;
// Get queue from storage
chrome.storage.local.get({
'queue': []
}, function(result) {
if (result.queue.length > 0) {
videoqueue = result.queue;
// Get first video
var vidurl = videoqueue[0][1];
// Shift queue (remove the first video)
videoqueue.shift();
// Store shifted queue
chrome.storage.local.set({
'queue': videoqueue
}, function() {});
// Set url to video url
chrome.tabs.update(tabtoupdate, {
url: vidurl
});
}
});
}
chrome.commands.onCommand.addListener(function(command) {
if (command === "playpause") {
chrome.storage.local.get({
'playing': []
}, function(result) {
if (result.playing) {
chrome.tabs.sendMessage(tabid, { type: "pause" });
} else {
chrome.tabs.sendMessage(tabid, { type: "play" });
}
});
}
});
chrome.storage.onChanged.addListener(function(changes, areaName) {
if (changes.tab) {
tabid = changes.tab.newValue;
if (tabid && tabid != "paused") { // If a tab is actually selected, let it know that it is selected
chrome.tabs.sendMessage(tabid, { type: "selected" });
}
chrome.tabs.query({ url: "https://www.youtube.com/*" }, function(tabs) {
tabs.forEach(function(tab) {
if (tab.id != tabid) { // all other tabs can fuck off
chrome.tabs.sendMessage(tab.id, { type: "notselected" });
}
});
});
}
});