-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworker.js
99 lines (95 loc) · 2.83 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
chrome.action.onClicked.addListener(tab => {
chrome.runtime.sendMessage({
cmd: 'ping'
}, r => {
chrome.runtime.lastError;
if (r !== 'pong') {
chrome.tabs.create({
url: '/data/window/index.html',
index: tab.index + 1
});
}
});
});
chrome.runtime.onMessage.addListener((request, sender) => {
if (request.cmd === 'bring-to-front') {
chrome.tabs.update(sender.tab.id, {
active: true
});
chrome.windows.update(sender.tab.windowId, {
focused: true
});
}
else if (request.cmd === 'state') {
chrome.action.setIcon({
tabId: sender.tab.id,
path: {
'16': '/data/icons' + (request.active ? '/active' : '') + '/16.png',
'32': '/data/icons' + (request.active ? '/active' : '') + '/32.png',
'48': '/data/icons' + (request.active ? '/active' : '') + '/48.png'
}
});
}
});
{
const once = () => {
if (once.done) {
return;
}
once.done = true;
chrome.contextMenus.create({
id: 'mode',
title: 'Mode',
contexts: ['action']
});
chrome.contextMenus.create({
id: 'mode:vertical',
title: 'Vertical',
contexts: ['action'],
parentId: 'mode',
type: 'radio'
});
chrome.contextMenus.create({
id: 'mode:horizontal',
title: 'Horizontal',
contexts: ['action'],
parentId: 'mode',
type: 'radio'
});
};
chrome.runtime.onInstalled.addListener(once);
}
chrome.contextMenus.onClicked.addListener(info => {
if (info.menuItemId.startsWith('mode:')) {
chrome.storage.local.set({
mode: info.menuItemId.slice(5)
});
}
});
/* FAQs & Feedback */
{
const {management, runtime: {onInstalled, setUninstallURL, getManifest}, storage, tabs} = chrome;
if (navigator.webdriver !== true) {
const page = getManifest().homepage_url;
const {name, version} = getManifest();
onInstalled.addListener(({reason, previousVersion}) => {
management.getSelf(({installType}) => installType === 'normal' && storage.local.get({
'faqs': true,
'last-update': 0
}, prefs => {
if (reason === 'install' || (prefs.faqs && reason === 'update')) {
const doUpdate = (Date.now() - prefs['last-update']) / 1000 / 60 / 60 / 24 > 45;
if (doUpdate && previousVersion !== version) {
tabs.query({active: true, lastFocusedWindow: true}, tbs => tabs.create({
url: page + '?version=' + version + (previousVersion ? '&p=' + previousVersion : '') + '&type=' + reason,
active: reason === 'install',
...(tbs && tbs.length && {index: tbs[0].index + 1})
}));
storage.local.set({'last-update': Date.now()});
}
}
}));
});
setUninstallURL(page + '?rd=feedback&name=' + encodeURIComponent(name) + '&version=' + version);
}
}