-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathbackground.js
More file actions
154 lines (125 loc) · 4.14 KB
/
background.js
File metadata and controls
154 lines (125 loc) · 4.14 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
/*
* Copyright 2015 Boris Smus. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var tabsState = {};
var controllableTabs = {};
var playingTabs = {};
var lastPlayingTab = -1;
var activeTab = -1;
function togglePlaying(tabId, state) {
if(state === undefined) {
state = !playingTabs[tabId];
}
if (playingTabs.hasOwnProperty(tabId) && !state) {
delete playingTabs[tabId];
} else if (!playingTabs.hasOwnProperty(tabId) && state) {
playingTabs[tabId] = true;
}
}
function sendCommand(tabId, command) {
if (command === 'play-pause') {
togglePlaying(tabId);
}
chrome.tabs.sendMessage(tabId, {command: command});
}
function registerTab(tabId) {
if (!controllableTabs.hasOwnProperty(tabId)) {
controllableTabs[tabId] = true;
chrome.tabs.get(tabId, function(tab) {
if (tab.audible) {
togglePlaying(tabId, true);
}
});
chrome.pageAction.show(tabId);
//TODO: save state in local storage
tabsPluginState(tabId, true);
}
}
function unregisterTab(tabId) {
if (controllableTabs.hasOwnProperty(tabId)) {
delete controllableTabs[tabId];
delete tabsPluginState[tabId];
togglePlaying(tabId, false);
}
}
function checkAudible(tabId, changeInfo) {
if (!changeInfo.hasOwnProperty('audible')) {
return;
}
if (!controllableTabs.hasOwnProperty(tabId)) {
console.log('ignore unregistered', tabId);
return;
}
if (changeInfo.audible) {
playingTabs[tabId] = true;
lastPlayingTab = tabId;
console.log('update playingTabs', playingTabs);
} else {
togglePlaying(tabId, false);
lastPlayingTab = tabId;
}
}
function tabsPluginState(tabId, state) {
tabId = tabId.id || tabId
tabsState[tabId] = state !== undefined && state || !tabsState[tabId]
postfix = ''
if (!tabsState[tabId]) {
postfix = '-inactive'
}
chrome.pageAction.setIcon({
tabId: tabId,
path: {
'19': 'icons/icon19' + postfix + '.png',
'38': 'icons/icon38' + postfix + '.png'
}
});
}
function processAction(command) {
console.log('Command:', command);
var isPlaying = false;
for (tabId in playingTabs) {
if (playingTabs.hasOwnProperty(tabId) && tabsState[tabId]) {
isPlaying = true;
sendCommand(+tabId, command);
console.log('control playing', tabId);
}
}
if (isPlaying) {
return;
}
if (controllableTabs[activeTab] && tabsState[activeTab]){
console.log('control active', activeTab);
sendCommand(activeTab, command)
} else if (lastPlayingTab !== -1 && tabsState[lastPlayingTab]) {
console.log('control lastPlayingTab', lastPlayingTab)
sendCommand(lastPlayingTab, command);
}
}
function processRegisterMessage(request, sender, sendResponse) {
console.log('Received tab message: ', request);
if (request.command == 'registerTab' && sender.tab) {
registerTab(sender.tab.id);
} else if (request.command == 'unregisterTab' && sender.tab) {
unregisterTab(sender.tab.id);
}
}
function switchActiveTab(evt) {
activeTab = evt.tabId;
}
chrome.commands.onCommand.addListener(processAction);
chrome.tabs.onRemoved.addListener(unregisterTab);
chrome.tabs.onUpdated.addListener(checkAudible);
chrome.pageAction.onClicked.addListener(tabsPluginState);
chrome.runtime.onMessage.addListener(processRegisterMessage);
chrome.tabs.onActivated.addListener(switchActiveTab);