DKK Torn Utilities DEVELOPMENT

Commonly used functions in my Torn scripts.

当前为 2019-11-18 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/392610/750737/DKK%20Torn%20Utilities%20DEVELOPMENT.js

  1. // ==UserScript==
  2. // @name DKK Torn Utilities DEVELOPMENT
  3. // @description Commonly used functions in my Torn scripts.
  4. // @version 1.8.7
  5. // @exclude *
  6. // @namespace https://openuserjs.org/users/DeKleineKobini
  7. // @homepageURL https://www.torn.com/forums.php#/p=threads&t=16110079
  8. // ==/UserScript==
  9.  
  10. /* Script Setup */
  11. addFunctions();
  12.  
  13. function addFunctions() {
  14. // formating for numbers
  15. Number.prototype.format = function(n, x) {
  16. var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
  17. return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
  18. };
  19. }
  20.  
  21. function initScript(options) {
  22. // some easy name conversion
  23. if (options.name) {
  24. if (!options.id) options.id = replaceAll(options.name, " ", "").toLowerCase();
  25. if (!options.abbr) options.abbr = "";
  26. }
  27. if (!options.logging) {
  28. options.logging = "ERROR";
  29. }
  30. // actually init
  31. if (options.api){
  32. if (options.apikey) _setAPI(options.apikey);
  33. requireAPI();
  34. }
  35. if (!window.scripts) window.scripts = { ids: [] };
  36. window.scripts.ids.push(options.id);
  37. window.scripts[options.id] = {
  38. name: options.name,
  39. abbr: options.short,
  40. api: options.api ? true : false
  41. };
  42. dkklog.level = dkklog.levels[options.logging];
  43. dkklog.prefix = "[" + options.abbr + "]";
  44. }
  45.  
  46. /* Messaging */
  47. var dkklog = {
  48. prefix: "[DKK]",
  49. level: 2, // show warnings by default
  50. levels: {
  51. FATAL: 0,
  52. ERROR: 1,
  53. WARN: 2,
  54. INFO: 3,
  55. DEBUG: 4,
  56. TRACE: 5,
  57. ALL: 6,
  58. OFF: 7
  59. },
  60. logging: function (level, message, ...objs) {
  61. if (dkklog.loggingLevel < dkklog.levels[level]) return;
  62.  
  63. let msg = `${dkklog.prefix}[${Object.getOwnPropertyNames(dkklog.levels)[dkklog.level]}] ${message}`;
  64.  
  65. console.log(msg, objs.length);
  66.  
  67. },
  68. fatal: function (message) { dkklog.logging("FATAL", message); },
  69. error: function (message, ...objs) { dkklog.logging("ERROR", message); },
  70. warn: function (message, ...objs) { dkklog.logging("WARN", message); },
  71. info: function (message, ...objs) { dkklog.logging("INFO", message); },
  72. debug: function (message, ...objs) { dkklog.logging("DEBUG", message, objs); },
  73. trace: function (message, ...objs) { dkklog.logging("TRACE", message); },
  74. all: function (message, ...objs) { dkklog.logging("ALL", message); }
  75. }
  76.  
  77. /* Torn API */
  78. var apiKey;
  79.  
  80. const API_LENGTH = 16;
  81. const STORAGE_LOCATION = "dkkutils_apikey";
  82.  
  83. function requireAPI() {
  84. dkklog.debug("Require API")
  85. apiKey = getAPI();
  86. if (!_isValidAPIKey(apiKey)) {
  87. dkklog.debug("Require API - no valid api found")
  88. let response = prompt("Please enter your API key: ");
  89. if (_isValidAPIKey(response)) {
  90. setAPI(response);
  91. } else {
  92. dkklog.debug("Require API - no valid api given")
  93. alert("Given API key was not valid, script might not work.\nRefresh to try again.")
  94. }
  95. }
  96. }
  97.  
  98. function _isValidAPIKey(key) {
  99. if (!key) key = apiKey;
  100.  
  101. if (!key || key === undefined || key == "undefined" || key === null || key == "null" || key === "") return false;
  102. if (key.length != API_LENGTH) return false;
  103.  
  104. return true;
  105. }
  106.  
  107. function sendAPIRequest(part, id, selections, key) {
  108. dkklog.debug(`Sending API request to ${part}/${selections} on id ${id}`);
  109. if (!key) key = apiKey;
  110.  
  111. return new Promise((resolve, reject) => {
  112. GM_xmlhttpRequest({
  113. method: "GET",
  114. url: `https://api.torn.com/${part}/${id}?selections=${selections}&key=${key}`,
  115. onreadystatechange: function(res) {
  116. if (res.readyState > 3 && res.status === 200) {
  117. dkklog.debug("API response received.")
  118. if (!isJsonString(res.responseText)) {
  119. reject("JSON Error", res.responseText);
  120. return;
  121. }
  122.  
  123. var json = JSON.parse(res.responseText);
  124. resolve(json);
  125.  
  126. if (json.error) {
  127. var code = json.error.code;
  128. dkklog.debug("API Error: " + code)
  129. if (code == 2) setAPI(null);
  130. }
  131. }
  132. },
  133. onerror: function(err) {
  134. console.log(err);
  135. reject('XHR error.');
  136. }
  137. })
  138. }).catch(e => {
  139. console.log(e);
  140. });
  141. }
  142.  
  143. function setAPI(key) {
  144. if (!_isValidAPIKey(key)) {
  145. localStorage.removeItem(STORAGE_LOCATION);
  146. dkklog.debug("Removed key from storage.")
  147. } else {
  148. localStorage.setItem(STORAGE_LOCATION, key);
  149. dkklog.debug(`Added key '${key}' to storage. '${localStorage.getItem(STORAGE_LOCATION)}'`)
  150. }
  151.  
  152. apiKey = key;
  153. }
  154.  
  155. function _setAPI(key) {
  156. if (!_isValidAPIKey(key)) localStorage.removeItem(STORAGE_LOCATION);
  157. else localStorage.setItem(STORAGE_LOCATION, key);
  158.  
  159. apiKey = key;
  160. }
  161.  
  162. function getAPI() {
  163. let utilsKey = localStorage.getItem(STORAGE_LOCATION);
  164. if (_isValidAPIKey(utilsKey)) {
  165. dkklog.debug("getAPI - from own storage '" + utilsKey.length + "'");
  166. return utilsKey;
  167. } else {
  168. dkklog.debug("getAPI - from own storage is invalid '" + utilsKey + "'");
  169. }
  170.  
  171. let key = localStorage.getItem("_apiKey") || localStorage.getItem("x_apikey") || localStorage.getItem("jebster.torn") || localStorage.getItem("TornApiKey");
  172.  
  173. if (_isValidAPIKey(key)) {
  174. dkklog.debug("getAPI - from other storage");
  175. setAPI(key);
  176. } else if (localStorage.getItem("_apiKey")) {
  177. dkklog.debug("getAPI - removed 1");
  178. localStorage.removeItem("_apiKey");
  179. } else if (localStorage.getItem("x_apikey")) {
  180. dkklog.debug("getAPI - removed 2");
  181. localStorage.removeItem("x_apikey");
  182. } else if (localStorage.getItem("jebster.torn")) {
  183. dkklog.debug("getAPI - removed 3");
  184. localStorage.removeItem("jebster.torn");
  185. } else if (localStorage.getItem("_apiKey")) {
  186. dkklog.debug("getAPI - removed 4");
  187. localStorage.removeItem("TornApiKey");
  188. }
  189.  
  190. return utilsKey;
  191. }
  192.  
  193. /* Torn General*/
  194. function getScriptUser() {
  195. let body = $("body")
  196. let contentWrapper = $("#mainContainer > .content-wrapper");
  197.  
  198. return {
  199. isJailed: body.hasClass("jail"),
  200. isHospitalized: body.hasClass("hospital"),
  201. isTravelling: contentWrapper.hasClass("travelling")
  202. }
  203. }
  204.  
  205. /* Networking */
  206.  
  207. function xhrIntercept(callback) {
  208. let oldXHROpen = window.XMLHttpRequest.prototype.open;
  209. window.XMLHttpRequest.prototype.open = function() {
  210. this.addEventListener('readystatechange', function() {
  211. if (this.readyState > 3 && this.status == 200) {
  212. var page = this.responseURL.substring(this.responseURL.indexOf("torn.com/") + "torn.com/".length, this.responseURL.indexOf(".php"));
  213.  
  214. var json, uri;
  215. if (isJsonString(this.response)) json = JSON.parse(this.response);
  216. else uri = getUrlParams(this.responseURL);
  217.  
  218. callback(page, json, uri, this);
  219. }
  220. });
  221.  
  222. return oldXHROpen.apply(this, arguments);
  223. }
  224. }
  225.  
  226. function ajax(callback) {
  227. $(document).ajaxComplete((event, xhr, settings) => {
  228. if (xhr.readyState > 3 && xhr.status == 200) {
  229. if (settings.url.indexOf("torn.com/") < 0) settings.url = "torn.com" + (settings.url.startsWith("/") ? "" : "/") + settings.url;
  230. var page = settings.url.substring(settings.url.indexOf("torn.com/") + "torn.com/".length, settings.url.indexOf(".php"));
  231.  
  232. var json, uri;
  233. if (isJsonString(xhr.responseText)) json = JSON.parse(xhr.responseText);
  234. else uri = getUrlParams(settings.url);
  235.  
  236. callback(page, json, uri, xhr, settings);
  237. }
  238. });
  239. }
  240.  
  241. function interceptFetch(url, callback) {
  242. unsafeWindow.fetch = async (input, options) => {
  243. const response = await fetch(input, options)
  244.  
  245. if (response.url.startsWith("https://www.torn.com/" + url)) {
  246. let res = response.clone();
  247.  
  248. Promise.resolve(res.json().then((json) => callback(json, res.url)));
  249. }
  250.  
  251. return response;
  252. }
  253. }
  254.  
  255. /* DOM */
  256. function observeMutationsFull(root, callback, options) {
  257. if (!options) options = {
  258. childList: true
  259. };
  260.  
  261. new MutationObserver(callback).observe(root, options);
  262. }
  263.  
  264. function observeMutations(root, selector, runOnce, callback, options, callbackRemoval) {
  265. var ran = false;
  266. observeMutationsFull(root, function(mutations, me) {
  267. var check = $(selector);
  268.  
  269. if (check.length) {
  270. if (runOnce) me.disconnect();
  271.  
  272. ran = true;
  273. callback(mutations, me);
  274. } else if (ran) {
  275. ran = false;
  276. if (callbackRemoval) callbackRemoval(mutations, me);
  277. }
  278. }, options);
  279. }
  280.  
  281. /* Caching */
  282. function setCache(key, value, time, sub) {
  283. var end = time == -1 ? -1 : Date.now() + time;
  284.  
  285. var obj = sub ? value : {
  286. value: value,
  287. end: Date.now() + time
  288. };
  289.  
  290. GM_setValue(key, JSON.stringify(obj));
  291. }
  292.  
  293. async function getCache(key, subbed) {
  294. let _obj = await GM_getValue(key, subbed ? "{}" : "{\"end\":0}");
  295. let obj = JSON.parse(_obj);
  296.  
  297. var end = obj.end;
  298. if (!end || end == -1 || end > Date.now())
  299. return subbed ? obj : obj.value;
  300.  
  301. return undefined;
  302. }
  303.  
  304. function getSubCache(cache, id) {
  305. if (cache[id]) {
  306. var end = cache[id].end;
  307. if (end == -1 || end > Date.now())
  308. return cache[id].value;
  309. }
  310.  
  311. return undefined;
  312. }
  313.  
  314. /* General Utilities */
  315.  
  316. function isJsonString(str) {
  317. if (!str || str == "") return false;
  318. try {
  319. JSON.parse(str);
  320. } catch (e) {
  321. return false;
  322. }
  323. return true;
  324. }
  325.  
  326. /**
  327. * JavaScript Get URL Parameter (https://www.kevinleary.net/javascript-get-url-parameters/)
  328. *
  329. * @param String prop The specific URL parameter you want to retreive the value for
  330. * @return String|Object If prop is provided a string value is returned, otherwise an object of all properties is returned
  331. */
  332. function getUrlParams(url, prop) {
  333. var params = {};
  334. var search = decodeURIComponent(((url) ? url : window.location.href).slice(window.location.href.indexOf('?') + 1));
  335. var definitions = search.split('&');
  336.  
  337. definitions.forEach(function(val, key) {
  338. var parts = val.split('=', 2);
  339. params[parts[0]] = parts[1];
  340. });
  341.  
  342. return (prop && prop in params) ? params[prop] : params;
  343. }
  344.  
  345. function getSpecialSearch() {
  346. let hash = window.location.hash;
  347.  
  348. hash = hash.replace("#/", "?");
  349. hash = hash.replace("#!", "?");
  350.  
  351. return hash;
  352. }
  353.  
  354. function replaceAll(str, text, replace) {
  355. while (contains(str, text)) {
  356. str = str.replace(text, replace);
  357. }
  358. return str;
  359. }
  360.  
  361. function contains(str, text) {
  362. return str.indexOf(text) >= 0;
  363. }
  364.  
  365. function stripHtml(html) {
  366. var tmp = document.createElement("DIV");
  367. tmp.innerHTML = html;
  368. var stripped = tmp.textContent || tmp.innerText || "";
  369.  
  370. stripped = replaceAll(stripped, "\n", "");
  371. stripped = replaceAll(stripped, "\t", "");
  372. stripped = replaceAll(stripped, " ", " ");
  373.  
  374. return stripped;
  375. }
  376.  
  377. function getNewDay() {
  378. let now = new Date();
  379. let newDay = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate() - 1, 0, 0, 0));
  380.  
  381. if (Date.now() >= newDay.getTime()) newDay.setUTCDate(newDay.getUTCDate() + 1);
  382. if (Date.now() >= newDay.getTime()) newDay.setUTCDate(newDay.getUTCDate() + 1);
  383.  
  384. return newDay;
  385. }
  386.  
  387. function getMillisUntilNewDay() {
  388. return getNewDay().getTime() - Date.now();
  389. }
  390.  
  391. function _isMobile() {
  392. return navigator.userAgent.match(/Android/i) ||
  393. navigator.userAgent.match(/webOS/i) ||
  394. navigator.userAgent.match(/iPhone/i) ||
  395. navigator.userAgent.match(/iPad/i) ||
  396. navigator.userAgent.match(/iPod/i) ||
  397. navigator.userAgent.match(/BlackBerry/i) ||
  398. navigator.userAgent.match(/Windows Phone/i);
  399. }
  400.  
  401. function runOnEvent(funct, event, runBefore) {
  402. if (runBefore) funct();
  403.  
  404. $(window).bind(event, function() {
  405. funct();
  406. });
  407. }
  408.  
  409. function timeSince(timeStamp) {
  410. let now = new Date();
  411. let secondsPast = (now.getTime() - timeStamp.getTime()) / 1000;
  412.  
  413. if (secondsPast < 60) return parseInt(secondsPast) + 's';
  414. else if (secondsPast < 3600) return parseInt(secondsPast / 60) + 'm';
  415. else if (secondsPast <= 86400) return parseInt(secondsPast / 3600) + 'h';
  416. else if (secondsPast > 86400) {
  417. let day = timeStamp.getDate();
  418. let month = timeStamp.toDateString().match(/ [a-zA-Z]*/)[0].replace(" ", "");
  419. let year = timeStamp.getFullYear() == now.getFullYear() ? "" : " " + timeStamp.getFullYear();
  420. return day + " " + month + year;
  421. }
  422. }