-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
68 lines (61 loc) · 2.54 KB
/
background.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
/*jslint indent:2*/
/*global chrome*/
// This script is run when the addon is loaded by the browser (probably when the first window opens).
(function () {
var _playingYoutubeTabIds = {};
var _activeTab = null;
// Sends the message to all youtube tabs we know about. Optionally except 1.
var messageAllRegisteredYoutubeTabs = function (message, exceptTabId) {
Object.keys(_playingYoutubeTabIds).forEach(function (tabId) {
var match = tabId.match(/^(\d+)_tabId$/);
if (match !== null) {
tabId = parseInt(match[1], 10);
if (tabId !== exceptTabId) {
delete _playingYoutubeTabIds[tabId + "_tabId"];
chrome.tabs.sendMessage(tabId, {message: message});
}
}
});
};
// Handle messages from content scripts in youtube tabs
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
if (typeof request === "object" && request.message !== undefined) {
if (request.message === "youtubeExtensionVideoStarted") {
_playingYoutubeTabIds[sender.tab.id + "_tabId"] = true;
messageAllRegisteredYoutubeTabs("youtubeExtensionPauseVideo", sender.tab.id);
}
}
});
// Tells the active tab (if there is one) to start polling
// and the last active (if there is) to stop.
// This will attempt to send to any tab, not just youtube ones.
var updatePollingTab = function (newTabId) {
var oldTabId = _activeTab;
_activeTab = newTabId;
if (oldTabId) {
chrome.tabs.sendMessage(oldTabId, {message: 'youtubeExtensionStopPolling'});
}
if (newTabId) {
chrome.tabs.sendMessage(newTabId, {message: 'youtubeExtensionStartPolling'});
}
};
// Tell youtube tabs whether they're active so they don't always have to poll.
// When a tab changes,
chrome.tabs.onActivated.addListener(function (activeInfo) {
updatePollingTab(activeInfo.tabId);
});
// and when the focussed window changes
chrome.windows.onFocusChanged.addListener(function (windowId) {
if (windowId === chrome.windows.WINDOW_ID_NONE) {
updatePollingTab(null);
} else {
chrome.tabs.query({ active: true, windowId: windowId }, function (tabs) {
updatePollingTab(tabs.length > 0 ? tabs[0].id : null);
});
}
});
// TODO:
// Could I have some code here to run on start up (which will run on extension install/enable)
// to query all tabs for their URLs and inject the content script so that it works without
// having to refresh all open youtube tabs?
}());