forked from dodying/UserJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached.user.js
112 lines (105 loc) · 3.5 KB
/
cached.user.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
/* eslint-env browser */
// ==UserScript==
// @name []cached
// @description cached
// @include http://*
// @include https://*
// @version 1.0.109
// @created 2019-11-24 18:48:14
// @modified 2019-11-25 09:31:55
// @author dodying
// @namespace https://github.com/dodying/UserJs
// @supportURL https://github.com/dodying/UserJs/issues
// @icon https://gitee.com/dodying/userJs/raw/master/Logo.png
// @run-at document-end
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @noframes
// ==/UserScript==
/* eslint-disable no-debugger */
(async function () {
const blacklist = GM_getValue('blacklist', []);
const checkedlist = GM_getValue('checkedlist', []);
const { host } = window.location;
const url = `https://2tool.top/kuaizhao.php?k=${encodeURIComponent(window.location.href)}`;
if (blacklist.includes(host)) {
GM_registerMenuCommand(`Cached: Effect ${host}`, () => {
const blacklist = GM_getValue('blacklist', []);
if (blacklist.includes(host)) {
blacklist.splice(blacklist.indexOf(host), 1);
GM_setValue('blacklist', blacklist);
window.location.reload();
}
});
return;
}
GM_registerMenuCommand(`Cached: DO NOT Effect ${host}`, () => {
const blacklist = GM_getValue('blacklist', []);
if (!blacklist.includes(host)) {
blacklist.push(host);
GM_setValue('blacklist', blacklist);
window.location.reload();
}
});
if (checkedlist.includes(host)) {
GM_registerMenuCommand('Cached: Show cached', () => {
GM_openInTab(url, true);
});
return;
}
const body = await xhrSync(url);
const args = body.response.match(/doLoadKz\(('.*)\)/g).map((i) => i.match(/doLoadKz\(('.*)\)/)[1]).map((i) => i.split(',').map((j) => j.replace(/^['"](.*)['"]$/, '$1')));
await Promise.all(args.map((arg) => doLoadKz(...arg))).then((result) => {
const caches = result.map((i) => i.response.match(/^callback\d\((.*)\)$/)[1]).map((i) => JSON.parse(i)).map((i) => i.kzUrl).filter((i) => i);
if (caches.length) {
const checkedlist = GM_getValue('checkedlist', []);
checkedlist.push(host);
GM_setValue('checkedlist', checkedlist);
}
if (!caches.length) {
GM_notification({
text: caches.length ? '已有快照' : '不存在快照',
title: document.title,
image: `${window.location.origin}/favicon.ico`,
tag: GM_info.script.name,
timeout: 10 * 1000,
onclick() {
GM_openInTab(caches.length ? url : `https://web.archive.org/save/${window.location.href}`, true);
},
});
}
});
}());
async function doLoadKz(s, typeId, num) {
const preUrl = 'https://2tool.top';
const url = `${preUrl}/kz.php?s=${s}&num=${num}`;
return xhrSync(url);
}
function xhrSync(url, parm = null, opt = {}) {
console.log({ url, parm });
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: parm ? 'POST' : 'GET',
url,
data: parm,
timeout: opt.timeout || 60 * 1000,
responseType: ['arraybuffer', 'blob', 'json'].includes(opt.responseType) ? opt.responseType : null,
headers: opt.headers || {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
onload(res) {
resolve(res);
},
ontimeout(res) {
reject(res);
},
onerror(res) {
reject(res);
},
});
});
}