tiku
// ==UserScript==
// @name tiku
// @author tfsn20
// @version 0.0.2
// ==/UserScript==
(async function () {
const tools = {
sleep: t => new Promise(res => setTimeout(res, t)),// 箭头函数体只有一句,可以省略return
net: {
change: (str, withCookie) => {
str = str.trim(); //去除文本首位\s
let method = str.match(/(.*?)\s/)[1];
//避免空白符影响
let url = str.match(/\S*\s*(.*)\n/)[1].split("").reverse().join("").match(/^\S*\s*(.*)/)[1].split("").reverse().join("");
str = str.replace(/.*?\n/, ''); //去除第一行
let data = /\n\s*\n\s*(.*)$/m.test(str) ? str.slice(str.match(/\n\s*\n\s*(.*)$/m).index).trim() : ''; //获取请求体,没有则返回'', 用slice截取防止请求体含有\n时出错
str = str.replace(new RegExp(data), '').trim(); //去除data
str = str.replace(/^(\s*?)(\S.*)/gm, `$2`); //去除每行行首的\s
str = str.replace(/\sContent-Length:.*/im, ''); //去除Content-Length所在行,不用/^\sContent-Length:./im, 这样做会多一个空白行避免一些问题
str = withCookie ? str : str.replace(/\s*cookie:.*/im, ''); //去除cookie所在行,不用/^\s*cookie.*/im, 这样做会多一个空白行
let headers = {}, h = str.match(/(\S*?):\s*(\S.*)/mg);
// GM_log(h)
h.forEach(e => {
let t = e.match(/(\S*?):\s*(.*)/);
headers[t[1]] = t[2].replace(/\s*$/, '');//去除行尾\s,避免一下总没错吧
});
//去除浏览器默认携带的请求头
if (!/origin:/i.test(str)) headers.origin = '';//脚本猫默认有一个拓展origin,这里去掉
if (!/dnt/i.test(str)) headers.dnt = '';
if (!/referer/i.test(str)) headers.referer = '';
//if (!/accept/i.test(str)) headers.accept='';
if (!/user-agent/i.test(str)) headers['user-agent'] = '';
if (!/sec-ch-ua/i.test(str)) headers['sec-ch-ua'] = '';
if (!/sec-ch-ua-mobile/i.test(str)) headers['sec-ch-ua-mobile'] = '';
if (!/sec-ch-ua-platform/i.test(str)) headers['sec-ch-ua-platform'] = '';
if (!/sec-fetch-dest/i.test(str)) headers['sec-fetch-dest'] = '';
if (!/sec-fetch-mode/i.test(str)) headers['sec-fetch-mode'] = '';
if (!/sec-fetch-site/i.test(str)) headers['sec-fetch-site'] = '';
//if (!/accept-language/i.test(str)) headers['accept-language']='';
if (!/accept-encoding/i.test(str)) headers['accept-encoding'] = '';
return { url, method, data, headers }
},
send: (str, anonymous = false, withCookie = false) => {
let t = tools.net.change(str, withCookie);
return new Promise((resolve, reject) => {
t.anonymous = anonymous;
t.onload = (xhr) => {
resolve(xhr)
};
t.onerror = _ => { GM_log('net error') };
GM_xmlhttpRequest(t)
})
}
},
string: {
removePunctuation: str => {
return str.replace(/[^\u4E00-\u9FA5a-zA-Z0-9]/g, '')
},
toUnicode: str => {
var ret = "";
var ustr = "";
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
var code16 = code.toString(16);
if (code < 0xf) {
ustr = "\\u" + "000" + code16;
} else if (code < 0xff) {
ustr = "\\u" + "00" + code16;
} else if (code < 0xfff) {
ustr = "\\u" + "0" + code16;
} else {
ustr = "\\u" + code16;
}
ret += ustr;
// ret += "\\u" + str.charCodeAt(i).toString(16);
}
return ret
},
raw: str => [...str.raw][0]
},
event: {
mousedown: element => {
element.dispatchEvent(new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
// view: window,
detail: 1,
// screenX: screenX, screenY: screenY,
// clientX: clientX, clientY: clientY,
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
button: 0,
relatedTarget: null,
}))
},
mouseup: element => {
element.dispatchEvent(new MouseEvent('mouseup', {
bubbles: true,
cancelable: true,
// view: window,
detail: 1,
// screenX: screenX, screenY: screenY,
// clientX: clientX, clientY: clientY,
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
button: 0,
relatedTarget: null,
}))
}
},
element: {
check: async (element, ...event) => {
for (let i = 0, len = event.length; i < len; i++) {
tools.event[event[i]](element);
await tools.sleep(200)
}
}
}
};
//返回形如['A','D']
async function getOption(searchKey) {
let resultList = JSON.parse((await tools.net.send(`
GET https://tiku.baidu.com/buguakewap/proxy/bgk/bigque/getwapquesearch?query=${searchKey.replace(/\n/g, '')}&na_uncheck=1&wk_cs_app=1&_t=1697440514827 HTTP/1.1
Host: tiku.baidu.com
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"
Pragma: no-cache
DNT: 1
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0
Accept: application/json, text/plain, */*
Cache-Control: no-cache, no-store
sec-ch-ua-platform: "Windows"
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ja;q=0.5,de-DE;q=0.4,de;q=0.3
`)).response).data.list;
if (resultList.length == 0) {
console.log('题库中没有此题目,将为您打开百度新标签页');
window.open(`https://m.baidu.com/s?word=${searchKey}`);
} else {
for (let i = 0, len = resultList.length; i < len; i++) {
let answerOptions = [];
let result = (await tools.net.send(`
GET https://tiku.baidu.com/buguakewap/browse/index?qid=${resultList[i].qid_str}&limitSwitch=1&limitTimes=3 HTTP/1.1
Host: tiku.baidu.com
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ja;q=0.5,de-DE;q=0.4,de;q=0.3
`)).response;
let node = new DOMParser().parseFromString(result, 'text/html');
eval(node.querySelector("body>script").innerText);
let questionObj = JSON.parse(`${window.pageData.answerList[0].bdjson}`)
console.log(questionObj);
if (questionObj.que_answer[0].length) {
answerOptions = questionObj.que_answer[0][0].c[0].c.match(/[A-Z]/g);
} else {
answerOptions = questionObj.que_answer[0].c[0].c.match(/[A-Z]/g);
}
return answerOptions
}
}
}
const win = window.unsafeWindow || document.defaultView || window;
win.getOption = getOption;
// console.log(await getOption(`生物学实验室内的煤气、酒精、汽油等是易燃易爆燃料,在一定的条件下均能引起燃烧和爆炸,必须妥善安置,正确使用。()
// A对 B错`))
})();