-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtamper-monkey-script.js
131 lines (116 loc) · 3.38 KB
/
tamper-monkey-script.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// ==UserScript==
// @name 自动刷新豆瓣图书缓存
// @description https://github.com/acdzh/douban-book-api/
// @author acdzh
// @connect *
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_deleteValue
// @grant GM_registerMenuCommand
// @include https://book.douban.com/*
// @include https://search.douban.com/book/subject_search*
// @version 1.2.0
// @icon https://img3.doubanio.com/favicon.ico
// @run-at document-end
// @namespace doveboy_js
// ==/UserScript==
// This Userscirpt can't run under Greasemonkey 4.x platform
if (typeof GM_xmlhttpRequest === 'undefined') {
alert('不支持Greasemonkey 4.x, 请换用暴力猴或Tampermonkey');
return;
}
const getJSON = (url, callback) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
headers: {
Accept: 'application/json'
},
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
callback(JSON.parse(response.responseText), url);
} else {
callback(false, url);
}
}
});
};
const postJSON = (url, data, callback) => {
GM_xmlhttpRequest({
method: 'POST',
url: url,
data: JSON.stringify(data),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
callback(JSON.parse(response.responseText), url);
} else {
callback(false, url);
}
}
});
};
const getJSONSync = (url) => new Promise((resolve, reject) => {
getJSON(url, (res) => {
if (res === false) {
reject(res);
} else {
resolve(res);
}
});
});
const postJSONSync = (url, data) => new Promise((resolve, reject) => {
postJSON(url, data, (res) => {
if (res === false) {
reject(res);
} else {
resolve(res);
}
});
});
const postPageData = () => {
if (location.href.match(/douban.com\/subject\/\d+\//)) {
const id = location.href.match(/(\d{7,8})/g);
fetch(location.href).then(r => r.text()).then(html => {
postJSONSync(`${origin}/book`, {
html
}).then(console.log, console.log);
});
} else if (location.href.match(/douban.com\/book\/subject_search/)) {
const searchText = new URLSearchParams(location.search).get('search_text');
if (searchText && searchText !== '') {
postJSONSync(`${origin}/search?text=${searchText}`, {
DATA: unsafeWindow.__DATA__
}).then(console.log, console.log);
}
}
};
if (typeof GM_registerMenuCommand !== 'undefined') {
const changeDomain = () => {
const domain = prompt('api server', GM_getValue('domain', ''));
if (domain != null && domain !== '') {
try {
const origin = new URL(domain).origin;
GM_setValue('domain', origin);
} catch (e) {
alert('解析输入出错');
}
}
};
GM_registerMenuCommand('切换 domain', changeDomain);
GM_registerMenuCommand('发送数据', postPageData);
}
const domain = GM_getValue('domain', '');
if (domain === '') {
alert('请配置 api 服务器地址');
return;
}
const origin = new URL(domain).origin;
window.addEventListener('load', postPageData);