后端放到网上了,现在部署只需要在油猴里粘贴如下代码。
// ==UserScript==
// @name LUOGU Discuss Better
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 自动填充验证码
// @author ln001
// @match https://www.luogu.com.cn/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
const captcha = 'https://www.luogu.com.cn/lg4/captcha';
const input = "//*[@id=\"app\"]/div[3]/main/div/div/div[2]/div[4]/div[3]/div/div/input";
const button = "//*[@id=\"app\"]/div[3]/main/div/div/div[2]/div[4]/div[3]/button";
function imageToBase64(url = captcha) {
// 转换为Base64
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network error.');
}
return response.blob();
})
.then(blob => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onloadend = () => {
resolve(fileReader.result);
};
fileReader.onerror = reject;
fileReader.readAsDataURL(blob);
});
});
}
function fetchUrl(url) {
// 请求ocr
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
if (response.status >= 200 && response.status < 300) {
resolve(response.responseText);
} else {
reject(`Error: ${response.status}`);
}
},
onerror: function(error) {
reject(`Request failed: ${error}`);
}
});
});
}
function fillInputAndClickButton(text, inputXPath = input, buttonXPath = button) {
// 填充验证码
// 查找输入框
const inputElement = document.evaluate(inputXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// 查找按钮
const buttonElement = document.evaluate(buttonXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (inputElement) {
inputElement.value = text;
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
inputElement.dispatchEvent(event);
console.log('验证码填写成功:', text);
} else {
console.error('验证码无处填写');
}
if (buttonElement) {
buttonElement.click();
console.log('点击发表按钮');
} else {
console.error('按钮未找到');
}
}
function main(){
console.log('into main()');
const NewButton = document.evaluate(button + "[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if(NewButton)
{ return;}
const targetButton = document.evaluate(button, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (targetButton) {
// 创建新按钮
const newButton = document.createElement('button');
newButton.textContent = 'plus';
// newButton.style.marginLeft = '10px'; // 添加一些左边距
// 设置样式,不会套用原样式,只能模仿了
newButton.style.backgroundColor = 'rgb(52, 152, 219)';
newButton.style.color = 'white';
newButton.style.border = 'none';
newButton.style.borderRadius = '3px'; // 添加圆角
newButton.style.padding = '10px 15px';
newButton.style.cursor = 'pointer'; // 鼠标显示为手型
newButton.style['font-size'] = '.875em'; // 字体大小
targetButton.parentNode.insertBefore(newButton, targetButton.nextSibling); // 插入按钮
// 点击事件
newButton.addEventListener('click', function() {
imageToBase64()
.then(base64 => {
base64 = base64.slice(23); // 删除前缀
console.log(base64);
// const ocr_url = "http://localhost:64496/?base64=" + base64;
// const ocr_url = "http://localhost:8000/?base64=" + base64;
const ocr_url = "http://8.140.166.24:44963/?base64=" + base64;
console.log(ocr_url);
fetchUrl(ocr_url)
.then(data => {
console.log('ocr返回:', data);
fillInputAndClickButton(data);
});
});
});
} else {
console.error('未找到发表按钮');
}
}
setInterval(() => main(), 1500);