// ==UserScript== // @name hostloc论坛自动获取积分 // @namespace hostloc_auto_visit2 // @version 1.0.2 // @description hostloc自动访问其他用户开机,赚积分 // @author zip11 // @grant GM_xmlhttpRequest // @grant GM_notification // @grant GM_log // @connect hostloc.com // @crontab * * once * * // ==/UserScript== // 访问网页 次数 var counting = 0; function autoCheckIn() { // 网页 id const userIds = [44661, 41965, 51285, 14201, 29246, 3609, 47027, 51825, 41856, 51471, 14201]; // 网页 等待 时间 const restSeconds = 3; if (counting >= userIds.length) { GM_log('所有用户访问完成。'); return Promise.resolve('所有访问完成'); } // 获取 网页 id const userId = userIds[counting]; // 网页 网址 合成 const userProfileUrl = `https://hostloc.com/space-uid-${userId}.html?ajaxmenu=1&inajax=1`; return new Promise((resolve, reject) => { // 使用脚本猫的HTTP请求API GM_xmlhttpRequest({ method: "GET", url: userProfileUrl, onload: function(response) { // GM_log(`正在访问第${counting+1}个MJJ`); counting++; // GM_log( "counting:" + counting ); setTimeout(() => { autoCheckIn().then(resolve).catch(reject); }, restSeconds * 1000); /* 在发送每个用户访问请求后,无论该请求成功 (onload) 还是失败 (onerror), 都会等待 restSeconds * 1000 毫秒数 这里的.then(resolve).catch(reject)是用来处理这个递归异步操作的Promise链: resolve是被new Promise接受的函数,当你调用resolve时,表示Promise成功完成了它的异步操作。 reject是被new Promise接受的函数,当你调用reject时,表示Promise在执行它的异步操作中遇到了错误。 */ }, onerror: function(err) { GM_log("访问出错:", err); reject("访问出错"); } }); }); } // 异步 延时 秒 function sleep1(time) { time*=1000 return new Promise(resolve => { setTimeout(() => { resolve(); }, time); }); } // 随机数 function sj_num() { //Math.random() 产生 0-1 // floor 取整数 向下 let sjs = Math.floor(Math.random() * (15-5) ); sjs = sjs + 5 // 可均衡获取 5 到 15 的随机整数。 return sjs; } // 开始 ~~~~~~~~~~~ return new Promise(async (resolve, reject) => { try { let rad_num = sj_num(); // 延时程序 await sleep1(rad_num); autoCheckIn(); // 开始执行自动签到任务 resolve("hostloc-sign-ok"); // 执行成功 } catch (error) { //GM_log(error); reject("hostloc-sign-ok: " + error); } });