-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrweb.background.js
245 lines (212 loc) · 5.79 KB
/
rweb.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
importScripts('rweb.helpers.js', 'rweb.sync.js');
const labels = [
'Disable RWeb for DOMAIN',
'Re-enable RWeb for DOMAIN',
];
chrome.runtime.onInstalled.addListener(function(info) {
chrome.contextMenus.create({
"title": labels[0],
"id": 'rwebxable',
"contexts": ['action'],
});
});
chrome.contextMenus.onClicked.addListener(async function(info, tab) {
if (!tab.url) {
return console.warn('[RWeb] Could not read origin tab URL. Check optional permissions.');
}
const host = rweb.host(tab.url);
console.time('get & save disabled');
rweb.browser.storage.local.get('disabled', function(items) {
const disabled = items.disabled || {};
toggleDisabled(disabled, host, tab);
});
});
// try {
function toggleDisabled(cache, host, tab) {
cache[host] = !cache[host];
const nowDisabled = cache[host];
// Save back into storage.local
if ( !cache[host] ) {
delete cache[host];
}
rweb.browser.storage.local.set({"disabled": cache}, function() {
console.timeEnd('get & save disabled');
});
// Update label
var newLabel = labels[ Number(nowDisabled) ];
updateLabel(nowDisabled, host, tab.id);
// Update tabs, like options.js does
function updateTabs(css) {
var message = {"rweb": {"disabled": nowDisabled, "css": css}};
rweb.browser.tabs.query({}, function(tabs) {
tabs.forEach(function(tab) {
var tabHost = rweb.host(tab.url);
// Only EXACT matches, no wildcards etc
if ( tabHost == host ) {
rweb.browser.tabs.sendMessage(tab.id, message, function(rsp) {
// console.log('Sent new status to origin tab', tab.url, rsp);
});
}
});
});
}
// Immediately send the disable command
if ( nowDisabled ) {
updateTabs('');
}
// Collect live CSS, because the page might be loaded disabled
else {
rweb.site(host, function(site) {
if ( site && site.css ) {
updateTabs(site.css);
}
});
}
}
rweb.browser.tabs.onUpdated.addListener(function(tabId, info, tab) {
// console.log('onUpdated', tabId, info, tab);
if ( info.status && tab.active ) {
updateLabelStatus(tab);
}
});
rweb.browser.tabs.onActivated.addListener(function(info) {
// console.log('onActivated', info);
rweb.browser.tabs.get(info.tabId, function(tab) {
updateLabelStatus(tab);
});
});
rweb.browser.windows.onFocusChanged.addListener(function(windowId) {
rweb.browser.windows.get(windowId, {"populate": true}, function(window) {
var e = rweb.browser.runtime.lastError; // Shut up, Chrome
if ( !window ) return;
// console.log('onFocusChanged', windowId, window);
for ( var i=window.tabs.length-1; i>=0; i-- ) {
var tab = window.tabs[i];
if ( tab.active ) {
updateLabelStatus(tab);
break;
}
};
});
});
function updateLabelStatus(tab) {
var host = rweb.host(tab.url);
rweb.browser.storage.local.get('disabled', function(items) {
var disabled = items.disabled || {};
updateLabel(host in disabled, host, tab.id);
});
}
function updateLabel(disabled, host, tabId) {
// Update label
var newLabel = labels[ Number(disabled) ].replace('DOMAIN', host);
rweb.browser.contextMenus.update('rwebxable', {"title": newLabel});
// Update badge
if ( disabled ) {
// Show X on red
rweb.browser.action.setBadgeBackgroundColor({
color: [255, 0, 0, 255], // red
tabId: tabId,
});
rweb.browser.action.setBadgeText({
text: 'x',
tabId: tabId,
});
}
else {
// Hide X
rweb.browser.action.setBadgeText({
text: '',
tabId: tabId,
});
}
}
// }
// catch (ex) {
// No permission?
// DEBUG //
// throw ex;
// DEBUG //
// }
rweb.browser.action.onClicked.addListener(function(tab) {
const u = new URL(tab.url);
const host = rweb.host(u.host);
var uri = rweb.browser.runtime.getURL('options/options.html');
uri += '#' + host;
rweb.browser.tabs.create({
url: uri,
index: tab.index + 1,
// openerTabId: tab.id,
});
});
var optionsClosedTimer;
rweb.browser.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
// Inject JS
if ( msg && msg.inject && msg.inject.js ) {
const runscript = function(data) {
const scr = document.createElement('script');
scr.dataset.origin = 'rweb';
scr.textContent = data;
(document.head || document.body || document.documentElement).append(scr);
};
chrome.scripting.executeScript({
target: {
tabId: sender.tab.id
},
world: 'MAIN',
func: runscript,
args: [msg.inject.js],
});
return sendResponse(true);
}
// Content script matched site
if ( msg && msg.site ) {
rweb.browser.action.getBadgeText({tabId: sender.tab.id}, function(text) {
var num = (parseFloat(text) || 0) + 1;
rweb.browser.action.setBadgeText({
text: String(num),
tabId: sender.tab.id
});
});
rweb.browser.action.setBadgeBackgroundColor({
color: '#000',
tabId: sender.tab.id
});
return sendResponse(true);
}
// Forced auto-download from content script
if ( msg && msg.forceAutoDownload ) {
rweb.sync.download(function(summary) {
if ( summary.imported ) {
rweb.log('download', true, summary.changes, function() {
// Log saved
});
}
sendResponse(summary);
}, true);
return;
}
// Options page closed
if ( msg && msg.optionsClosed && rweb.sync ) {
optionsClosedTimer = setTimeout(function() {
console.log('Uploading automatically, because options page closed');
rweb.sync.upload(function(summary) {
if (summary.unconnected) {
console.log('Nothing uploaded, because unconnected');
return;
}
var changes = !summary.dirty ? 0 : null;
rweb.log('upload', true, changes, function() {
// Log saved
});
// Done
console.log('Automatic upload done');
}, true);
}, 1000);
return sendResponse(true);
}
// Options page opened
if ( msg && msg.optionsOpened ) {
clearTimeout(optionsClosedTimer);
return sendResponse(true);
}
});