stjslab

a javascript lab

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/460309/1151565/stjslab.js

  1. // ==UserScript==
  2. // @name stjslab
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @license Apache License 2.0
  6. // @author 捈荼
  7. // ==/UserScript==
  8.  
  9. class stjslab {
  10. static getCookie(name) {
  11. let nameEQ = name + "=";
  12. let ca = document.cookie.split(';');
  13. for (let i = 0; i < ca.length; i++) {
  14. let c = ca[i];
  15. while (c.charAt(0) == ' ')
  16. c = c.substring(1, c.length);
  17. if (c.indexOf(nameEQ) == 0)
  18. return c.substring(nameEQ.length, c.length);
  19. }
  20. return null;
  21. }
  22.  
  23. static setCookie(name, value, days) {
  24. let expires = "";
  25. if (days) {
  26. let date = new Date();
  27. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  28. expires = "; expires=" + date.toUTCString();
  29. }
  30. document.cookie = name + "=" + (value || "") + expires + "; path=/";
  31. }
  32.  
  33. static deleteCookie(name) {
  34. document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
  35. }
  36.  
  37. static clearAllCookie() {
  38. return document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));
  39. }
  40.  
  41. static format(str) {
  42. let cnt = 1;
  43. let realArgs = Array.from(arguments).slice(1);
  44. return str.match(/{(\d+)}/g) == null && str.match(/{}/g) != null ?
  45. str.replace(/{}/g, (match) => {
  46. return typeof arguments[cnt] != 'undefined' ? arguments[cnt++] : match;
  47. }) :
  48. str.replace(/{(\d+)}/g, (match, number) => {
  49. return typeof realArgs[number] != 'undefined' ? realArgs[number] : match;
  50. });
  51. }
  52.  
  53. static get(query, base = document) {
  54. return base.querySelector(query);
  55. }
  56.  
  57. static getall(query, base = document) {
  58. return [...base.querySelectorAll(query)];
  59. }
  60.  
  61. static getid(id, base = document) {
  62. return base.getElementById(id);
  63. }
  64.  
  65. static getbyClass(classname, base = document) {
  66. return base.getElementsByClassName(classname);
  67. }
  68.  
  69. static getbyTag(tag, base = document) {
  70. return base.getElementsByTagName(tag);
  71. }
  72.  
  73. static getbyName(name, base = document) {
  74. return base.getElementsByName(name);
  75. }
  76.  
  77. static getUrlParam(url) {
  78. return (new URL(location.href)).searchParams.get(url);
  79. }
  80.  
  81. constructor() {
  82. throw this;
  83. }
  84.  
  85. static EventMng = class {
  86. #handlers = {};
  87.  
  88. on(name, func) {
  89. if (!(func instanceof Function))
  90. throw "Param must be func!";
  91. if (!(name in this.#handlers)) {
  92. this.#handlers[name] = [];
  93. }
  94. this.#handlers[name].push(func);
  95. }
  96.  
  97. off(name, func) {
  98. if (!(func instanceof Function))
  99. throw "Param must be func!";
  100. if (name in this.#handlers) {
  101. for (let i = 0; i < this.#handlers[name].length; i++) {
  102. if (this.#handlers[name][i] === func) {
  103. this.#handlers[name].splice(i, 1);
  104. i--;
  105. }
  106. }
  107. }
  108. }
  109.  
  110. clean(name) {
  111. if (name in this.#handlers)
  112. this.#handlers[name] = [];
  113. }
  114.  
  115. emit(name, ...args) {
  116. if (name in this.#handlers) {
  117. for (let func of this.#handlers[name]) {
  118. try {
  119. func(...args);
  120. } catch (e) {
  121. console.error('ERROR:', e);
  122. }
  123. }
  124. }
  125. }
  126. }
  127.  
  128. static uuid() {
  129. var d = new Date().getTime();
  130. if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
  131. d += performance.now();
  132. }
  133. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  134. var r = (d + Math.random() * 16) % 16 | 0;
  135. d = Math.floor(d / 16);
  136. return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  137. });
  138. }
  139.  
  140. static guid() {
  141. function s4() {
  142. return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  143. }
  144. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  145. }
  146.  
  147. static sha1(msg) {
  148. function rotate_left(n, s) {
  149. var t4 = (n << s) | (n >>> (32 - s));
  150. return t4;
  151. };
  152. function lsb_hex(val) {
  153. var str = "";
  154. var i;
  155. var vh;
  156. var vl;
  157. for (i = 0; i <= 6; i += 2) {
  158. vh = (val >>> (i * 4 + 4)) & 0x0f;
  159. vl = (val >>> (i * 4)) & 0x0f;
  160. str += vh.toString(16) + vl.toString(16);
  161. }
  162. return str;
  163. };
  164. function cvt_hex(val) {
  165. var str = "";
  166. var i;
  167. var v;
  168. for (i = 7; i >= 0; i--) {
  169. v = (val >>> (i * 4)) & 0x0f;
  170. str += v.toString(16);
  171. }
  172. return str;
  173. };
  174. function Utf8Encode(string) {
  175. string = string.replace(/\r\n/g, "\n");
  176. var utftext = "";
  177. for (var n = 0; n < string.length; n++) {
  178. var c = string.charCodeAt(n);
  179. if (c < 128) {
  180. utftext += String.fromCharCode(c);
  181. }
  182. else if ((c > 127) && (c < 2048)) {
  183. utftext += String.fromCharCode((c >> 6) | 192);
  184. utftext += String.fromCharCode((c & 63) | 128);
  185. }
  186. else {
  187. utftext += String.fromCharCode((c >> 12) | 224);
  188. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  189. utftext += String.fromCharCode((c & 63) | 128);
  190. }
  191. }
  192. return utftext;
  193. };
  194. var blockstart;
  195. var i, j;
  196. var W = new Array(80);
  197. var H0 = 0x67452301;
  198. var H1 = 0xEFCDAB89;
  199. var H2 = 0x98BADCFE;
  200. var H3 = 0x10325476;
  201. var H4 = 0xC3D2E1F0;
  202. var A, B, C, D, E;
  203. var temp;
  204. msg = Utf8Encode(msg);
  205. var msg_len = msg.length;
  206. var word_array = new Array();
  207. for (i = 0; i < msg_len - 3; i += 4) {
  208. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  209. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  210. word_array.push(j);
  211. }
  212. switch (msg_len % 4) {
  213. case 0:
  214. i = 0x080000000;
  215. break;
  216. case 1:
  217. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  218. break;
  219. case 2:
  220. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  221. break;
  222. case 3:
  223. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  224. break;
  225. }
  226. word_array.push(i);
  227. while ((word_array.length % 16) != 14) word_array.push(0);
  228. word_array.push(msg_len >>> 29);
  229. word_array.push((msg_len << 3) & 0x0ffffffff);
  230. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  231. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  232. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  233. A = H0;
  234. B = H1;
  235. C = H2;
  236. D = H3;
  237. E = H4;
  238. for (i = 0; i <= 19; i++) {
  239. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  240. E = D;
  241. D = C;
  242. C = rotate_left(B, 30);
  243. B = A;
  244. A = temp;
  245. }
  246. for (i = 20; i <= 39; i++) {
  247. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  248. E = D;
  249. D = C;
  250. C = rotate_left(B, 30);
  251. B = A;
  252. A = temp;
  253. }
  254. for (i = 40; i <= 59; i++) {
  255. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  256. E = D;
  257. D = C;
  258. C = rotate_left(B, 30);
  259. B = A;
  260. A = temp;
  261. }
  262. for (i = 60; i <= 79; i++) {
  263. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  264. E = D;
  265. D = C;
  266. C = rotate_left(B, 30);
  267. B = A;
  268. A = temp;
  269. }
  270. H0 = (H0 + A) & 0x0ffffffff;
  271. H1 = (H1 + B) & 0x0ffffffff;
  272. H2 = (H2 + C) & 0x0ffffffff;
  273. H3 = (H3 + D) & 0x0ffffffff;
  274. H4 = (H4 + E) & 0x0ffffffff;
  275. }
  276. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  277. return temp.toLowerCase();
  278. }
  279.  
  280.  
  281. static JSLAB_VERSION = '1.0.0';
  282.  
  283. static __UUID = "90822dad-7367-4dd6-8193-1acc041c008d";
  284. }