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
82 lines (78 loc) · 2.56 KB
/
Copy pathbackground.js
File metadata and controls
82 lines (78 loc) · 2.56 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
'use strict';
import { runtime } from './ext.js';
import { log } from './logger.js';
import { us } from './api.js';
import Config from './config.js';
const win = self ?? window;
win.US = Config;
us.check(Config).then((c) => {
let messenger;
const connected = (p) => {
messenger = p;
messenger.postMessage({cfg: c.cachedLocalStorage});
messenger.onMessage.addListener((r) => {
log('Background Script: received message from content script',r);
if(r.delete) {
c.local.handler.deleteProperty(r.delete);
};
if(r.save) {
log('Background Script: saving...',r.save);
if(r.params) {
c.local.handler.set(r.save,r.params);
log('Background Script: ',r.save,r.params);
} else {
c.local.handler.set(r.save,c.cachedLocalStorage[r.save]);
log('Background Script: ',c.cachedLocalStorage[r.save]);
};
};
});
},
/**
* [handleMessage description]
* @param request The message itself. This is a JSON-ifiable object.
* @param sender A runtime.MessageSender object representing the sender of the message.
* @param callback 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.
*/
// eslint-disable-next-line no-unused-vars
handleMessage = (request, sender, callback) => {
log(sender);
if(!sender.url.includes('options.html')) {
return Promise.resolve({
value: c.cachedLocalStorage[request.name]
});
} else {
c.local.handler.set(request.name,request.value);
return Promise.resolve({
name: request.name,
value: request.value
});
};
};
// const currentTab = {};
// let tabHostname = '';
// async function init() {
// const [ tab ] = await brws.tabs.query({ active: true });
// if ( tab instanceof Object === false ) { return true; }
// Object.assign(currentTab, tab);
// let url;
// try {
// url = new URL(currentTab.url);
// tabHostname = url.hostname || '';
// } catch(ex) {
// err(ex)
// };
// log(tabHostname,currentTab);
// };
// async function tryInit() {
// try {
// await init();
// } catch(ex) {
// setTimeout(tryInit, 100);
// }
// };
// tryInit();
runtime.onConnect.addListener(connected);
runtime.onMessage.addListener(handleMessage);
});
// const countApi = 'https://greasyfork.org/scripts/by-site.json',
// adultAPI = 'https://sleazyfork.org/scripts/by-site.json';