Idleontoolbox Produce Check

Check and notify on https://idleontoolbox.com/dashboard with Telegram support

当前为 2024-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Idleontoolbox Produce Check
  3. // @namespace http://tampermonkey.net/
  4. // @version 7.0
  5. // @description Check and notify on https://idleontoolbox.com/dashboard with Telegram support
  6. // @author Tiande
  7. // @match https://idleontoolbox.com/*
  8. // @grant GM_notification
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @license MIT
  12. // ==/UserScript==
  13. (function() {
  14. 'use strict';
  15.  
  16. var interval = 5 * 60 * 1000; // check status every 5m
  17. //var interval = 5000 // USE FOR TEST
  18. var refreshInterval = 30.1 * 60 * 1000; // auto refresh to Dashboard every 10m
  19. // Telegram Bot API Token
  20. var tgbotToken = 'YOUR BOT TOKEN';
  21. // Telegram user ID to send the message to
  22. var tguserId = 'YOUR USER ID';
  23.  
  24. // CHECK 1: lable text under player's name
  25. var whichFull = /(is full|\s[1-5]\sminute)/i; // (is full|being full|can equip sth|missing a trap|maxed) etc.
  26. // CHECK 2: skill to notify
  27. var skill = /(Refinery|Birthday)/i; // (Refinery|Cooking|Arena|Printer Go Brr) etc.
  28. // CHECK 3: bookcount to check
  29. var bookcount = 3;
  30. // CHECK 4: AFKTime Check
  31. var AFKHour = 9;
  32. var AFKMin = 30;
  33. // CHECK 5: banner notify. Must use img src
  34. var bannerRegex = /(biubiubiu|blablabla)/i;
  35. //exclude 3d printer circle check
  36. var excludeKeyword = 'printer';
  37.  
  38. var notificationPermission = GM_getValue('notificationPermission');
  39. var isFunctionEnabled = true;
  40. var intervalId; // Store interval ID for pausing and resuming
  41.  
  42. // tg send
  43. function tgsendMessage(message) {
  44. var tgurl = 'https://api.telegram.org/bot' + tgbotToken + '/sendMessage';
  45. var tgparams = 'chat_id=' + tguserId + '&text=' + encodeURIComponent(message);
  46.  
  47. var tgxhr = new XMLHttpRequest();
  48. tgxhr.open('POST', tgurl, true);
  49. tgxhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  50. tgxhr.onreadystatechange = function() {
  51. if (tgxhr.readyState == 4 && tgxhr.status == 200) {
  52. console.log('Message sent successfully!');
  53. }
  54. };
  55. tgxhr.onerror = function(error) {
  56. console.error('Error sending message:', error);
  57. };
  58. tgxhr.send(tgparams);
  59. }
  60.  
  61. // autorefresh time
  62. var refreshId;
  63. // refreshPage
  64. function refreshPage() {
  65. if (isFunctionEnabled) {
  66. location.href = 'https://idleontoolbox.com/dashboard'; // 刷新页面到 dashboard
  67. } else {
  68. // 如果不是,则等待下一个刷新时间间隔
  69. refreshId = setInterval(refreshPage, refreshInterval);
  70. }
  71. }
  72.  
  73. function toggleRefresh() {
  74. if (isFunctionEnabled) {
  75. intervalId = setInterval(startInterval, interval); // 启动定时器
  76. refreshId = setInterval(refreshPage, refreshInterval);
  77. } else {
  78. clearInterval(intervalId); // 清除定时器
  79. clearInterval(refreshId);
  80. }
  81. }
  82. toggleRefresh(); // 在脚本启动时调用一次,以确保定时器已启动
  83. // Preload audio
  84. var audio = new Audio();
  85. audio.src = 'https://github.com/Tiande/IdelonCheck/raw/main/iphonewake.wav';
  86.  
  87. // Add CSS styles
  88. var style = document.createElement('style');
  89. style.innerHTML = `#toggleButtonContainer {
  90. position: fixed;
  91. top: 50 % ;
  92. left: calc(50 % +100px);
  93. transform: translate( - 50 % , -50 % );
  94. display: flex;
  95. align - items: center;
  96. padding: 5px;
  97. background: green;
  98. /* Green color for default enabled state */
  99. color: white;
  100. border - radius: 5px;
  101. font - family: Arial,
  102. sans - serif;
  103. font - size: 14px;
  104. box - shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
  105. z - index: 99999;
  106. user - select: none;
  107. cursor: move;
  108. }#toggleButtonContainer.off {
  109. background: red;
  110. /* Red color for disabled state */
  111. }#toggleButton {
  112. padding: 5px 10px;
  113. border: none;
  114. border - radius: 5px;
  115. background: transparent;
  116. cursor: pointer;
  117. outline: none;
  118. }.drag - handle {
  119. cursor: move;
  120. }#produceCheck {
  121. background - color: black;
  122. color: white;
  123. padding: 5px;
  124. border - radius: 5px;
  125. margin - right: 5px;
  126. }`;
  127. document.head.appendChild(style);
  128.  
  129. createToggleButton(); // Create toggle button on script execution
  130. startInterval(); // Start interval on script execution
  131. function createToggleButton() {
  132. var toggleButtonContainer = document.createElement('div');
  133. toggleButtonContainer.id = 'toggleButtonContainer';
  134. toggleButtonContainer.classList.add('drag-handle');
  135. var produceCheck = document.createElement('span');
  136. produceCheck.id = 'produceCheck';
  137. produceCheck.textContent = 'Produce Check: ';
  138. toggleButtonContainer.appendChild(produceCheck);
  139. var toggleButton = document.createElement('button');
  140. toggleButton.id = 'toggleButton';
  141. toggleButton.addEventListener('click', toggleFunction);
  142. toggleButtonContainer.appendChild(toggleButton);
  143. document.body.appendChild(toggleButtonContainer);
  144. updateButtonStyle();
  145. makeDraggable(toggleButtonContainer);
  146. }
  147.  
  148. function updateButtonStyle() {
  149. var toggleButtonContainer = document.getElementById('toggleButtonContainer');
  150. toggleButtonContainer.classList.toggle('off', !isFunctionEnabled);
  151. var toggleButton = document.getElementById('toggleButton');
  152. toggleButton.innerHTML = isFunctionEnabled ? 'ON': 'OFF';
  153. }
  154.  
  155. function toggleFunction() {
  156. isFunctionEnabled = !isFunctionEnabled;
  157. updateButtonStyle();
  158. toggleRefresh(); // 根据isFunctionEnabled的值启用或暂停定时器
  159. }
  160.  
  161. function startInterval() {
  162. // sth is full
  163. var matchedText = '';
  164. var prepareelements = document.querySelectorAll('img[aria-label]');
  165. var elements = Array.from(prepareelements).filter(function(element) {
  166. var ariaLabel = element.getAttribute('aria-label');
  167. var matched = ariaLabel.match(whichFull);
  168. if (matched !== null) {
  169. // 如果匹配到了,则将匹配项拼接到 matchedText 中
  170. matchedText += matched[0];
  171. // 如果还有下一个匹配项,则添加 '&'
  172. if (matched.index !== whichFull.lastIndex - matched[0].length) {
  173. matchedText += '&';
  174. }
  175. }
  176. // 返回原有的条件
  177. return whichFull.test(ariaLabel);
  178. });
  179.  
  180. if (elements.length > 0) {
  181. var messages = [];
  182. elements.forEach(function(element) {
  183. var parentDiv = element.closest('.MuiCardContent-root');
  184. if (parentDiv) {
  185. var roleNameElement = parentDiv.querySelector('.MuiTypography-root.MuiTypography-body1.css-9l3uo3');
  186. if (roleNameElement) {
  187. var roleName = roleNameElement.textContent.trim();
  188. var matchedText = ''; // 重新初始化 matchedText
  189. var matched = element.getAttribute('aria-label').match(whichFull);
  190. if (matched !== null) {
  191. matchedText = matched[0]; // 获取匹配文本
  192. }
  193. messages.push(roleName + ' : ' + matchedText);
  194. }
  195. }
  196. });
  197.  
  198. if (messages.length > 0) {
  199. var message = messages.join(', ');
  200. showNotification(message);
  201. audio.play();
  202. if (tguserId !== 'your user ID') {
  203. tgsendMessage(message);
  204. }
  205. }
  206. }
  207.  
  208. // skill is ready
  209. var matchedText = '';
  210. var prepareelements = document.querySelectorAll('img[aria-label]');
  211. var elements = Array.from(prepareelements).filter(function(element) {
  212. var ariaLabel = element.getAttribute('aria-label');
  213. var matched = ariaLabel.match(skill);
  214. if (matched !== null) {
  215. // 如果匹配到了,则将匹配项拼接到 matchedText 中
  216. matchedText += matched[0];
  217. // 如果还有下一个匹配项,则添加 '&'
  218. if (matched.index !== skill.lastIndex - matched[0].length) {
  219. matchedText += '&';
  220. }
  221. }
  222. // 返回原有的条件
  223. return skill.test(ariaLabel);
  224. });
  225.  
  226. if (elements.length > 0) {
  227. var messages = [];
  228. elements.forEach(function(element) {
  229. var parentDiv = element.closest('.MuiCardContent-root');
  230. if (parentDiv) {
  231. var roleNameElement = parentDiv.querySelector('.MuiTypography-root.MuiTypography-body1.css-9l3uo3');
  232. if (roleNameElement) {
  233. var roleName = roleNameElement.textContent.trim();
  234. var matchedText = ''; // 重新初始化 matchedText
  235. var matched = element.getAttribute('aria-label').match(skill);
  236. if (matched !== null) {
  237. matchedText = matched[0]; // 获取匹配文本
  238. }
  239. messages.push(roleName + ' : ' + matchedText);
  240. }
  241. }
  242. });
  243.  
  244. if (messages.length > 0) {
  245. var message = messages.join(', ');
  246. showNotification(message);
  247. audio.play();
  248. if (tguserId !== 'your user ID') {
  249. tgsendMessage(message);
  250. }
  251. }
  252. }
  253.  
  254. // 追踪另一组元素
  255. var timeElements = document.querySelectorAll('.MuiTypography-root.MuiTypography-inherit');
  256. var trackedContents = [];
  257.  
  258. timeElements.forEach(function(timeElement) {
  259. var color = getComputedStyle(timeElement).color;
  260. var match = color.match(/^rgb\((\d+), (\d+), (\d+)\)$/);
  261. if (match && match[1] === '249' && match[2] === '29' && match[3] === '29') {
  262. // color 属性值为 rgb(249, 29, 29),视为捕获
  263. var parentElement = timeElement.closest('.MuiStack-root');
  264. if (parentElement && parentElement.getAttribute('aria-label')) {
  265. var ariaLabel = parentElement.getAttribute('aria-label');
  266. var splitIndex = ariaLabel.indexOf(':');
  267. if (splitIndex !== -1) {
  268. var content = ariaLabel.substring(0, splitIndex).trim(); // 获取':'前的内容,并去除空格
  269. if (!new RegExp(excludeKeyword).test(content)) { // 排除包含自定义关键词的项目
  270. trackedContents.push(content);
  271. }
  272. }
  273. }
  274. }
  275. });
  276.  
  277. if (trackedContents.length > 0) {
  278. var message = trackedContents.join(', '); // 将数组内容连接成字符串,以逗号分隔
  279. showNotification(message);
  280. audio.play();
  281. if (tguserId !== 'your user ID') {
  282. tgsendMessage(message + ' is finished.');
  283. }
  284. }
  285.  
  286. // Book count
  287. var bookCountElements = document.querySelectorAll('.MuiCardContent-root h4');
  288. // 遍历每个 <h4> 元素
  289. bookCountElements.forEach(function(element) {
  290. // 获取文本内容
  291. var text = element.textContent.trim();
  292. // 提取数字部分
  293. var count = parseInt(text.match(/\d+/)[0]);
  294. // 如果数字大于等于2,则发送通知
  295. if (count >= bookcount) {
  296. // 发送通知
  297. var message = 'The book count has exceeded the limit!';
  298. showNotification(message);
  299. audio.play();
  300. if (tguserId !== 'your user ID') {
  301. tgsendMessage(message);
  302. }
  303. }
  304. });
  305.  
  306. // 追踪另一组元素 AFKTime
  307. var timeElements = document.querySelectorAll('.MuiTypography-root.MuiTypography-caption.css-deomsi');
  308. timeElements.forEach(function(timeElement) {
  309. var parentElement = timeElement.closest('.MuiStack-root');
  310. if (parentElement) {
  311. var resetTextElement = parentElement.querySelector('.MuiTypography-body1');
  312. if (resetTextElement) {
  313. var resetText = resetTextElement.textContent.trim();
  314. // 排除含有 Daily Reset 和 Weekly Reset 的模块内容
  315. if (resetText !== 'Daily Reset' && resetText !== 'Weekly Reset') {
  316. var timeText = timeElement.textContent.trim();
  317. var match = timeText.match(/^(\d+)h:(\d+)m:(\d+)s$|^(\d+)d:(\d+)h:(\d+)m$/);
  318.  
  319. if (match) {
  320. var days = match[4] ? parseInt(match[4], 10) : 0;
  321. var hours = match[1] ? parseInt(match[1], 10) : parseInt(match[2], 10);
  322. var minutes = match[2] ? parseInt(match[2], 10) : parseInt(match[5], 10);
  323. var seconds = match[3] ? parseInt(match[3], 10) : parseInt(match[6], 10);
  324.  
  325. // 转换成统一的格式
  326. hours += days * 24;
  327.  
  328. if (hours >= AFKHour && minutes >= AFKMin) {
  329. showNotification(timeText);
  330. audio.play();
  331. if (tguserId !== 'your user ID') {
  332. tgsendMessage(timeText + ' is finished.');
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. });
  340.  
  341. //check top banner
  342. var images = document.querySelectorAll('div.css-1eybjch > div.css-79elbk > img'); // 获取页面上所有的img元素
  343. var matchedImages = Array.from(images).filter(function(image) {
  344. return bannerRegex.test(image.src); // 测试每个图片的src属性是否匹配正则表达式
  345. });
  346.  
  347. // 遍历匹配到正则的img元素并执行相应的操作
  348. if (matchedImages.length > 0) {
  349. var messages = [];
  350. matchedImages.forEach(function(image) {
  351. var src = image.src;
  352. var matchedText = src.match(bannerRegex)[0]; // 获取匹配文本
  353. messages.push(matchedText); // 将匹配的文本收集起来
  354. });
  355.  
  356. // 将所有匹配的结果合并成一条消息并显示出来
  357. var message = messages.join(' ');
  358. showNotification(message);
  359. audio.play();
  360. if (tguserId !== 'your user ID') {
  361. tgsendMessage('Banner check: ' + message);
  362. }
  363. }
  364.  
  365. }
  366.  
  367. function showNotification(message) {
  368. if (notificationPermission === 'granted') {
  369. GM_notification({
  370. text: message,
  371. title: 'Idleontoolbox Notification',
  372. timeout: 5000,
  373. onclick: function() {
  374. window.focus();
  375. }
  376. });
  377. } else {
  378. window.Notification.requestPermission().then(function(permission) {
  379. if (permission === 'granted') {
  380. GM_notification({
  381. text: message,
  382. title: 'Idleontoolbox Notification',
  383. timeout: 5000,
  384. onclick: function() {
  385. window.focus();
  386. }
  387. });
  388. }
  389. });
  390. }
  391. }
  392.  
  393. function makeDraggable(element) {
  394. let pos1 = 0,
  395. pos2 = 0,
  396. pos3 = 0,
  397. pos4 = 0;
  398.  
  399. element.onmousedown = dragMouseDown;
  400.  
  401. function dragMouseDown(e) {
  402. e = e || window.event;
  403. e.preventDefault();
  404. // get the mouse cursor position at startup:
  405. pos3 = e.clientX;
  406. pos4 = e.clientY;
  407. document.onmouseup = closeDragElement;
  408. // call a function whenever the cursor moves:
  409. document.onmousemove = elementDrag;
  410. }
  411.  
  412. function elementDrag(e) {
  413. e = e || window.event;
  414. e.preventDefault();
  415. // calculate the new cursor position:
  416. pos1 = pos3 - e.clientX;
  417. pos2 = pos4 - e.clientY;
  418. pos3 = e.clientX;
  419. pos4 = e.clientY;
  420. // set the element's new position:
  421. element.style.top = (element.offsetTop - pos2) + "px";
  422. element.style.left = (element.offsetLeft - pos1) + "px";
  423. }
  424.  
  425. function closeDragElement() {
  426. /* stop moving when mouse button is released:*/
  427. document.onmouseup = null;
  428. document.onmousemove = null;
  429. }
  430. }
  431.  
  432. })();