deCloudflare/tool/mypdns/ld/addon/firefox/main.js

130 lines
3.9 KiB
JavaScript
Raw Normal View History

2022-12-01 20:34:10 -05:00
let timr, memcache = {},
forcePurge = false,
2022-12-05 00:32:00 -05:00
withCloudflare = false,
2022-12-04 20:10:00 -05:00
apiHost = ['https://karma.crimeflare.eu.org:1984', 'http://karma.im5wixghmfmt7gf7wb4xrgdm6byx2gj26zn47da6nwo7xvybgxnqryid.onion'],
apiBase = apiHost[0];
2022-12-01 20:34:10 -05:00
function get_matrix_cat(domain) {
if (memcache[domain]) {
return new Promise((g, b) => {
g(memcache[domain]);
});
}
return new Promise((g, b) => {
browser.storage.local.get([domain]).then(c => {
if (c[domain]) {
memcache[domain] = c[domain];
g(memcache[domain]);
} else {
2022-12-04 20:10:00 -05:00
fetch(apiBase + '/api/mypdns/cat/', {
2022-12-01 20:34:10 -05:00
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
2022-12-05 00:32:00 -05:00
body: (withCloudflare ? 'wcf=1&' : '') + 'f=' + domain
2022-12-01 20:34:10 -05:00
}).then(r => r.json()).then(r => {
if (r && r.length == 2) {
let m = {
'cat': r[0],
'labels': r[1]
};
browser.storage.local.set({
[domain]: m
});
memcache[domain] = m;
if (Object.keys(memcache).length > 1200) {
memcache = {};
}
g(m);
} else {
b();
}
}).catch(b);
}
});
});
}
function forget_cache(x) {
if (x) {
2022-12-05 00:32:00 -05:00
browser.storage.local.get(['opt00', 'opt01', 'opt02', 'opt03', 'opt04', 'opt05', 'lastU', 'lastV']).then(g => {
2022-12-01 20:34:10 -05:00
browser.storage.local.clear();
memcache = {};
browser.storage.local.set({
'opt00': (g.opt00 == 'y' ? 'y' : 'n')
});
browser.storage.local.set({
'opt01': (g.opt01 == 'y' ? 'y' : 'n')
});
browser.storage.local.set({
'opt02': (g.opt02 == 'y' ? 'y' : 'n')
});
browser.storage.local.set({
'opt03': (g.opt03 == 'y' ? 'y' : 'n')
});
2022-12-04 20:10:00 -05:00
browser.storage.local.set({
'opt04': (g.opt04 == 'y' ? 'y' : 'n')
});
2022-12-05 00:32:00 -05:00
browser.storage.local.set({
'opt05': (g.opt05 == 'y' ? 'y' : 'n')
});
2022-12-01 20:34:10 -05:00
browser.storage.local.set({
'lastU': Math.round((new Date()).getTime() / 1000)
});
browser.storage.local.set({
'lastV': (browser.runtime.getManifest()).version
});
});
}
clearTimeout(timr);
timr = setTimeout(function () {
forget_cache(true);
}, 1814400000);
}
2022-12-05 00:32:00 -05:00
browser.storage.local.get(['lastU', 'lastV', 'opt04', 'opt05']).then(g => {
2022-12-01 20:34:10 -05:00
if (g.lastU == undefined || Math.abs(Math.round((new Date()).getTime() / 1000) - g.lastU) > 1814400 || g.lastV != (browser.runtime.getManifest()).version || forcePurge) {
forget_cache(true);
} else {
forget_cache(false);
}
2022-12-04 20:10:00 -05:00
if (g.opt04 == 'y') {
apiBase = apiHost[1];
}
2022-12-05 00:32:00 -05:00
if (g.opt05 == 'y') {
withCloudflare = true;
}
2022-12-01 20:34:10 -05:00
});
browser.runtime.onMessage.addListener((requests, sender, sendResponse) => {
if (requests) {
if (requests === 'clear') {
forget_cache(true);
return;
}
2022-12-04 20:10:00 -05:00
if (requests == 'abase-y') {
apiBase = apiHost[1];
return;
}
if (requests == 'abase-n') {
apiBase = apiHost[0];
return;
}
2022-12-05 00:32:00 -05:00
if (requests == 'withcf-y') {
withCloudflare = true;
return;
}
if (requests == 'withcf-n') {
withCloudflare = false;
return;
}
2022-12-01 20:34:10 -05:00
requests.forEach(request => {
get_matrix_cat(request).then((r) => {
browser.tabs.sendMessage(sender.tab.id, [request, r]);
}, () => {});
});
}
return;
});
browser.runtime.onInstalled.addListener(g => {
if (g.reason == 'install') {
browser.runtime.openOptionsPage();
}
});