TSLibrary - Generic

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

目前為 2016-10-29 提交的版本,檢視 最新版本

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

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