Greasy Fork 支持 简体中文。

Torn: Scripts library

Library of functions used in my Torn scripts.

目前為 2024-09-25 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/510149/1454015/Torn%3A%20Scripts%20library.js

  1. // ==UserScript==
  2. // @name Torn: Scripts library
  3. // @namespace lugburz.lib
  4. // @version 0.1.5
  5. // @description Library of functions used in my Torn scripts.
  6. // @author Lugburz
  7. // @exclude *
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. function ajax(callback) {
  12. $(document).ajaxComplete((event, xhr, settings) => {
  13. if (xhr.readyState > 3 && xhr.status == 200) {
  14. let url = settings.url;
  15. if (url.indexOf("torn.com/") < 0) url = "torn.com" + (url.startsWith("/") ? "" : "/") + url;
  16. const page = url.substring(url.indexOf("torn.com/") + "torn.com/".length, url.indexOf(".php"));
  17.  
  18. callback(page, xhr, settings);
  19. }
  20. });
  21. }
  22.  
  23. function pad(num, size) {
  24. return ('000000000' + num).substr(-size);
  25. }
  26.  
  27. function formatTime(date) {
  28. return pad(date.getUTCHours(), 2) + ':' + pad(date.getUTCMinutes(), 2) + ':' + pad(date.getUTCSeconds(), 2);
  29. }
  30.  
  31. function formatTimeMsec(msec, alwaysShowHours = false) {
  32. const hours = Math.floor((msec % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  33. const minutes = Math.floor((msec % (1000 * 60 * 60)) / (1000 * 60));
  34. const seconds = Math.floor((msec % (1000 * 60)) / 1000);
  35. const mseconds = Math.floor(msec % 1000);
  36.  
  37. return (alwaysShowHours ? pad(hours, 2) + ":" : (hours > 0 ? hours + ":" : '')) + (hours > 0 || minutes > 0 ? pad(minutes, 2) + ":" : '') + pad(seconds, 2) + "." + pad(mseconds, 3);
  38. }
  39.  
  40. function formatTimeSecWithLetters(msec) {
  41. const hours = Math.floor((msec % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  42. const minutes = Math.floor((msec % (1000 * 60 * 60)) / (1000 * 60));
  43. const seconds = Math.floor((msec % (1000 * 60)) / 1000);
  44.  
  45. return (hours > 0 ? hours + "h " : '') + (hours > 0 || minutes > 0 ? minutes + "min " : '') + seconds + "s";
  46. }
  47.  
  48. function decode64(input) {
  49. var output = '';
  50. var chr1, chr2, chr3 = '';
  51. var enc1, enc2, enc3, enc4 = '';
  52. var i = 0;
  53. var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  54. var base64test = /[^A-Za-z0-9\+\/\=]/g;
  55. if (base64test.exec(input)) {
  56. console.log('There were invalid base64 characters in the input text.\n' +
  57. 'Valid base64 characters are A-Z, a-z, 0-9, \'+\', \'/\',and \'=\'\n' +
  58. 'Expect errors in decoding.');
  59. }
  60. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
  61. do {
  62. enc1 = keyStr.indexOf(input.charAt(i++));
  63. enc2 = keyStr.indexOf(input.charAt(i++));
  64. enc3 = keyStr.indexOf(input.charAt(i++));
  65. enc4 = keyStr.indexOf(input.charAt(i++));
  66. chr1 = (enc1 << 2) | (enc2 >> 4);
  67. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  68. chr3 = ((enc3 & 3) << 6) | enc4;
  69. output = output + String.fromCharCode(chr1);
  70. if (enc3 != 64) {
  71. output = output + String.fromCharCode(chr2);
  72. }
  73. if (enc4 != 64) {
  74. output = output + String.fromCharCode(chr3);
  75. }
  76. chr1 = chr2 = chr3 = '';
  77. enc1 = enc2 = enc3 = enc4 = '';
  78. } while (i < input.length);
  79. return unescape(output);
  80. }