js-domExtend

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

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

  1. // ==UserScript==
  2. // @name js-domExtend
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.7.2
  5. // @description 轻量级原生js增强插件,将部分原生dom对象方法模仿jQuery进行二次封装,便于使用
  6. // @author tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
  7. // @day 2022.5.27 GMT+0800 (中国标准时间)
  8. // @license MIT License
  9. // @note 相关参考信息请前往greasyfork仓库或github仓库
  10. // @note greasyfork仓库:
  11. // @note github仓库:https://github.com/IcedWatermelonJuice/js-domExtend
  12. // ==/UserScript==
  13.  
  14. function $ele(dom, dom2 = document) {
  15. if (typeof dom === "object") {
  16. return dom;
  17. } else if (!dom || typeof dom !== "string") {
  18. return document;
  19. } else if (dom.trim()[0] === "<" && dom.indexof(">") > 0 && dom.trim().length >= 3) {
  20. dom2 = document.createElement("div");
  21. dom2.innerHTML = dom;
  22. dom2 = dom2.childNodes;
  23. } else {
  24. dom2 = dom2.querySelectorAll(dom);
  25. }
  26. return dom2.length > 1 ? dom2 : dom2[0]
  27. }
  28.  
  29. function $eleFn(dom, dom2 = document) {
  30. return {
  31. data: [dom, dom2],
  32. listen: function(callback, interval = 500) {
  33. if (typeof callback !== "function") {
  34. return null
  35. }
  36. var that = this;
  37. return setInterval(() => {
  38. let target = $ele(that.data[0], that.data[1]);
  39. if (target) {
  40. callback(target);
  41. }
  42. }, interval)
  43. },
  44. ready: function(callback, timeout = 3000) {
  45. var timer = this.listen((target) => {
  46. clearInterval(timer);
  47. callback(target);
  48. })
  49. setTimeout(() => {
  50. clearInterval(timer);
  51. }, timeout)
  52. return timer
  53. },
  54. copy: function(str) {
  55. var res = false;
  56. if (typeof str === "string" && str.trim()) {
  57. let text = $ele("body").insert(`<textarea style="opacity: 0"></textarea>`);
  58. text.value = str;
  59. text.select();
  60. res = document.execCommand("copy");
  61. text.remove();
  62. }
  63. return res;
  64. },
  65. ajax: function(options) {
  66. options.method = options.method || "GET";
  67. options.params = options.params || null;
  68. options.timeout = options.timeout || 60 * 1000;
  69. options.success = typeof options.success === "function" ? options.success : function() {};
  70. options.error = typeof options.error === "function" ? options.error : function() {};
  71. var xhr = new XMLHttpRequest();
  72. xhr.open(options.method, options.url);
  73. if (options.method.toLowerCase() !== 'get') { //判断请求方式
  74. xhr.setRequestHeader('Content-Type', "application / x - www - form - urlencoded");
  75. var str = "";
  76. for (p in options.params) {
  77. str += `${p}=${options.params[p]}&`;
  78. }
  79. options.params = str;
  80. }
  81. xhr.timeout = options.timeout;
  82. xhr.ontimeout = () => {
  83. options.error();
  84. }
  85. xhr.send(options.params);
  86. xhr.onreadystatechange = () => {
  87. if (xhr.readyState === 4) {
  88. if (xhr.status >= 200 && xhr.status < 300) {
  89. options.success(xhr.responseText);
  90. } else {
  91. options.error();
  92. }
  93. }
  94. }
  95. return xhr
  96. }
  97. }
  98. }
  99.  
  100. function $domExtendJS() {
  101. //Element
  102. Element.prototype.attr = function(key, val) {
  103. if (typeof key === "string") {
  104. if (/string|boolean/.test(typeof val)) {
  105. if (!val && val !== false) {
  106. this.removeAttribute(key);
  107. } else {
  108. this.setAttribute(key, val);
  109. }
  110. return this;
  111. } else {
  112. return this.getAttribute(key);
  113. }
  114. }
  115. }
  116. Element.prototype.data = function(key, val) {
  117. this.dataAttrsMap = this.dataAttrsMap ? this.dataAttrsMap : {};
  118. for (let i = 0; i < this.attributes.length; i++) {
  119. let attr = this.attributes[i];
  120. if (/^data-/.test(attr.name)) {
  121. this.dataAttrsMap[attr.name] = attr.value;
  122. }
  123. }
  124. if (typeof key === "string") {
  125. key = `data-${key}`;
  126. if (/string|boolean/.test(typeof val)) {
  127. if (!val && val !== false) {
  128. delete this.dataAttrsMap[key];
  129. this.attr(key, "");
  130. } else {
  131. this.dataAttrsMap[key] = val;
  132. this.attr(key) !== null && this.attr(key, val);
  133. }
  134. return this;
  135. } else {
  136. return this.dataAttrsMap[key];
  137. }
  138. }
  139. }
  140. Element.prototype.css = function(key, val) {
  141. if (typeof key === "string") {
  142. if (/string|boolean/.test(typeof val)) {
  143. this.style.setProperty(key, val);
  144. } else if (!val) {
  145. return getComputedStyle(this)[key];
  146. }
  147. } else {
  148. for (let i in key) {
  149. this.style.setProperty(i, key[i]);
  150. }
  151. }
  152. if (this.style === "") {
  153. this.attr("style", "")
  154. }
  155. return this;
  156. }
  157. Element.prototype.hide = function() {
  158. this.data("displayBackup", this.css("display"));
  159. this.css("display", "none")
  160. return this;
  161. }
  162. Element.prototype.show = function() {
  163. var backup = this.data("displayBackup") || "";
  164. this.css("display", backup !== "none" ? backup : "");
  165. return this;
  166. }
  167. Element.prototype.insert = function(dom, position = "end", reNew = false) {
  168. dom = typeof dom === "string" ? $ele(dom) : dom;
  169. dom = (Array.isArray(dom) || dom instanceof NodeList) ? dom : [dom];
  170. switch (position) {
  171. case "start":
  172. Array.from(dom).reverse().forEach((e, i) => {
  173. this.insertBefore(e, this.firstChild);
  174. })
  175. break;
  176. case "end":
  177. dom.forEach((e, i) => {
  178. this.append(e);
  179. })
  180. break;
  181. case "before":
  182. Array.from(dom).reverse().forEach((e, i) => {
  183. this.parentElement.insertBefore(e, this);
  184. })
  185. break;
  186. case "after":
  187. if (this.parentElement.lastChild === this) {
  188. dom.forEach((e, i) => {
  189. this.append(e);
  190. })
  191. } else {
  192. let next = this.nextSilbing;
  193. Array.from(dom).reverse().forEach((e, i) => {
  194. this.parentElement.insertBefore(e, next);
  195. })
  196. }
  197. break;
  198. }
  199. if (reNew) {
  200. return dom.length > 1 ? dom : dom[0]
  201. } else {
  202. return this
  203. }
  204. }
  205. Element.prototype.replace = function(dom) {
  206. dom = this.insert(dom, "before", true);
  207. this.remove();
  208. return dom;
  209. }
  210. Element.prototype.findNode = function(nodeName) {
  211. var nodeArray = [];
  212. if (!this.firstChild) {
  213. return null
  214. }
  215. this.childNodes.forEach((e, i) => {
  216. if (new RegExp(`^${nodeName}$`, "i").test(e.nodeName)) {
  217. nodeArray.push(e);
  218. } else {
  219. let temp = e.findNode(nodeName);
  220. nodeArray = nodeArray.concat(Array.isArray(temp) ? temp : (temp ? [temp] : []));
  221. }
  222. })
  223. return nodeArray.length > 1 ? nodeArray : nodeArray[0]
  224. }
  225. Element.prototype.eleText = function(val, remainDom = false) {
  226. if (typeof val !== "string") {
  227. return this.innerText
  228. } else {
  229. if (remainDom) {
  230. var textNode = this.findNode("#text");
  231. if (Array.isArray(textNode)) {
  232. textNode.forEach((e, i) => {
  233. if (val === "") {
  234. e.nodeValue = "";
  235. } else {
  236. let textLength = i >= (textNode.length - 1) ? val.length : e.length;
  237. e.nodeValue = val.slice(0, textLength);
  238. val = val.slice(textLength);
  239. }
  240. })
  241. }
  242. } else {
  243. this.innerText = val;
  244. }
  245. return this
  246. }
  247. }
  248. Element.prototype.eleHTML = function(val) {
  249. if (typeof val !== "string") {
  250. return this.innerHTML
  251. } else {
  252. this.innerHTML = val;
  253. return this
  254. }
  255. }
  256. Element.prototype.eleVal = function(val) {
  257. if (typeof val !== "string" && typeof val !== "boolean") {
  258. return this.value
  259. } else {
  260. this.value = val;
  261. return this
  262. }
  263. }
  264. //NodeList
  265. NodeList.prototype.attr = function(key, val) {
  266. this.forEach((e, i) => {
  267. e.attr(key, val)
  268. })
  269. return this
  270. }
  271. NodeList.prototype.data = function(key, val) {
  272. this.forEach((e, i) => {
  273. e.data(key, val)
  274. })
  275. return this
  276. }
  277. NodeList.prototype.css = function(key, val) {
  278. this.forEach((e, i) => {
  279. e.css(key, val)
  280. })
  281. return this
  282. }
  283. NodeList.prototype.hide = function() {
  284. this.forEach((e, i) => {
  285. e.hide();
  286. })
  287. return this
  288. }
  289. NodeList.prototype.show = function() {
  290. this.forEach((e, i) => {
  291. e.show();
  292. })
  293. return this
  294. }
  295. NodeList.prototype.findNode = function(nodeName) {
  296. var nodeArray = []
  297. this.forEach((e, i) => {
  298. let temp = e.findNode(nodeName);
  299. nodeArray = nodeArray.concat(Array.isArray(temp) ? temp : []);
  300. })
  301. return nodeArray[0] ? nodeArray : null
  302. }
  303. NodeList.prototype.eleText = function(val, remainDom = false) {
  304. var res = "";
  305. this.forEach((e, i) => {
  306. let temp = e.eleText(val, remainDom)
  307. res += typeof temp === "string" ? temp : "";
  308. })
  309. return typeof val === "string" ? this : res
  310. }
  311. NodeList.prototype.eleHTML = function(val) {
  312. var res = "";
  313. this.forEach((e, i) => {
  314. let temp = e.eleHTML(val)
  315. res += typeof temp === "string" ? temp : "";
  316. })
  317. return typeof val === "string" ? this : res
  318. }
  319. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址