-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_worker.js
88 lines (81 loc) · 2.94 KB
/
service_worker.js
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
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
hostEquals: "www.youtube.com",
pathEquals: "/watch"
},
})
],
actions: [new chrome.declarativeContent.ShowAction()]
}]);
});
chrome.storage.local.set({ playingTabs: [] }); // Reloading the extension doesn't trigger onStartup
});
chrome.runtime.onStartup.addListener(function() {
chrome.storage.local.set({ playingTabs: [] });
});
function startPlaying(tabID) {
var manifest = chrome.runtime.getManifest();
chrome.action.setIcon({ tabId: tabID, path: manifest.action.enabled_icon });
chrome.action.setTitle({ tabId: tabID, title: manifest.action.enabled_title });
chrome.tabs.update(tabID, { active: true });
chrome.scripting.executeScript({ target: { tabId: tabID }, files: ["content-script.js"] });
chrome.storage.local.get("playingTabs").then((items) => {
items.playingTabs.push(tabID);
chrome.storage.local.set({ playingTabs: items.playingTabs });
});
}
function stopPlaying(tabID) {
chrome.storage.local.get("playingTabs").then((items) => {
var index = items.playingTabs.indexOf(tabID);
if(index != -1) {
items.playingTabs.splice(index, 1);
}
chrome.storage.local.set({ playingTabs: items.playingTabs });
});
var manifest = chrome.runtime.getManifest();
chrome.action.setIcon({ tabId: tabID, path: manifest.action.default_icon });
chrome.action.setTitle({ tabId: tabID, title: manifest.action.default_title });
}
chrome.action.onClicked.addListener(function(tab) {
chrome.storage.local.get("playingTabs").then((items) => {
if(items.playingTabs.indexOf(tab.id) == -1) {
startPlaying(tab.id);
} else {
stopPlaying(tab.id);
}
});
});
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message == "ended" && sender.tab !== undefined) {
// Received video ended message from player. Attempting to switch tabs
chrome.storage.local.get("playingTabs").then((items) => {
if(items.playingTabs.indexOf(sender.tab.id) != -1) {
// Find the next tab to the right (+1) in our original window
chrome.tabs.query({ windowId: sender.tab.windowId, index: sender.tab.index+1 }).then((tabs) => {
if(tabs[0] !== undefined) {
// Found a tab, make sure its Youtube before we switch to it
if(!tabs[0].url) {
return;
}
var url = new URL(tabs[0].url);
if(!(url.host == "www.youtube.com" && url.pathname == "/watch")) {
return;
}
// Switch to the tab and start the video
startPlaying(tabs[0].id);
// Optionally close the tab that just finished playing
chrome.storage.sync.get({"option-closeTab":true}).then((items) => {
if(items["option-closeTab"] === true) {
chrome.tabs.remove(sender.tab.id);
}
});
}
});
}
});
}
});