js-domExtend

原生js增强插件,将部分原生dom对象方法模仿jQuery进行二次封装,便于使用

目前为 2022-04-26 提交的版本。查看 最新版本

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

  1. // ==UserScript==
  2. // @name js-domExtend
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 原生js增强插件,将部分原生dom对象方法模仿jQuery进行二次封装,便于使用
  6. // @author tutu辣么可爱
  7. // @dayr 2022.4.27 GMT+0800 (中国标准时间)
  8. // @license MIT License
  9. // ==/UserScript==
  10. /*
  11. *author:tutu辣么可爱
  12. *day:2022.4.27 GMT+0800 (中国标准时间)
  13. *description:原生js增强插件,将部分原生dom对象方法模仿jQuery进行二次封装,便于使用
  14. *
  15. *function $ele(dom, dom2)
  16. *parameter0 dom:需要创建的dom对象的html字符串或者选择器字符串
  17. *parameter1 dom2:选择器模式时生效,选择器的父节点,默认值document
  18. *note:本质是对createElement、querySelectorAll的二次封装,使用体验类似jQuery的$()方法
  19. *
  20. *function $eleFn(dom, dom2)
  21. *parameter0 dom:选择器字符串
  22. *parameter1 dom2:选择器的父节点,默认值document
  23. *note:返回一组方法,用于对需要选择的dom对象进行监控,具体用法见$eleFn.listen和$eleFn.ready部分
  24. *
  25. *function $eleFn.listen(callback, interval)
  26. *parameter0 callback:对dom对象监控时循环执行的回调方法
  27. *parameter1 interval:dom对象的监控时间间隔,默认值500
  28. *note:返回定时器ID。本质是调用setInterval不断执行$ele方法检测dom对象是否存在
  29. *
  30. *function $eleFn.ready(callback, interval)
  31. *parameter0 callback:dom对象检测到存在时的回调方法
  32. *parameter1 timeout:dom对象检测存在的超时时长
  33. *note:返回定时器ID。本质是调用$eleFn.listen检测对象是否存在,setTimeout控制超时时长
  34. *
  35. *function DOM.attr(key, val)
  36. *parameter0 key:dom对象属性名
  37. *parameter1 val:dom对象属性值
  38. *note:没有val则返回对应属性名的值;有val则进行属性设置,返回dom对象本身。本质是对getAttribute、setAttribute、removeAttribute的二次封装,使用体验类似jQuery的$.attr方法
  39. *
  40. *function DOM.css(key, val)
  41. *parameter0 key:dom对象css样式属性名
  42. *parameter1 val:dom对象css样式属性值
  43. *note:没有val则返回对应属性名的值;有val则进行属性设置,返回dom对象本身。本质是对getComputedStyle、DOM.style.setProperty的二次封装,使用体验类似jQuery的$.css方法
  44. *
  45. *function DOM.hide()
  46. *note:调用DOM.css隐藏dom对象,返回dom对象本身。本质是将dom对象样式display设置为none,使用体验类似jQuery的$.hide方法。DOM.hide支持nodeList对象
  47. *
  48. *function DOM.show()
  49. *note:调用DOM.css显示dom对象,返回dom对象本身。本质是将dom对象样式display还原为原属性值,使用体验类似jQuery的$.show方法。DOM.show支持nodeList对象
  50. *
  51. *function DOM.insert(dom, position)
  52. *parameter0 dom:选择器字符串、dom对象或多个dom对象组成的Array数组
  53. *parameter1 position:插入位置,默认值end
  54. *note:返回值为dom节点本身。position为start时,插入到DOM父节点开头;position为end时,插入到DOM父节点结尾;position为before时,插入到dom节点前面;position为after时,插入到dom节点后面。本质是对insertBefore、append的二次封装
  55. *
  56. *function DOM.replace(dom)
  57. *parameter dom:选择器字符串、dom对象或多个dom对象组成的Array数组
  58. *note:返回值为新dom节点。本质是调用DOM.insert后将原dom节点remove
  59. *
  60. */
  61. function $ele(dom, dom2 = document) {
  62. switch (dom.slice(0, 1)) {
  63. case "<":
  64. dom2 = document.createElement("div");
  65. dom2.innerHTML = dom;
  66. dom2 = dom2.childNodes;
  67. break;
  68. default:
  69. dom2 = dom2.querySelectorAll(dom);
  70. break;
  71. }
  72. return dom2.length > 1 ? dom2 : dom2[0]
  73. }
  74. function $eleFn(dom, dom2 = document) {
  75. return {
  76. data: [dom, dom2],
  77. listen: function(callback, interval = 500) {
  78. var that = this;
  79. return setInterval(() => {
  80. if ($ele(that.data[0], that.data[1])) {
  81. callback();
  82. }
  83. }, interval)
  84. },
  85. ready: function(callback, timeout = 3000) {
  86. var timer = this.listen(() => {
  87. clearInterval(timer);
  88. callback();
  89. })
  90. setTimeout(() => {
  91. clearInterval(timer);
  92. }, timeout)
  93. return timer
  94. }
  95. }
  96. }
  97.  
  98. HTMLElement.prototype.attr = function(key, val) {
  99. if (typeof key === "string") {
  100. if (/string|boolean/.test(typeof val)) {
  101. if (!val && val !== false) {
  102. this.removeAttribute(key);
  103. } else {
  104. this.setAttribute(key, val);
  105. }
  106. return this;
  107. } else {
  108. return this.getAttribute(key);
  109. }
  110. }
  111. }
  112. HTMLElement.prototype.css = function(key, val) {
  113. if (typeof key === "string") {
  114. if (/string|boolean/.test(typeof val)) {
  115. this.style.setProperty(key, val);
  116. } else if (!val) {
  117. return getComputedStyle(this)[key];
  118. }
  119. } else {
  120. for (let i in key) {
  121. this.style.setProperty(i, key[i]);
  122. }
  123. }
  124. if (this.style === "") {
  125. this.attr("style", "")
  126. }
  127. return this;
  128. }
  129. HTMLElement.prototype.hide = function() {
  130. this.setAttribute("display_backup", this.css("display"));
  131. this.css("display", "none")
  132. return this;
  133. }
  134. HTMLElement.prototype.show = function() {
  135. var backup = this.attr("display_backup") ? this.attr("display_backup") : "";
  136. this.css("display", backup !== "none" ? backup : "");
  137. return this;
  138. }
  139. HTMLElement.prototype.insert = function(dom, position = "end") {
  140. dom = typeof dom === "string" ? $ele(dom) : (Array.isArray(dom) ? dom : [dom]);
  141. switch (position) {
  142. case "start":
  143. Array.from(dom).reverse().forEach((e, i) => {
  144. this.insertBefore(e, this.firstChild);
  145. })
  146. break;
  147. case "end":
  148. dom.forEach((e, i) => {
  149. this.append(e);
  150. })
  151. break;
  152. case "before":
  153. Array.from(dom).reverse().forEach((e, i) => {
  154. this.parentElement.insertBefore(e, this);
  155. })
  156. break;
  157. case "after":
  158. if (this.parentElement.lastChild === this) {
  159. dom.forEach((e, i) => {
  160. this.append(e);
  161. })
  162. } else {
  163. let next = this.nextSilbing;
  164. Array.from(dom).reverse().forEach((e, i) => {
  165. this.parentElement.insertBefore(e, next);
  166. })
  167. }
  168. break;
  169. }
  170. return this;
  171. }
  172. HTMLElement.prototype.replace = function(dom) {
  173. dom = this.insert(dom, "before");
  174. this.remove();
  175. return dom;
  176. }
  177. NodeList.prototype.hide = function() {
  178. this.forEach((e, i) => {
  179. e.hide();
  180. })
  181. }
  182. NodeList.prototype.show = function() {
  183. this.forEach((e, i) => {
  184. e.show();
  185. })
  186. }