Task loop tool

Useful programmer tool

  1. // ==UserScript==
  2. // @name Task loop tool
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.2
  5. // @description Useful programmer tool
  6. // @author You
  7. // @match *://*/*
  8. // @icon https://i.ibb.co/pvv9522/Icon.png
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. //(function() {
  13. // 'use strict';
  14. function newElement(type, propertys, parent) {
  15. let id = null;
  16. let className = null;
  17. let type1 = null;
  18. if (type.indexOf("#") !== -1 && type.indexOf(".") !== -1) {
  19. if (type.indexOf("#") < type.indexOf(".")) {
  20. type1 = type.slice(0, type.indexOf("#"));
  21. id = type.slice(type.indexOf("#") + 1, type.indexOf("."));
  22. className = type.slice(type.indexOf(".") + 1);
  23. } else {
  24. type1 = type.slice(0, type.indexOf("."));
  25. className = type.slice(type.indexOf(".") + 1, type.indexOf("#"));
  26. id = type.slice(type.indexOf("#") + 1);
  27. }
  28. } else if (type.indexOf("#") !== -1 && type.indexOf(".") == -1) {
  29. type1 = type.slice(0, type.indexOf("#"));
  30. id = type.slice(type.indexOf("#") + 1);
  31. } else if (type.indexOf("#") == -1 && type.indexOf(".") !== -1) {
  32. type1 = type.slice(0, type.indexOf("."));
  33. className = type.slice(type.indexOf(".") + 1);
  34. };
  35. let result = null;
  36. if (type1 !== null) result = document.createElement(type1);
  37. else result = document.createElement(type);
  38. for (let name in propertys) {
  39. result[name] = propertys[name];
  40. }
  41. if (id !== null) result.id = id;
  42. if (className !== null) result.className = className;
  43. parent.appendChild(result);
  44. return result;
  45. }
  46.  
  47. function cpush(varName, value) {
  48. return value;
  49. }
  50.  
  51. function newWindow(width, height, callback, appname, property) {
  52. function returnIf(condition, iftrue, iffalse) {
  53. if (condition) return iftrue;
  54. else return iffalse;
  55. }
  56. let tab = cpush('tab', document.createElement("div"));
  57. tab.style = "box-sizing: border-box; box-shadow: white 0 0 0 1px; position: absolute; width: 0px; height: 0px; min-height: fit-content;";
  58. if (property.maxSize == undefined) {
  59. let randomX = Math.floor(Math.random() * (window.innerWidth - width));
  60. let randomY = Math.floor(Math.random() * (window.innerHeight - (height + 35)) + 35);
  61. tab.style.left = randomX + "px";
  62. tab.style.top = randomY + "px";
  63. } else {
  64. width = window.innerWidth;
  65. height = window.innerHeight;
  66. };
  67. if (property.transition == false) tab.style.width = width + "px";
  68. if (height == "fit-content") tab.style.height = "fit-content";
  69. if (width == "fit-content") tab.style.width = "fit-content";
  70. document.body.appendChild(tab);
  71.  
  72. let tabBar = document.createElement("div");
  73. tabBar.style = "height: 35px; background-color: black; width: 100%; color: white; font-family: monospace; font-size: 19px; display: flex; justify-content: space-between; align-items: center; padding-left: 40px; box-sizing: border-box;";
  74. tabBar.innerHTML = appname.constructor.name == "String" ? appname : appname[1];
  75. tab.appendChild(tabBar);
  76. let tabBody = document.createElement("div");
  77. tabBody.style = "overflow-y: auto; height: " + (returnIf((property.maxSize == undefined), height, window.innerHeight) - 35) + "px; background-color: rgba(20, 20, 20); width: 100%; padding: 16px; box-sizing: border-box;";
  78. tab.appendChild(tabBody);
  79. let tabClose = document.createElement("div");
  80. tabClose.style = "user-select: none; font-family: 'Source Code Pro', monospace; cursor: pointer; color: white; height: 35px; background-color: black; width: 50px; float: right; align-items: center; display: flex; justify-content: center;";
  81. tabClose.innerText = "x";
  82. tabClose.onclick = function () {
  83. document.body.removeChild(tab);
  84. }
  85. tabClose.onmouseover = function () {
  86. this.style.backgroundColor = "red";
  87. }
  88. tabClose.onmouseout = function () {
  89. this.style.backgroundColor = "black";
  90. }
  91. tabBar.appendChild(tabClose);
  92.  
  93. let isDragging = false;
  94. let offsetX = 0;
  95. let offsetY = 0;
  96.  
  97. tabBar.addEventListener("mousedown", function (event) {
  98. if (property.drag == undefined) {
  99. isDragging = true;
  100. offsetX = event.clientX - tab.offsetLeft;
  101. offsetY = event.clientY - tab.offsetTop;
  102.  
  103. }
  104. });
  105.  
  106. let newPosX;
  107. let newPosY;
  108. let maxX;
  109. let maxY;
  110. document.addEventListener("mousemove", function (event) {
  111. if (isDragging) {
  112. event.preventDefault();
  113. newPosX = event.clientX - offsetX;
  114. newPosY = event.clientY - offsetY;
  115. maxX = (window.innerWidth - tab.offsetWidth) + (width - 50);
  116. maxY = (window.innerHeight - tab.offsetHeight) + (height - 35);
  117. tab.style.left = newPosX + "px";
  118. tab.style.top = newPosY + "px";
  119. }
  120. });
  121.  
  122. document.addEventListener("mouseup", function (event) {
  123. isDragging = false;
  124. });
  125.  
  126.  
  127. let duration = cpush('duration', 500);
  128. let startTime = cpush('startTime', null);
  129. let startWidth = cpush('startWidth', 0);
  130. let startHeight = cpush('startHeight', 0);
  131. function animate(timestamp) {
  132. if (!startTime) {
  133. startTime = timestamp;
  134. startWidth = 0;
  135. startHeight = 0;
  136. }
  137. let progress = timestamp - startTime;
  138. if (progress > duration) {
  139. progress = duration;
  140. }
  141. let ratio = progress / duration;
  142. let newWidth = startWidth + (returnIf((property.maxSize == undefined), width, window.innerWidth) - startWidth) * ratio;
  143. let newHeight = startHeight + (height - startHeight) * ratio;
  144. tab.style.width = newWidth + "px";
  145. tab.style.height = newHeight + "px";
  146. if (progress < duration) {
  147. window.requestAnimationFrame(animate);
  148. }
  149. }
  150. if (property.transition !== false) window.requestAnimationFrame(animate);
  151.  
  152. callback(tab, tabBar, tabBody, tabClose);
  153.  
  154. window.addEventListener("resize", () => {
  155. if (property.maxSize !== undefined) {
  156. tab.style.width = window.innerWidth + "px";
  157. }
  158. })
  159. }
  160.  
  161. window.task = (callback, taskCount) => {
  162. newWindow(400, "fit-content", (tab, tabBar, tabBody, tabClose) => {
  163. tab.style.position = "absolute";
  164. tab.style.zIndex = "100000";
  165. tab.style.left = "10px";
  166. tab.style.top = "10px";
  167. let progress = 0;
  168. let count = 0;
  169. let progressInfos = newElement("p", { "style": "word-wrap: break-word; color: white; font-family: monospace; margin-top: 0px;" }, tabBody);
  170. var parent = document.createElement("div");
  171. parent.style = "border: 1px solid white;";
  172. tabBody.appendChild(parent);
  173. let progressBarDiv = newElement("div", { "style": "width: 100%; height: 25px; border: 1px white solid; box-sizing: border-box;" }, tabBody)
  174. let progressBar = newElement("div", { "style": `width: ${progress}%; height: 100%; background: linear-gradient(to right, #004800, rgb(0, 167, 0));` }, progressBarDiv)
  175. let lastTime = Date.now();
  176. let delay = 1000;
  177. let minDelay = delay;
  178. let maxDelay = 0;
  179. let data = [];
  180.  
  181. let graphic = document.createElement('canvas');
  182. graphic.width = parent.offsetWidth;
  183. graphic.height = 80;
  184.  
  185. let context = graphic.getContext('2d');
  186.  
  187. function refreshGraphic() {
  188. let columnHeight = data[data.length - 1] * 1.5;
  189. let columnWidth = (parent.offsetWidth / taskCount);
  190. context.fillStyle = "rgb(0, 167, 0)";
  191. context.fillRect((data.length - 1) * columnWidth, graphic.height, columnWidth, 0 - columnHeight);
  192. }
  193.  
  194. parent.appendChild(graphic); // ajout du graphique au parent
  195. let backup1 = tabClose.onclick;
  196. tabClose.onclick = () => {
  197. backup1();
  198. count = taskCount - 1;
  199. };
  200. let start = Date.now();
  201. function getTime(ms) {
  202. let hours = Math.floor(ms / 3600000);
  203. let minutes = Math.floor((ms % 3600000) / 60000);
  204. let seconds = Math.floor(((ms % 3600000) % 60000) / 1000);
  205. let milliseconds = Math.floor(((ms % 3600000) % 60000) % 1000);
  206.  
  207. let hourText = hours.toString().padStart(2, '0');
  208. let minuteText = minutes.toString().padStart(2, '0');
  209. let secondText = seconds.toString().padStart(2, '0');
  210. let millisecondText = milliseconds.toString().padStart(3, '0');
  211.  
  212. return hourText + ':' + minuteText + ':' + secondText + ':' + millisecondText;
  213. }
  214. let timeLeftEstimed = 0;
  215. let loop = () => {
  216. setTimeout(() => {
  217. delay = Date.now() - lastTime;
  218. data.push(delay);
  219. refreshGraphic();
  220. if (delay < minDelay) minDelay = delay;
  221. if (delay > maxDelay) maxDelay = delay;
  222. let info = "";
  223. let send = (message) => {
  224. info = message;
  225. };
  226.  
  227. callback();
  228. count++;
  229. progress = (count / taskCount) * 100;
  230. progressBar.style.width = `${progress}%`;
  231. timeLeftEstimed = 0;
  232. data.forEach(localDelay => {
  233. timeLeftEstimed += localDelay;
  234. });
  235. timeLeftEstimed /= data.length;
  236. timeLeftEstimed *= (taskCount - count);
  237. progressInfos.innerHTML = `Tasks running in progress : ${Math.round(progress * 100) / 100}%<br>Delay : ${delay}ms.<br>Max delay : ${maxDelay}, min delay : ${minDelay}.<br>Time passed : ${getTime(Date.now() - start)}.<br><br>Time left estimed : ${getTime(timeLeftEstimed)}.<br><br>Tasks left : ${taskCount - count}.<br><br>${info}`;
  238. if (count == taskCount) {
  239. progressInfos.innerHTML = `Tasks completed !<br><br>` + progressInfos.innerHTML;
  240. } else {
  241. loop();
  242. }
  243. lastTime = Date.now();
  244. }, 20);
  245. };
  246. loop();
  247. }, "TASK", { "transition": false })
  248. };
  249. //})();

QingJ © 2025

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