TSLibrary - Generic

A resource JS library file providing common useful functions to be used by other scripts

当前为 2016-10-22 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name TSLibrary - Generic
  3. // @namespace TimidScript
  4. // @version 1.0.18.2
  5. // @description A resource JS library file providing common useful functions to be used by other scripts
  6. // @author TimidScript
  7. // @homepageURL https://github.com/TimidScript
  8. // @copyright © 2014+ TimidScript, Some Rights Reserved.
  9. // @license https://github.com/TimidScript/UserScripts/blob/master/license.txt
  10. // @exclude *
  11. // ==/UserScript==
  12.  
  13.  
  14. /* License + Copyright Notice
  15. ********************************************************************************************
  16. License can be found at: https://github.com/TimidScript/UserScripts/blob/master/license.txt
  17. Below is a copy of the license the may not be up-to-date.
  18.  
  19. Copyright © TimidScript, Some Rights Reserved.
  20.  
  21. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  22. following conditions are met:
  23.  
  24. 1) GPL-3 License is met that does not conflict with the rest of the license (http://www.gnu.org/licenses/gpl-3.0.en.html)
  25. 2) This notice must be included
  26. 3) Due credits and link to original author's homepage (included in this notice).
  27. 4) Notify the original author of redistribution
  28. 5) Clear clarification of the License and Notice to the end user
  29. 6) Do not upload on OpenUserJS.org or any other site that infringes on this license
  30.  
  31. TimidScript's Homepages: GitHub: https://github.com/TimidScript
  32. GreasyFork: https://greasyfork.org/users/1455
  33. */
  34. /* Information
  35. ********************************************************************************************
  36. Version History
  37. ----------------------------------------------
  38. 1.0.18 (2016-05-27)
  39. - License altered
  40. 1.0.17 (2016-04-03)
  41. - Changed license to GPL-3
  42. 1.0.16 (2015-07-20)
  43. - addScript and addStyle return created nodes
  44. - Change location of addScript to head instead of body incase of dynamic pages that alter the body
  45. 1.0.15 (2015-06-18)
  46. - updateDocumentURL renamed to updateURL
  47. 1.0.14 (2015-06-18)
  48. - Using \b for regex in class functions
  49. - Better handling of spaces in the class functions
  50. 1.0.13 (2015-01-16)
  51. - updateDocumentURL added
  52. 1.0.12 (2014-12-12)
  53. - @exclude added
  54. 1.0.11 (2014-10-12)
  55. - innerHTML instead of textContent for scripts and CSS.
  56. 1.0.10 (2014-09-17)
  57. - Optimised - When adding or removing a class it now first checks
  58. it exists thus avoiding to make any extra changes.
  59. - Ability to add or remove more than one class. Separator is space.
  60. - hasClass also handles more than one class. Returns true only
  61. if all classes are present.
  62. - addClass, removeClass return true if class is added or removed
  63. 1.0.9 (2014-09-07)
  64. - Fixed bug in addScript and moved the script to the head instead of body.
  65. - CSS appended to textContent instead of innerHTML
  66. 1.0.8 (2014-09-05)
  67. - Improved removeClass
  68. - Added absolutePosition(element)
  69. 1.0.7 (2014-09-02)
  70. - Functions added: addClass removeClass hasClass
  71. - Removed makeStruct as it is useless
  72. 1.0.6 (2014-08-29)
  73. - Changed the NTFS chars http://unicode-search.net
  74. 1.0.5 (2014-08-24)
  75. - TSL part no longer commented out
  76. 1.0.4
  77. - Added new functions createElement, createElementHTML function
  78. - Partial support for non-main document
  79. 1.0.3
  80. - Added NTFS illegal character replacer
  81. - escape regular expression function
  82. 1.0.2
  83. - Added scroll bar thickness
  84. 1.0.1
  85. - Initial Release
  86. **********************************************************************************************/
  87.  
  88.  
  89. var TimidScriptLibrary =
  90. {
  91. //http://unicode-search.net
  92. ALTNTFSChars: [[">", ">"],
  93. ["<", "<"],
  94. [":", ":"],
  95. ['"', """],
  96. ["/", "/"],
  97. ["\\", "\"],
  98. ["?", "?"],
  99. ["*", "*"]],
  100.  
  101. removeNode: function (node, doc)
  102. {
  103. if (!doc) doc = document;
  104. if (typeof node == "string") node = doc.getElementById(node);
  105. if (node && node.parentElement) node.parentElement.removeChild(node);
  106. },
  107.  
  108. addStyle: function (id, CSS, doc)
  109. {
  110. if (!doc) doc = document;
  111. var style = doc.createElement("style");
  112. style.type = "text/css";
  113. if (id)
  114. {
  115. style.id = id;
  116. TimidScriptLibrary.removeNode(id, doc);
  117. }
  118.  
  119. style.innerHTML = CSS;
  120. doc.head.appendChild(style);
  121. return style;
  122. },
  123.  
  124. addScript: function (id, text, doc)
  125. {
  126. if (!doc) doc = document;
  127. var script = doc.createElement("script");
  128. if (id)
  129. {
  130. script.id = id;
  131. TimidScriptLibrary.removeNode(id, doc);
  132. }
  133.  
  134. script.innerHTML = text;
  135. doc.head.appendChild(script);
  136. return script;
  137. },
  138.  
  139. createElement: function (tag, attributes, doc)
  140. {
  141. if (!doc) doc = document;
  142. var el = doc.createElement(tag);
  143.  
  144. for (var x in attributes) el.setAttribute(x, attributes[x]);
  145. return el;
  146. },
  147.  
  148. createElementHTML: function (html, doc)
  149. {
  150. if (!doc) doc = document;
  151. var el = doc.createElement("e");
  152.  
  153. el.innerHTML = html;
  154. return el.firstElementChild;
  155. },
  156.  
  157. paddingLeft: function (str, chr, length)
  158. {
  159. while (str.length < length)
  160. str = chr + str;
  161.  
  162. return str;
  163. },
  164.  
  165. paddingRight: function (str, chr, length)
  166. {
  167. while (str.length < length)
  168. str = str + chr;
  169.  
  170. return str;
  171. },
  172.  
  173. isMouseEventInClientArea: function (event, element)
  174. {
  175. var rect = element.getBoundingClientRect();
  176. var minX = rect.left + element.clientLeft;
  177.  
  178. var x = event.clientX;
  179. if (x < minX || x >= minX + element.clientWidth) return false;
  180. var minY = rect.top + element.clientTop;
  181. var y = event.clientY;
  182. if (y < minY || y >= minY + element.clientHeight) return false;
  183. return true;
  184. },
  185.  
  186.  
  187. getScrollBarThickness: function ()
  188. {
  189. var outer = document.createElement("div");
  190. outer.style.visibility = "hidden";
  191. outer.style.width = "100px";
  192. document.body.appendChild(outer);
  193.  
  194. var widthNoScroll = outer.offsetWidth;
  195. // force scrollbars
  196. outer.style.overflow = "scroll";
  197.  
  198. // add innerdiv
  199. var inner = document.createElement("div");
  200. inner.style.width = "100%";
  201. outer.appendChild(inner);
  202.  
  203. var widthWithScroll = inner.offsetWidth;
  204.  
  205. // remove divs
  206. outer.parentNode.removeChild(outer);
  207.  
  208. return widthNoScroll - widthWithScroll;
  209. },
  210.  
  211.  
  212. escapeRegExp: function (str)
  213. {
  214. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  215. return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  216. },
  217.  
  218. replaceNTFSIllegals: function (str)
  219. {
  220. for (var i = 0; i < TimidScriptLibrary.ALTNTFSChars.length; i++)
  221. {
  222. var rx = new RegExp(TimidScriptLibrary.escapeRegExp(TimidScriptLibrary.ALTNTFSChars[i][0]), "gi");
  223. str = str.replace(rx, TimidScriptLibrary.ALTNTFSChars[i][1]);
  224. }
  225.  
  226. return str;
  227. },
  228.  
  229. addClass: function (node, names)
  230. {
  231. var altered = false;
  232. var newclass = node.className;
  233. var classes = names.replace(/\s+/g, " ").trim().split(" ");
  234.  
  235. for (var i = 0; i < classes.length; i++)
  236. {
  237. //var re = new RegExp("(^|\\s+)" + classes[i] + "(\\s+|$)");
  238. var re = new RegExp("\\b" + classes[i] + "\\b");
  239. if (!newclass.match(re))
  240. {
  241. newclass += " " + classes[i];
  242. altered = true;
  243. }
  244. }
  245.  
  246. if (altered) node.className = newclass.replace(/\s+/g, " ").trim();
  247. return altered;
  248. },
  249.  
  250. removeClass: function (node, names)
  251. {
  252. var altered = false;
  253. var newclass = node.className;
  254. var classes = names.replace(/(\s)\s+/g, " ").trim().split(" ");
  255.  
  256. for (var i = 0; i < classes.length; i++)
  257. {
  258. //var re = new RegExp("(^|\\s+)" + classes[i] + "(\\s+|$)");
  259. var re = new RegExp("\\b" + classes[i] + "\\b");
  260. if (newclass.match(re))
  261. {
  262. newclass = newclass.replace(re, " ");
  263. altered = true;
  264. }
  265. }
  266.  
  267. if (altered) node.className = newclass.replace(/\s+/g, " ").trim();
  268. return altered;
  269. },
  270.  
  271. hasClass: function (node, names)
  272. {
  273. var classes = names.replace(/(\s)\s+/g, " ").trim().split(" ");
  274. for (var i = 0; i < classes.length; i++)
  275. {
  276. //var re = new RegExp("(^|\\s+)" + classes[i] + "(\\s+|$)");
  277. var re = new RegExp("\\b" + classes[i] + "\\b");
  278. if (!node.className.match(re)) return false;
  279. }
  280.  
  281. return true;
  282. },
  283.  
  284. getAbsolutePosition: function (element)
  285. {
  286. var x = 0;
  287. var y = 0;
  288.  
  289. while (element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop))
  290. {
  291. x += element.offsetLeft;
  292. y += element.offsetTop;
  293. element = element.offsetParent;
  294. }
  295. return { top: y, left: x };
  296. },
  297.  
  298. updateURL: function (url)
  299. {
  300. window.history.pushState(null, "", url);
  301. }
  302. }
  303.  
  304. //#region TimidScript Library Functions
  305. /*
  306. Copy and paste the code underneath into your script for quick reference
  307. and auto-complete feature if available.
  308. *********************************************************************************/
  309.  
  310. var TSL = new Object();
  311.  
  312. //Remove node from document. Accepts id or node object
  313. TSL.removeNode = function (node, doc) { TimidScriptLibrary.removeNode(node, doc); };
  314.  
  315. // Creates document element. Default doc value is the document.
  316. TSL.createElement = function (tag, attributes, doc) { return TimidScriptLibrary.createElement(tag, attributes, doc) };
  317.  
  318. // Creates document element using html code. Default doc value is the document.
  319. TSL.createElementHTML = function (html, doc) { return TimidScriptLibrary.createElementHTML(html, doc) };
  320.  
  321. //Add CSS styles to document header. Document can be left empty.
  322. TSL.addStyle = function (id, CSS, doc) { return TimidScriptLibrary.addStyle(id, CSS, doc); };
  323. //Add JS script to document header. Document can be left empty.
  324. TSL.addScript = function (id, script, doc) { return TimidScriptLibrary.addScript(id, script, doc); };
  325.  
  326. // Checks if mouse event is within an elements client area
  327. TSL.isMouseEventInClientArea = function (event, element) { return TimidScriptLibrary.isMouseEventInClientArea(event, element); };
  328. // Gets the position of the element within the document
  329. TSL.getAbsolutePosition = function (element) { return TimidScriptLibrary.getAbsolutePosition(element); };
  330.  
  331. //Returns the thickness of the scrollbar
  332. TSL.getScrollBarThickness = function () { return TimidScriptLibrary.getScrollBarThickness(); };
  333.  
  334. //Array containing NTFS illegal characters alternatives
  335. TSL.ALTNTFSChars = TimidScriptLibrary.ALTNTFSChars;
  336. TSL.replaceNTFSIllegals = function (str) { return TimidScriptLibrary.replaceNTFSIllegals(str); };
  337.  
  338. TSL.escapeRegExp = function (str) { return TimidScriptLibrary.escapeRegExp(str); };
  339.  
  340. //String Padding
  341. String.prototype.lPad = function (chr, length) { return TimidScriptLibrary.paddingLeft(this, chr[0], length); };
  342. String.prototype.rPad = function (chr, length) { return TimidScriptLibrary.paddingRight(this, chr[0], length); };
  343.  
  344. //Node className functions. All three functions can handle multiple classes separated by spaces
  345. TSL.addClass = function (node, names) { return TimidScriptLibrary.addClass(node, names); };
  346. TSL.removeClass = function (node, names) { return TimidScriptLibrary.removeClass(node, names); };
  347. TSL.hasClass = function (node, names) { return TimidScriptLibrary.hasClass(node, names); };
  348.  
  349. //Allows you to change the document URL, which is reflected in the URL bar.
  350. TSL.updateURL = function (url) { TimidScriptLibrary.updateURL(url); };
  351. /*
  352. *********************************************************************************/
  353. //#endregion