-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmenu.js
86 lines (76 loc) · 2.85 KB
/
menu.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @author [email protected]
*/
var started = 'off';
const debug_mode = false;
window.onload = function () {
document.getElementById('config').addEventListener('click', function (e) {
start_config();
});
document.getElementById('start_stop').addEventListener('click', function (e) {
start_modify();
});
loadFromBrowserStorage(['started'], function (result) {
started = result.started;
if (started === 'on') document.getElementById('start_stop').value = 'Stop';
});
};
function loadFromBrowserStorage(item, callback_function) {
chrome.storage.local.get(item, callback_function);
}
function storeInBrowserStorage(item, callback_function) {
chrome.storage.local.set(item, callback_function);
}
function start_modify() {
if (started === 'off') {
storeInBrowserStorage({started: 'on'}, function () {
started = 'on';
if (useManifestV3) {
applyConfigWithManifestV3();
} else {
chrome.runtime.sendMessage('on');
}
document.getElementById('start_stop').value = 'Stop';
// if exists reload config tab , to get the start/stop information correct
chrome.tabs.query({currentWindow: true}, reloadConfigTab);
});
} else {
storeInBrowserStorage({started: 'off'}, function () {
if (useManifestV3) {
removeConfigWithManifestV3(() => {});
} else {
chrome.runtime.sendMessage('off');
}
started = 'off';
document.getElementById('start_stop').value = 'Start';
// if exists reload config tab , to get the start/stop information correct
chrome.tabs.query({currentWindow: true}, reloadConfigTab);
});
}
}
function reloadConfigTab(tabs) {
var config_tab;
// search for config tab
for (let tab of tabs) {
if (tab.url.startsWith(chrome.runtime.getURL(''))) config_tab = tab;
}
// config tab exists , reload it
if (config_tab) chrome.tabs.reload(config_tab.id);
}
function start_config() {
chrome.tabs.query({currentWindow: true}, loadConfigTab);
}
function loadConfigTab(tabs) {
var config_tab;
// search for config tab
for (let tab of tabs) {
if (tab.url.startsWith(chrome.runtime.getURL(''))) config_tab = tab;
}
// config tab exits , put the focus on it
if (config_tab) chrome.tabs.update(config_tab.id, {active: true});
// else create a new tab
else chrome.tabs.create({url: '/popup/config.html'});
}