-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrweb.content.js
114 lines (92 loc) · 2.99 KB
/
rweb.content.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
const RWEB_CHANNEL = new BroadcastChannel('rweb');
/**
* Load sites and inject CSS & JS
*/
if ( document.documentElement && document.documentElement.nodeName == 'HTML' && location.protocol != 'chrome-extension:' ) {
var host = rweb.host(location.host);
rweb.site(host, function(site, meta) {
var specific = site ? site.specific : 0;
var wildcard = site ? site.wildcard : 0;
document.documentElement.dataset.rweb = host + '/' + specific + '/' + wildcard;
if ( site && !meta.disabled ) {
// Save stats
if ( site.specific ) {
rweb.matched(host);
}
// Add CSS & JS
site.css && rweb.css(site.css);
site.js && rweb.js(site.js);
}
if ( !meta.lastDownload || meta.lastDownload < Date.now() - rweb.MUST_DOWNLOAD_EVERY_N_MINUTES * 60000 ) {
if ( !meta.downloadingSince || meta.downloadingSince < Date.now() - 10000 ) {
if ( !meta.dirty ) {
console.log('[RWeb] WILL START AUTO-DOWNLOAD NOW! See background script for log.');
rweb.browser.runtime.sendMessage({forceAutoDownload: true}, function(response) {
if (response && response.imported) {
console.log('[RWeb] DOWNLOADED SITES!');
}
else {
// console.warn('[RWeb] NOT DOWNLOADED...', response);
}
});
}
else {
if (meta.lastDownload) {
// Only nofity if this is news. If there has NEVER been a download, we're probably not connected.
console.warn("[RWeb] NOT STARTING AUTO-DOWNLOAD, because local state is dirty. Go to options page to fix.");
}
}
}
}
});
RWEB_CHANNEL.addEventListener('message', function(e) {
if (!e.data.sendCallback) return;
const fn = new Function('data', 'resolve', `return (${e.data.sendCallback})(data, resolve)`);
const resolve = rsp => RWEB_CHANNEL.postMessage({receiveData: rsp});
fn(e.data.sendData, resolve);
});
}
/**
* Listen for disabling this host/website
*/
rweb.browser.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if ( msg.rweb && 'disabled' in msg.rweb ) {
// Immediately update page
if ( msg.rweb.disabled ) {
// Remove all CSS
disableLocalRWebCSS();
}
else {
enableLocalRWebCSS(msg.rweb.css);
}
sendResponse(true);
}
});
function disableLocalRWebCSS() {
[].forEach.call(document.querySelectorAll('style[data-origin="rweb"]'), function(el) {
el.disabled = true;
});
}
function enableLocalRWebCSS(css) {
doCSSUpdate(css);
}
/**
* Listen for CSS updates from the options page
*/
function doCSSUpdate(css) {
console.debug('[RWeb] Updating CSS:', rweb.thousands(css.length) + ' bytes');
// Delete existing style[data-origin="rweb"]
[].forEach.call(document.querySelectorAll('style[data-origin="rweb"]'), function(el) {
el.remove();
});
// Create 1 new style[data-origin="rweb"]
rweb.css(css);
}
rweb.browser.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
// console.debug('onMessage', msg, sender);
if ( msg && msg.cssUpdate != null /*&& !sender.tab*/ ) {
var css = msg.cssUpdate;
doCSSUpdate(css);
sendResponse(true);
}
});