-
Notifications
You must be signed in to change notification settings - Fork 177
/
boism.js
67 lines (64 loc) · 2.16 KB
/
boism.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
//仅在个人动态页有效
function getAnswers(){
return new Promise(function(resolve, reject){
var oldHeight = 0;
var h = setInterval(function(){
if(document.querySelectorAll('.zu-button-more').length) {
oldHeight = document.body.scrollHeight;
document.body.scrollTop = document.body.scrollHeight;
} else {
clearInterval(h);
console.log(document.querySelectorAll('.feed-item'))
//resolve(answers)
}
},2000)
})
}
//仅在问题页有效
function getToken(){
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open("GET", location.href, true);
request.send(null);
request.onload = e => {
var tmp = document.createElement('div');
tmp.innerHTML = request.responseText.match(/data\-state\=\"([^\"]*)/)[1];
resolve(JSON.parse(tmp.innerText).token.carCompose)
}
})
}
function addTopic(token, name){
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open("POST", "https://www.zhihu.com/api/v4/topics", true);
request.setRequestHeader("accept", "application/json, text/plain, */*");
request.setRequestHeader("content-type", "application/json");
request.setRequestHeader("authorization", "Bearer " + token);
request.send(JSON.stringify({name: name}));
request.onload = e => {
resolve(request.responseText)
}
})
}
function sleep(t) {
return new Promise((resolve, reject) => {
setTimeout(resolve, t);
})
}
function random(str) {
var chars = str.split('');
for(var i = 0; i < chars.length; i++) {
chars[i] += ['\uFEFF','\u200C','\u200D'][Math.random()*3 | 0]
chars[i] += ['\uFEFF','\u200C','\u200D'][Math.random()*3 | 0]
chars[i] += ['\uFEFF','\u200C','\u200D'][Math.random()*3 | 0]
}
return chars.join('');
}
async function run() {
var token = await getToken();
while(true) {
await sleep(3000);
await addTopic(token, random("勃学"));
}
}
run();