自动任务

try to take over the world!

  1. // ==UserScript==
  2. // @name 自动任务
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. const waitForLoadTime = 2000;
  12. const TASK_STATUS = {init: 0, success: 1, fail: -1};
  13. (function() {
  14. 'use strict';
  15.  
  16. // Your code here...
  17.  
  18. // 创建iframe任务数据集中存储,解决跨越问题
  19. var noHostCache = (() => {
  20. var frame = document.createElement('iframe');
  21. // frame.style = 'display: none';
  22. frame.id = 'storage-frame';
  23. frame.src = `data:text/html,
  24. <html>
  25. <script>
  26. window.addEventListener('message', function(e) {
  27. if (e.source != window.parent) {
  28. return;
  29. }
  30. console.log(e.data);
  31. var data = JSON.parse(e.data);
  32. var res = false;;
  33. if (data.opr == 'get') {
  34. res = localStorage.getItem(data.key);
  35. } else if (data.opr == 'set') {
  36. localStorage.setItem(data.key, data.value);
  37. res = true;
  38. }
  39. window.parent.postMessage(JSON.stringify(res), '*');
  40. }, false);
  41. </script>
  42. <body></body>
  43. </html>
  44. `;
  45. frame.src = 'https://lin_bo.gitee.io/html/localStorage.html';
  46. // document.body.append(frame);
  47.  
  48. function set(key, value) {
  49. var data = JSON.stringify({opr: 'set', key: key, value: value});
  50. new Promise((resolve, reject) => {
  51. document.getElementById('storage-frame').contentWindow.postMessage(data, '*');
  52. }).then((data) => {
  53. console.log(data)
  54. }).catch((e) => {
  55. console.error(`set错误 ${e}`)
  56. });
  57. }
  58.  
  59. function get(key) {
  60. var data = JSON.stringify({opr: 'get', key: key});
  61. document.getElementById('storage-frame').contentWindow.postMessage('data', '*');
  62. window.addEventListener('message',function(e){
  63. if(e.source!=window.parent) return;
  64. console.log(e.data);
  65. }, false);
  66. }
  67.  
  68. return {set: set, get: get};
  69. })();
  70.  
  71. // 任务核心功能
  72. const _task = (function() {
  73.  
  74. const taskKeyPrefix = 'bob_auto_task';
  75. const taskKey = `${taskKeyPrefix}_${new Date().getFullYear()}-${new Date().getMonth()}-${new Date().getDate()}`;
  76.  
  77. function getCache(key) {
  78. try {
  79. var data = noHostCache.get(key);
  80. if (data) {
  81. return JSON.parse(data);
  82. }
  83. return null;
  84. } catch (e) {
  85. console.error(e);
  86. return null;
  87. }
  88. }
  89.  
  90. function setCache(key, data) {
  91. if (!data) {
  92. return;
  93. }
  94. noHostCache.set(key, JSON.stringify(data));
  95. }
  96.  
  97. // 判断是否匹配到任务
  98. function isUrlMatchTask(curUrl, taskUrl) {
  99. var cUrl = new URL(curUrl);
  100. var cPath = `${cUrl.protocol}//${cUrl.hostname}${cUrl.port == '' ? '' : ':'+cUrl.port}${cUrl.pathname}`;
  101. var tUrl = new URL(taskUrl);
  102. var tPath = `${tUrl.protocol}//${tUrl.hostname}${tUrl.port == '' ? '' : ':'+tUrl.port}${tUrl.pathname}`;
  103. return cPath == tPath;
  104. }
  105.  
  106. // 获取当前网页任务信息
  107. function getCurrentTaskInfo() {
  108. var allTask = getCache(taskKey);
  109. if (!allTask) {
  110. return null;
  111. }
  112. var taskInfo = allTask.filter((item, index) => {
  113. return isUrlMatchTask(window.location.href, item.url);
  114. });
  115. if (taskInfo && taskInfo[0]) {
  116. return taskInfo[0];
  117. }
  118. return null;
  119. }
  120.  
  121. // 注册(不可用)任务
  122. function _reg(name, fun, url) {
  123. if (!(name && fun && url)) {
  124. console.warn('注册(不可用)任务,必须有 name、 fun、 url');
  125. return;
  126. }
  127.  
  128. // 删除非当天任务
  129. for (var i=0; i<window.localStorage.length; i++) {
  130. var key = window.localStorage.key(i);
  131. if (key.indexOf(taskKeyPrefix) == 0 && key !== taskKey) {
  132. console.log(`删除以前任务: ${key}`)
  133. window.localStorage.removeItem(key);
  134. }
  135. }
  136. var task = getCurrentTaskInfo();
  137. if (task) {
  138. console.log(`已注册(不可用)任务: ${JSON.stringify(task)}`);
  139. return;
  140. }
  141. var taskInfo = getCache(taskKey);
  142. // 创建当天任务
  143. if (!taskInfo) {
  144. console.log(`创建当天任务: ${taskKey}`);
  145. taskInfo = [];
  146. }
  147. console.log(`注册(不可用)任务: ${name}】 ${url}`)
  148. // status: -1: 失败; 0: 未执行; 1: 成功
  149. taskInfo.push({name: name, url: url, fun: fun, status: 0})
  150. setCache(taskKey, taskInfo);
  151. }
  152.  
  153. // 执行成功
  154. function success() {
  155. // 获取当前任务
  156. var allTask = getCache(taskKey);
  157. if (!allTask) {
  158. return null;
  159. }
  160. var index = -1;
  161. allTask.forEach((item, i) => {
  162. if (isUrlMatchTask(window.location.href, item.url)) {
  163. index = i;
  164. }
  165. });
  166. if (index >= 0) {
  167. var task = allTask[index];
  168. task.status = TASK_STATUS.success;
  169. var tasks = getCache(taskKey);
  170. allTask[index[0]] = task;
  171. setCache(taskKey, allTask);
  172. console.log('任务执行成功: ' + JSON.stringify(task));
  173. return;
  174. }
  175. console.warn('当前页没有任务');
  176. }
  177.  
  178. // 执行失败
  179. function fail() {
  180. // 获取当前任务
  181. var allTask = getCache(taskKey);
  182. if (!allTask) {
  183. return null;
  184. }
  185. var index = -1;
  186. allTask.forEach((item, i) => {
  187. if (isUrlMatchTask(window.location.href, item.url)) {
  188. index = i;
  189. }
  190. });
  191. if (index >= 0) {
  192. var task = allTask[index];
  193. task.status = TASK_STATUS.fail;
  194. var tasks = getCache(taskKey);
  195. allTask[index[0]] = task;
  196. setCache(taskKey, allTask);
  197. console.log('任务执行失败: ' + JSON.stringify(task));
  198. return;
  199. }
  200. console.warn('当前页没有任务');
  201. }
  202.  
  203. // 执行下一个任务
  204. function doNext() {
  205. var allTask = getCache(taskKey);
  206. allTask.forEach(item => {
  207. if (item.status == 0) {
  208. console.log(`开始执行: ${item.name}`)
  209. } else if (item.status == 1) {
  210. console.log(`跳过已执行: ${item.name}`)
  211. } else if (item.status == 1) {
  212. console.log(`跳过失败: ${item.name}`)
  213. }
  214. });
  215. }
  216.  
  217. // 开始任务
  218. function start() {
  219. // 获取当前任务
  220. var taskInfo = getCurrentTaskInfo();
  221. if (!taskInfo) {
  222. console.log('当前无任务')
  223. return;
  224. }
  225. console.log(`当前任务: ${JSON.stringify(taskInfo)}`);
  226. // 执行任务
  227. window.addEventListener("load", () => {
  228. setTimeout(() => {
  229. eval(`${taskInfo.fun}()`);
  230. }, waitForLoadTime);
  231. });
  232. //
  233. }
  234.  
  235. // 执行任务
  236. function reg(name, fun, url) {
  237. if (isUrlMatchTask(window.location.href, url)) {
  238. window.addEventListener("load", () => {
  239. setTimeout(() => {
  240. console.log(`开始执行: ${name}`)
  241. eval(`${fun}()`);
  242. }, waitForLoadTime);
  243. });
  244. }
  245. return;
  246. }
  247.  
  248. return {reg: reg, doNext: doNext, start: start, success: success, fail: fail};
  249. })();
  250.  
  251.  
  252. // 任务1
  253. _task.reg('测试任务', 'task1', 'file:///C:/Users/71085/Desktop/temp/jd.html');
  254. function task1() {
  255. setTimeout(() => {
  256. console.log('taksk1')
  257. }, 1000);
  258. }
  259.  
  260. // 任务1
  261. _task.reg('京东金融-每日签到', 'task11', 'https://uf.jr.jd.com/activities/sign/v5/index.html?channel=JRAPP');
  262. function task11() {
  263. var signBtn = document.querySelector('.sign-btn');
  264. if (signBtn) {
  265. if (signBtn.innerHTML.indexOf('已连续签到') >= 0) {
  266. console.log('每日签到:已签到');
  267. } else {
  268. signBtn.click();
  269. console.log('每日签到:成功');
  270. // TODO 关闭奖励
  271. }
  272. } else {
  273. console.warn('每日签到:找不到签到按钮');
  274. }
  275. }
  276.  
  277.  
  278. // 注册(不可用)任务2
  279. // _task.reg('京东金融-种草阅读文章', 'task2', 'https://jddx.jd.com/m/jddnew/discovery/0.html');
  280. function task2() {
  281. console.log('开始阅读3篇种草文章');
  282. var jddUrls = new Array();
  283. var targets = document.getElementsByClassName('essay-holder');
  284. // 一个连接含有 'essay-holder' 这样两个标签,从标签中解析出url参数
  285. var p1 = targets[0].getAttribute('clstag'); // jr|keycount|jiandandian_0305|faxianpage_neirong_info_8582799
  286. p1 = p1.substr(p1.lastIndexOf('_')); // 得到 8582799
  287. jddUrls.push(`https://jddx.jd.com/m/jdd/index.html?id=${p1}`);
  288.  
  289. var p2 = targets[2].getAttribute('clstag');
  290. p2 = p2.substr(p2.lastIndexOf('_'));
  291. jddUrls.push(`https://jddx.jd.com/m/jdd/index.html?id=${p2}`);
  292.  
  293. var p3 = targets[4].getAttribute('clstag');
  294. p3 = p3.substr(p3.lastIndexOf('_'));
  295. jddUrls.push(`https://jddx.jd.com/m/jdd/index.html?id=${p3}`);
  296. jddUrls.forEach(url => {
  297. console.log(url)
  298. var aTag = document.createElement('a');
  299. aTag.href = url
  300. aTag.target = '_blank'
  301. // aTag.click();
  302. });
  303. targets[0].click();
  304. }
  305.  
  306. // 注册(不可用)任务3
  307. _task.reg('看广告领京东豆', 'task3', 'https://jdda.jd.com/btyingxiao/advertMoney/html/collar.html?iframeSrc=https%3A%2F%2Fpro.m.jd.com%2Fmall%2Factive%2F3xdpa5DWqPDhqZgf9qX1kkfixyES%2Findex.html%3Ffrom%3Dkgg&adId=09999999&bussource=');
  308. function task3() {
  309. console.log('看广告领京东豆');
  310. var runner = setInterval(() => {
  311. var btn = document.getElementById('idButton');
  312. if (btn.innerHTML === '同一广告不能重复领取!') {
  313. clearInterval(runner);
  314. console.log('看广告领京东豆: 已完成');
  315. return;
  316. }
  317. if (btn && btn.getAttribute('class') === 'button') {
  318. btn.click();
  319. clearInterval(runner);
  320. console.log('查看广告领豆: 完成');
  321. } else {
  322. console.log('查看广告领豆: 未有可领取按钮');
  323. }
  324. }, 1000);
  325.  
  326. }
  327.  
  328. // 注册(不可用)任务4
  329. _task.reg('赚钱签到', 'task4', 'https://jddx.jd.com/m/jddnew/money/index.html?from=jrmd');
  330. function task4() {
  331. console.log('赚钱签到');
  332. var p = document.getElementsByClassName('item-content');
  333. for (var i=0; i<p.length; i++) {
  334. var className = p[i].childNodes[0].getAttribute('class');
  335. if ('item-icon today done' === className) {
  336. console.log('赚钱签到: 已签到');
  337. } else if (className.indexOf(' today ') >= 0) {
  338. p[i].click();
  339. console.log('赚钱签到:签到完成');
  340. // TODO 关闭奖励
  341. }
  342. }
  343. // 领取每日任务
  344. console.log('领取赚钱任务: 开始');
  345. var btns = document.getElementsByClassName('listItem-jingdou item');
  346. var hasTaskBtn = false;
  347. for (var j=0; j<btns.length; j++) {
  348. var btnHtml = btns[j].innerHTML;
  349. // 只领取一个京东豆的任务
  350. if (btnHtml.indexOf('去浏览') > 0 && btnHtml.indexOf('class="num">+1</span>') > 0) {
  351. hasTaskBtn = true;
  352. btns[j].click();
  353. } else if (btnHtml.indexOf('领取任务') > 0 && btnHtml.indexOf('class="num">+1</span>') > 0) {
  354. hasTaskBtn = true;
  355. btns[j].click();
  356. }
  357. }
  358. if (!hasTaskBtn) {
  359. console.log('当前页没有任务领取按钮');
  360. } else {
  361. window.location.reload();
  362. }
  363. console.log('领取赚钱任务: 完成');
  364. }
  365.  
  366. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址