-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·73 lines (61 loc) · 1.6 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
69
70
71
72
73
"use strict";
let currentState = false;
/**
* @param {boolean} state
* @return {Promise}
*/
function setTabState(state) {
return browser.tabs.query({
url: 'https://www.youtube.com/watch?v=*'
})
.then((tabs) => {
tabs.forEach((t) => {
browser.tabs.sendMessage(t.id, {
tabID: t.id,
active: t.active,
state
})
.catch((err) => {
throw err;
});
});
return true;
})
.catch((err) => {
console.error(err);
return false;
});
}
async function buttonClick() {
currentState = !currentState;
let result = await setTabState(currentState);
if (result === false) { return; }
const icon = (currentState === true) ? 'on.png' : 'off.png';
result = browser.browserAction.setIcon({
path: {
48: icon
}
})
.then(() => {
browser.browserAction.setTitle({
title: currentState ? 'on' : 'off'
});
})
.catch((err) => {
console.error(err);
});
}
browser.browserAction.onClicked.addListener(buttonClick);
browser.tabs.onUpdated.addListener((tabID, changeInfo, tab) => {
if (currentState === true && changeInfo.status === 'complete') {
setTimeout(() => {
setTabState(true);
}, 500);
}
}, {
urls: ['*://www.youtube.com/watch?v=*'],
properties: ['status', 'url']
});
browser.runtime.onMessage.addListener((observer) => {
console.log(observer);
});