暑假时需要对某网站实现抢单功能,所以通过浏览器插件Tampermonkey实现抢单功能脚本。其主要思路是:
- 首先定义了一个变量timeHandler和一个状态变量functionalityEnabled,用于记录抢单功能是否开启;
- 通过GM_addStyle函数添加了一个悬浮窗口,其中包含一个复选框,用于控制抢单功能的开启和关闭;
- 定义了toggleFunctionality函数,用于切换抢单功能的状态,并通过GM_setValue函数将状态保存在浏览器的缓存中;
- 定义了sendRequest函数,用于向服务器发送请求并进行抢单;
- 在页面加载完成后,根据缓存中的状态来决定是否自动开启抢单功能,并为复选框绑定事件,用于控制抢单功能的开启和关闭。
// ==UserScript==
// @name 抢单
// @namespace https://rensr.site
// @version 0.2
// @description try to take over the world!
// @author 任尚仁
// @match https://kuajing.pinduoduo.com/main/order-manage
// @icon https://bpic.588ku.com/element_pic/19/04/04/669edc93405cbe43292be19cb6224d75.jpg
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
var timeHandler;
var functionalityEnabled = GM_getValue('functionalityEnabled', false);
GM_addStyle(`
#floating-window {
position: fixed;
top: 15%;
right: 20px;
padding: 10px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 10px;
z-index: 9999;
}
`);
var floatingWindow = document.createElement('div');
floatingWindow.setAttribute('id', 'floating-window');
floatingWindow.innerHTML = `
<label>
<input type="checkbox" id="toggle-switch"> 启用抢单功能
</label>
`;
document.body.appendChild(floatingWindow);
function toggleFunctionality() {
functionalityEnabled = !functionalityEnabled;
GM_setValue('functionalityEnabled', functionalityEnabled);
if (functionalityEnabled) {
timeHandler = setInterval(sendRequest, 500);
} else {
clearInterval(timeHandler);
}
}
document.getElementById('toggle-switch').addEventListener('change', toggleFunctionality);
function sendRequest() {
document.querySelectorAll(".BTN_textPrimary_5-72-0").forEach(div => {
if (div.innerHTML.indexOf("加入发货台") > -1 && div.className.indexOf("BTN_disabled_") == -1) {
div.click();
setTimeout(() => {
document.querySelectorAll(".PP_popoverContent_5-72-0 button").forEach(btn => {
btn.click()
});
}, 200);
}
});
}
if (functionalityEnabled) {
document.getElementById('toggle-switch').checked = true;
timeHandler = setInterval(sendRequest, 500);
}
})();


