summaryrefslogtreecommitdiffstats
path: root/ismitmlink/bg.js
blob: 5e57569274954f06222475d04beabd6213cdc3bd (plain)
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
const apiurl = 'https://searxes.danwin1210.me/collab/open/ismitm.php';
let mymemory = {};

function ismitm(f) {
	return new Promise((g, b) => {
		fetch(apiurl, {
			method: 'POST',
			mode: 'cors',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: 'f=' + f
		}).then(function (r) {
			return r.json();
		}).then(function (r) {
			if (r[0]) {
				g(r[1]);
			} else {
				b();
			}
		}).catch(b);
	});
}

browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
	if (request && sender) {
		if (mymemory[request] != undefined) {
			let rlt = mymemory[request];
			if (Object.keys(mymemory).length > 20000) {
				let cnt = 1;
				for (let t in mymemory) {
					if (cnt > 10) {
						break;
					}
					mymemory[t] = null;
					delete mymemory[t];
					cnt++;
				}
			}
			browser.tabs.sendMessage(sender.tab.id, [request, rlt]);
		} else {
			ismitm(request).then(function (a) {
				mymemory[request] = a;
				browser.tabs.sendMessage(sender.tab.id, [request, a]);
			}, function () {
				browser.tabs.sendMessage(sender.tab.id, [request, false]);
			});
		}
	}
});