forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
187 lines (175 loc) · 5.31 KB
/
Copy pathbackground.js
File metadata and controls
187 lines (175 loc) · 5.31 KB
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
'use strict';
import { dbg, log, err } from './logger.js';
import Config from './config.js';
const hermes = MU.hermes;
const win = self ?? window;
win.Config = Config;
webext.runtime.onConnect.addListener((p) => {
hermes.port = p;
/**
* Default post message to send to all connected scripts
*/
hermes.send('Config', { cfg: Config.cachedLocalStorage });
let cfg = Config.cachedLocalStorage;
hermes.getPort().onMessage.addListener((root) => {
log('Background Script: received message from content script', root);
const r = root.msg;
if (root.channel === 'Save') {
if (MU.isNull(r.params)) {
Config.local.handler.set(r.save, cfg[r.save]);
} else {
Config.local.handler.set(r.save, r.params);
}
};
if (root.channel === 'Reset') {
Config.resetToDefault();
}
});
});
let alang = [],
clang = navigator.language.split('-')[0] ?? 'en',
formatURL = txt => txt.split('.').splice(-2).join('.').replace(/\/|https:/g, '');
if(!MU.isEmpty(navigator.languages)) {
for(let nlang of navigator.languages) {
let lg = nlang.split('-')[0];
if(alang.indexOf(lg) === -1) {
alang.push(lg);
};
};
};
let cache = [];
let isCached = txt => cache.filter(c => Object.is(txt,c.host));
const webFetcher = (loc) => {
let host = formatURL(loc);
let cfg = Config.cachedLocalStorage,
urls = [],
sites = [],
custom = [],
engines = cfg.engines.filter(e => e.enabled),
blacklist = cfg.blacklist.filter(b => b.enabled),
isBlacklisted = false;
let cacheFilter = isCached(host);
if(!MU.isEmpty(cacheFilter)) {
dbg('Cache', cacheFilter[0].data);
return Promise.resolve(cacheFilter[0].data);
};
for (const b of blacklist) {
if (b.regex) {
let reg = new RegExp(b.url, b.flags);
if (!reg.test(host)) continue;
isBlacklisted = true;
}
if (!Array.isArray(b.url)) {
if (!host.includes(b.url)) continue;
isBlacklisted = true;
}
for (const c of b.url) {
if (!host.includes(c)) continue;
isBlacklisted = true;
}
}
log('Blacklisted: ', isBlacklisted, host);
if (isBlacklisted) {
return log(isBlacklisted);
};
for(const i of engines) {
if(i.url.match(/fork.org/gi)) {
if(cfg.filterlang) {
if(alang.length > 1) {
for(const a of alang) {
urls.push(`${i.url}/${a}/scripts/by-site/${host}.json`);
sites.push(MU.fetchURL(`${i.url}/${a}/scripts/by-site/${host}.json`),);
};
continue;
};
urls.push(`${i.url}/${clang}/scripts/by-site/${host}.json`);
sites.push(MU.fetchURL(`${i.url}/${clang}/scripts/by-site/${host}.json`),);
continue;
};
urls.push(`${i.url}/scripts/by-site/${host}.json`);
sites.push(MU.fetchURL(`${i.url}/scripts/by-site/${host}.json`),);
} else if(i.url.match(/(openuserjs.org|github.com)/gi)) {
urls.push(`${i.url}${host}`);
custom.push(MU.fetchURL(`${i.url}${host}`,'GET','text'),);
};
};
return Promise.all(sites).then((data) => {
let filterDeleted = data.filter(d => d.filter(ujs => !ujs.deleted)),
joinData = [...new Set([...filterDeleted[0], ...filterDeleted[1]])],
filterLang = joinData.filter((d) => {
if(cfg.filterlang) {
if(alang.length > 1) {
let rvalue = true;
for(const a of alang) {
if(!d.locale.includes(a)) {
rvalue = false;
continue;
};
};
return rvalue;
} else if(!d.locale.includes(clang)) return false;
};
return true;
});
cache.push({
host: host,
data: filterLang
});
dbg('Data', filterLang);
return filterLang;
// hermes.send('Data', {
// list: filterLang
// });
}).catch(err);
};
webext.webRequest.onHeadersReceived.addListener(
(e) => {
if (Object.is(e.type, 'main_frame')) {
const loc = new URL(e.url);
webFetcher(loc.host);
}
},
{
urls: [
// 'http://*/*',
'https://*/*',
],
}
);
/**
* [handleMessage description]
* @param msg The message itself. This is a JSON-ifiable object.
* @param sender A brws.runtime.MessageSender object representing the sender of the message.
* @param response A function to call, at most once, to send a response to the message. The function takes a single argument, which may be any JSON-ifiable object. This argument is passed back to the message sender.
*/
function handleMessage(msg, sender, response) {
// log('Message Handler:', sender, msg);
if (sender.url.includes('popup.html')) {
if(msg.location) {
webFetcher(msg.location).then(data => response(data));
} else {
webext.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
for (const tab of tabs) {
const loc = new URL(tab.url);
let cacheFilter = isCached(formatURL(loc.host));
if(!MU.isEmpty(cacheFilter)) {
response(cacheFilter[0].data);
};
};
});
};
};
if (msg.name) {
if (sender.url.includes('settings.html')) {
Config.local.handler.set(msg.name, msg.value);
response({
name: msg.name,
value: msg.value,
});
} else {
response({ value: Config.cachedLocalStorage[msg.name] });
}
}
return true;
}
webext.runtime.onMessage.addListener(handleMessage);