-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
107 lines (88 loc) · 2.83 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
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
var currentTab;
var currentState;
/*
* Enable or disable Debugging
*/
function toggleButton(event) {
console.log('toggleButton');
console.log(typeof event);
console.log(event);
function onExecuted(result) {
browser.tabs.executeScript(
currentTab.id, {
file: "cookies.js"
});
}
function onError(error) {
console.log(`Can't execute script: ${error}, for current tab ID ` + currentTab.id);
}
var userTriggered = typeof event === 'object';
var executing = browser.tabs.executeScript(
currentTab.id, {
code: "var currentState = "+ currentState +"; var userTriggered = " + userTriggered + ";"
});
executing.then(onExecuted, onError);
}
browser.browserAction.onClicked.addListener(toggleButton);
/*
* Switches currentTab to reflect the currently active tab
*/
function updateActiveTab(e) {
console.log('Event', e);
function isSupportedProtocol(urlString) {
var supportedProtocols = ["https:", "http:", "ftp:", "file:"];
var url = document.createElement('a');
url.href = urlString;
return supportedProtocols.indexOf(url.protocol) != -1;
}
function updateTab(tabs) {
if (tabs[0]) {
currentTab = tabs[0];
console.log('Current tab is ' + currentTab.id);
if (isSupportedProtocol(currentTab.url)) {
toggleButton();
}
}
}
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then(updateTab);
}
function updateButton(result) {
if (typeof currentTab.id === 'undefined') {
console.log('No current tab yet')
return;
}
browser.browserAction.setIcon({
path: result ? {
32: "data/icons/debug_32_active.png",
48: "data/icons/debug_32_active.png"
} : {
32: "data/icons/debug_48_inactive.png",
48: "data/icons/debug_48_inactive.png"
},
tabId: currentTab.id
});
browser.browserAction.setTitle({
title: result ? 'Disable Debugging' : 'Enable Debugging',
tabId: currentTab.id
});
}
// listen to tab URL changes
browser.tabs.onUpdated.addListener(updateActiveTab);
// listen to tab switching
browser.tabs.onActivated.addListener(updateActiveTab);
// listen for window switching
browser.windows.onFocusChanged.addListener(updateActiveTab);