DKK Torn Utilities DEVELOPMENT

Commonly used functions in my Torn scripts.

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

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