Aluk-js

Aluk-js可以帮助用户选择元素,get/post/jsop请求

当前为 2024-02-24 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/488144/1332591/Aluk-js.js

  1. /*
  2. Aluk Query Library
  3. (c)2023-2024 Flutas,All rights,Reserved.
  4. (Powered by Xbodw)
  5.  
  6. Linense MIT
  7. */
  8.  
  9. /**
  10. * @param {*} s - Selector
  11. * @param {*} [m] - Mode
  12. * @param {*} [e] - The base object
  13. */
  14. var aluk = (s, m, e) => {
  15. return new querylist(s, m, e);
  16. };
  17. var al = {
  18. fn: {
  19. request: (o, method) => {
  20. if (!o) {
  21. o = { promise: false, url: '' }
  22. }
  23. if (o.jsonp) {
  24. return new Promise((resolve) => {
  25. var script = document.createElement('script');
  26. let url = new URL(o.url);
  27. let param = url.searchParams;
  28. let callback = aluk.generateRandomFunctionName();
  29. if (!(param.get('callback'))) {
  30. param.set('callback', callback);
  31. }
  32. script.src = url.href;
  33. window[callback] = function (data) {
  34. delete window[callback];
  35. document.body.removeChild(script);
  36. resolve(data);
  37. };
  38. document.body.appendChild(script);
  39. });
  40. }
  41. return new Promise((resolve, reject) => {
  42. if (o.promise) {
  43. fetch(o.url, {
  44. method: method || 'GET',
  45. headers: o.headers,
  46. body: o.body,
  47. })
  48. .then(response => {
  49. if (!response.ok) {
  50. throw new Error('Network response was not ok');
  51. }
  52. resolve(response);
  53. })
  54. .catch(error => {
  55. reject(error);
  56. });
  57. } else {
  58. var xhr = new XMLHttpRequest();
  59. xhr.open(method || 'GET', o.url);
  60. if (o.headers) {
  61. for (var header in o.headers) {
  62. xhr.setRequestHeader(header, o.headers[header]);
  63. }
  64. }
  65. xhr.onload = () => {
  66. if (xhr.status >= 200 && xhr.status < 300) {
  67. resolve(xhr.response);
  68. } else {
  69. reject(xhr.statusText);
  70. }
  71. };
  72. xhr.onerror = () => {
  73. reject(xhr.statusText);
  74. };
  75. xhr.send(o.body);
  76. }
  77. });
  78. }
  79. }
  80. }
  81. aluk.version = '1.5.1';
  82. aluk.language = 'zh-cn';
  83.  
  84. aluk.generateRandomFunctionName = () => {
  85. const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  86. const length = 10;
  87. let randomFunctionName = 'aluk';
  88. for (let i = 0; i < length; i++) {
  89. randomFunctionName += characters.charAt(Math.floor(Math.random() * characters.length));
  90. }
  91. return randomFunctionName;
  92. }
  93.  
  94. /**
  95. * @param {*} s - Selector
  96. * @param {*} [m] - Mode
  97. * @param {*} [e] - The base object
  98. */
  99. function querylist(s, m = {}, e = document) {
  100. var ce;
  101.  
  102. if (s == '' || s == undefined) {
  103. ce = '';
  104. return;
  105. }
  106. if (typeof (s) == 'string' && aluk.checkHtml(s) === false) {
  107. try {
  108. ce = e.querySelectorAll(s);
  109. } catch (ex) {
  110. ce = e;
  111. throw new Error("Failed to execute aluk(s,m,e): selector is undefined or Query Failed")
  112. }
  113.  
  114. } else {
  115. if (typeof (s) == 'number') {
  116. ce = '';
  117. } else {
  118. if (typeof (s) == 'object') {
  119. ce = new Array(s);
  120. } else {
  121. if (aluk.checkHtml(s) === true) {
  122. ce = new Array(aluk.htmlToElement(s));
  123. }
  124. }
  125. }
  126. }
  127. if (m.shadowroot == true) {
  128. ce = Array.from(ce).reduce((acc, curr) => {
  129. acc.push(curr);
  130. if (curr.shadowRoot) {
  131. let shadowRootElements = curr.shadowRoot.querySelectorAll('*');
  132. acc.push(...shadowRootElements);
  133. shadowRootElements.forEach(element => {
  134. let nestedShadowRootElements = queryShadowRoots(element);
  135. acc.push(...nestedShadowRootElements);
  136. });
  137. }
  138. return acc;
  139. }, []);
  140.  
  141. }
  142. if (ce.length > 1) {
  143. ce.forEach(element => {
  144. this.push(element);
  145. });
  146. } else {
  147. if (ce.length > 0) {
  148. this.push(ce[0]);
  149. }
  150. }
  151.  
  152. this.NormalResult = document;
  153. }
  154.  
  155.  
  156. function queryShadowRoots(element) {
  157. if (element.shadowRoot) {
  158. let elements = element.shadowRoot.querySelectorAll('*');
  159. let result = Array.from(elements);
  160. elements.forEach(el => {
  161. let nestedShadowRootElements = queryShadowRoots(el);
  162. result.push(...nestedShadowRootElements);
  163. });
  164. return result;
  165. } else {
  166. return [];
  167. }
  168. }
  169.  
  170. /*
  171. function querylist(s,m = {},e = document) {
  172. var ce;
  173. if (s == '' || s == undefined) {
  174. ce = '';
  175. return;
  176. }
  177. if (typeof (s) == 'string' && aluk.checkHtml(s) === false) {
  178. try {
  179. ce = e.querySelectorAll(s);
  180. } catch(ex) {
  181. ce = e;
  182. throw new Error("Failed to execute aluk(s,m,e): selector is undefined or Query Failed")
  183. }
  184. } else {
  185. if (typeof (s) == 'number') {
  186. ce = '';
  187. } else {
  188. if (typeof (s) == 'object') {
  189. ce = new Array(s);
  190. } else {
  191. if (aluk.checkHtml(s) === true) {
  192. ce = new Array(aluk.htmlToElement(s));
  193. }
  194. }
  195. }
  196. }
  197. if(m.shadowroot == true) {
  198. ce = [];
  199. }
  200. if (ce.length > 1) {
  201. ce.forEach(element => {
  202. this.push(element);
  203. });
  204. } else {
  205. if (ce.length > 0) {
  206. this.push(ce[0]);
  207. }
  208. }
  209. this.NormalResult = document;
  210. }
  211. */
  212.  
  213. querylist.prototype = new Array();
  214.  
  215. querylist.prototype.createChildElement = function (index, options) {
  216. if (typeof (options) != 'object') {
  217. if (aluk.language != 'zh-cn') {
  218. throw new Error('Element Options Type Must as the Object');
  219. } else {
  220. throw new Error('Element选项必须是Object');
  221. }
  222. }
  223. if (options.ElementType == undefined) {
  224. if (aluk.language != 'zh-cn') {
  225. throw new Error('Element name not specified or empty')
  226. } else {
  227. throw new Error('Element类型不能为空');
  228. }
  229.  
  230.  
  231. }
  232. if (options.ElementType == '') {
  233. if (aluk.language != 'zh-cn') {
  234. throw new Error('Element name not specified or empty')
  235. } else {
  236. throw new Error('Element类型不能为空');
  237. }
  238. }
  239. var result = document.createElement(options.ElementType);
  240. if (options.Class == undefined) {
  241.  
  242. } else {
  243. result.classList.value += options.Class;
  244. }
  245. if (options.id == undefined) {
  246.  
  247. } else {
  248. result.id = options.id;
  249. }
  250. if (options.innerHTML == undefined) {
  251.  
  252. } else {
  253. result.innerHTML = options.innerHTML;
  254. }
  255. this[index].appendChild(result);
  256. return Promise.resolve(this[index]);
  257. }
  258.  
  259. aluk.objectToCss = function (o, m = {}) {
  260. return Object.entries(o)
  261. .map(([key, value]) => `${key}: ${value};`)
  262. //.join('\n');
  263. }
  264.  
  265. querylist.prototype.SetCss = function (index, cssList) {
  266.  
  267. if (typeof (index) == 'object') {
  268. var csst = aluk.objectToCss(index);
  269. var cssaddcount = 0;
  270. this.forEach((e) => {
  271. e.style.cssText = '';
  272. csst.forEach(h => {
  273. e.style.cssText += h;
  274. cssaddcount++;
  275. })
  276. })
  277. return cssaddcount;
  278. }
  279. if (index > this.length - 1) {
  280. throw new Error('Index超出了预期范围');
  281. } else if (index == undefined || index == null) {
  282. throw new Error('Index为空或不存在');
  283. }
  284. if (typeof (cssList) != 'object') {
  285. throw new Error('Css列表必须为Object');
  286. }
  287. var csst = aluk.objectToCss(cssList);
  288. this[index].style.cssText = '';
  289. var cssaddcount = 0;
  290. csst.forEach(h => {
  291. this[index].style.cssText += h;
  292. cssaddcount++;
  293. })
  294. return cssaddcount;
  295. }
  296.  
  297. querylist.prototype.AppendorMoveto = function (index, index2, appender) {
  298. var append;
  299. if (appender instanceof querylist) {
  300. if (index > appender.length - 1) {
  301. throw new Error('Index超出了预期范围');
  302. } else if (index == undefined || index == null) {
  303. throw new Error('Index为空或不存在: 如果使用aluk querylist对象代替Element,那么请指定Index');
  304. }
  305. append = appender[index];
  306. } else {
  307. if (!aluk.isHtmlElement(appender)) {
  308. throw new Error('请指定html元素或者aluk querylist对象');
  309. }
  310. append = appender;
  311. }
  312. if (index2 > this.length - 1) {
  313. throw new Error('Index2超出了预期范围');
  314. } else if (index2 == undefined || index2 == null) {
  315. throw new Error('Index2为空或不存在: 选择第几项来插入到appender的' + index2 + '项', '那么请指定Index2');
  316. }
  317. append.appendChild(this[index]);
  318. }
  319.  
  320. querylist.prototype.RemoveX = function () {
  321. let count = 0;
  322. this.forEach(s => {
  323. s.remove();
  324. count++;
  325. })
  326. return count;
  327. }
  328.  
  329. querylist.prototype.continue = function (s) {
  330. if (s == undefined || s == '') {
  331. if (aluk.language != 'zh-cn') {
  332. throw new Error('Your Selector was empty or undefined,please.');
  333. } else {
  334. throw new Error('您的选择器为空或未定义');
  335. }
  336. }
  337. var newe = [];
  338.  
  339. for (var i = 0; i < this.length; i++) {
  340. var m = aluk(s,{},this[i]);
  341. newe.push(m);
  342. }
  343. var n = new querylist('<null>');
  344. n.shift();
  345. newe.forEach(y => {
  346. y.forEach(z => {
  347. n.push(z)
  348. })
  349. })
  350. n.NormalResult = n[0];
  351. return n;
  352. }
  353.  
  354. querylist.prototype.gsval = function (text) {
  355. if (text == undefined) {
  356. var result = [];
  357. this.forEach(e => {
  358. result.push(e.value)
  359. })
  360. return result;
  361. } else {
  362. this.forEach(e => {
  363. e.value = text;
  364. })
  365. }
  366. }
  367.  
  368. querylist.prototype.gstext = function (text) {
  369. if (text == undefined) {
  370. var result = [];
  371. this.forEach(e => {
  372. result.push(e.innerText)
  373. })
  374. return result;
  375. } else {
  376. this.forEach(e => {
  377. e.innerText = text;
  378. })
  379. }
  380. }
  381.  
  382. querylist.prototype.newclicke = function (call) {
  383. if (call == undefined || typeof call != 'function') {
  384. throw new Error('函数为空');
  385. }
  386. this.forEach(ef => {
  387. ef.addEventListener('click', function (e) {
  388. call(e);
  389. })
  390. })
  391. }
  392.  
  393. querylist.prototype.Prep = function (call) {
  394. var events = ['addEventListener'];
  395. var getEvent = function (index) {
  396. index = index - 0;
  397. var eventName = events[index];
  398. return eventName;
  399. };
  400. document[getEvent(0)]('DOMContentLoaded', function (event) {
  401. call(event);
  402. });
  403. }
  404.  
  405. querylist.prototype.event = (o,s,t) => {
  406. if(typeof (o) == 'number') {
  407. if(this.length > 0) {
  408. this[o].addEventListener(s,t);
  409. } else {
  410. throw new Error('Failed to event(o,s,t): Beyond the bounds of an array')
  411. }
  412. } else {
  413. if(this.length > 1) {
  414. this.forEach(element => {
  415. element.addEventListener(o,s);
  416. })
  417. } else {
  418. if(this.length > 0) {
  419. this[0].addEventListener(o,s);
  420. }
  421. }
  422. }
  423. }
  424.  
  425. querylist.prototype.hide = (i) => {
  426. if(typeof(i) == 'undefined') {
  427. if(this.length > 1) {
  428. this.forEach(e => {
  429. e.SetCss({'display' : 'none'});
  430. })
  431. } else {
  432. this[0].SetCss({'display' : 'none'});
  433. }
  434. } else {
  435. this[i].SetCss({'display' : 'none'});
  436. }
  437. }
  438.  
  439. querylist.prototype.show = (i) => {
  440. if(typeof(i) == 'undefined') {
  441. if(this.length > 1) {
  442. this.forEach(e => {
  443. e.SetCss({'display' : ''});
  444. })
  445. } else {
  446. this[0].SetCss({'display' : ''});
  447. }
  448. } else {
  449. this[i].SetCss({'display' : ''});
  450. }
  451. }
  452.  
  453. aluk.isHtmlElement = (variable) => {
  454. return variable instanceof Element || variable instanceof HTMLElement;
  455. }
  456.  
  457. aluk.createElementX = (options) => {
  458. if (typeof (options) != 'object') {
  459. if (aluk.language != 'zh-cn') {
  460. throw new Error('Element Options Type Must as the Object');
  461. } else {
  462. throw new Error('Element选项必须是Object');
  463. }
  464. }
  465. if (options.ElementType == undefined) {
  466. if (aluk.language != 'zh-cn') {
  467. throw new Error('Element name not specified or empty')
  468. } else {
  469. throw new Error('Element类型不能为空');
  470. }
  471.  
  472.  
  473. }
  474. if (options.ElementType == '') {
  475. if (aluk.language != 'zh-cn') {
  476. throw new Error('Element name not specified or empty')
  477. } else {
  478. throw new Error('Element类型不能为空');
  479. }
  480. }
  481. var result = document.createElement(options.ElementType);
  482. if (options.Class == undefined) {
  483.  
  484. } else {
  485. result.classList.value += options.Class;
  486. }
  487. if (options.id == undefined) {
  488.  
  489. } else {
  490. result.id = options.id;
  491. }
  492. if (options.innerHTML == undefined) {
  493.  
  494. } else {
  495. result.innerHTML = options.innerHTML;
  496. }
  497. return aluk(result);
  498. }
  499.  
  500. aluk.htmlEscape = (htmlStr) => {
  501. return htmlStr.replace(/<|>|"|&/g, match => {
  502. switch (match) {
  503. case '<':
  504. return '&lt;';
  505. case '>':
  506. return '&gt;';
  507. case '"':
  508. return '&quot;';
  509. case '&':
  510. return '&amp;';
  511. }
  512. })
  513. }
  514. aluk.htmlUnescape = (html) => {
  515. return html.replace(/&lt;|&gt;|&quot;|&amp;/g, match => {
  516. switch (match) {
  517. case '&lt;':
  518. return '<';
  519. case '&gt;':
  520. return '>';
  521. case '&quot;':
  522. return '"';
  523. case '&amp;':
  524. return '&';
  525. }
  526. })
  527. }
  528.  
  529.  
  530. aluk.appendHTMLX = (appender, element, options) => {
  531. if (appender == undefined) {
  532. if (aluk.language != 'zh-cn') {
  533. throw new Error('AppendElement name not specified or empty')
  534. } else {
  535. throw new Error('追加者Element类型不能为空');
  536. }
  537. }
  538. if (element == undefined) {
  539. if (aluk.language != 'zh-cn') {
  540. throw new Error('Append HTML not specified or empty')
  541. } else {
  542. throw new Error('追加的HTML不能为空');
  543. }
  544. }
  545. if (typeof (options) != 'boolean') {
  546. if (options != undefined) {
  547. if (aluk.language != 'zh-cn') {
  548. throw new Error('Options not specified or empty')
  549. } else {
  550. throw new Error('选项为空或不存在');
  551. }
  552. }
  553. }
  554. let fixr = element.innerHTML;
  555. let fixed = fixr;
  556. if (options) {
  557. fixed = aluk.htmlEscape(fixr);
  558. }
  559. appender.innerHTML += fixed;
  560. return Promise.resolve(appender.innerHTML);
  561. }
  562.  
  563. aluk.checkHtml = (htmlStr) => {
  564.  
  565. var reg = /<[a-z][\s\S]*>/i;
  566.  
  567. return reg.test(htmlStr);
  568.  
  569. }
  570. aluk.htmlToElement = (html) => {
  571. const parser = new DOMParser();
  572. const doc = parser.parseFromString(html, 'text/html');
  573. return doc.body.firstChild;
  574. }
  575.  
  576. aluk.WebUrlToBase64 = function (url, callback) {
  577. var methods = [
  578. 'send',
  579. 'responseType',
  580. 'onload'
  581. ];
  582. var getMethod = function (index) {
  583. index = index - 0;
  584. var methodName = methods[index];
  585. return methodName;
  586. };
  587. var xhr = new XMLHttpRequest();
  588. xhr[getMethod(2)] = function () {
  589. var fileReader = new FileReader();
  590. fileReader.onloadend = function () {
  591. callback(fileReader.result);
  592. };
  593. fileReader.readAsDataURL(xhr.response);
  594. };
  595. xhr.open('GET', url);
  596. xhr[getMethod(1)] = 'blob';
  597. xhr[getMethod(0)]();
  598. }
  599.  
  600.  
  601.  
  602. aluk.ajax = (o) => {
  603. return new al.fn.ajax(o, o.method);
  604. }
  605.  
  606. aluk.encodeX = (code, key = 0) => {
  607. var keys;
  608. if (key == undefined) {
  609. keys = '0';
  610. } else {
  611. keys = key;
  612. }
  613. var codeb;
  614. if (typeof code == 'object') {
  615. codeb = JSON.stringify(code)
  616. } else {
  617. codeb = code;
  618. }
  619. var codea = window.btoa(window.encodeURI(codeb));
  620. var lista = [];
  621. for (var i = 0; i < codea.length; i++) {
  622. var asciic = escape(codea.charCodeAt(i) + key).replace(/\%u/g, '/u');
  623. lista.push(asciic);
  624. } return lista;
  625. }
  626.  
  627.  
  628. aluk.decodeX = (code, key = 0) => {
  629. if (!Array.isArray(code)) { return }
  630. var keys;
  631. if (key == undefined) {
  632. keys = '0';
  633. } else {
  634. keys = key;
  635. }
  636. var result = '';
  637. var resultb = '';
  638. code.forEach(e => {
  639. var sh = unescape(String.fromCharCode(e - key)).replace(/\/u/g, '%u');
  640. resultb += sh;
  641. })
  642. result = window.decodeURI(window.atob(resultb));
  643. return result;
  644. }
  645. Terminal = function (output) {
  646. if (!aluk.isHtmlElement(output)) {
  647. return this;
  648. }
  649. this.Resultelement = output;
  650. }
  651.  
  652. TerminalAPI = function () { return this; };
  653.  
  654. var API = undefined;
  655. var consolebak = undefined;
  656. function InTerminal(e) {
  657. consolebak = console.log;
  658. console.log = function (e) { return e; };
  659. API = new TerminalAPI();
  660. API.InvertHTMLinit = function () {
  661. aluk.createElementX({
  662. ElementType: "style",
  663. id: "InvertHTMLStyleSheet",
  664. innerHTML: "*.invert {filter: invert(100%);}"
  665. }).AppendorMoveto(0, 0, document.head);
  666. }
  667. API.Invert = function () {
  668. aluk('*').forEach(e => {
  669. e.classList.add('invert');
  670. })
  671. }
  672. API.Revert = function () {
  673. aluk('*').forEach(e => {
  674. e.classList.remove('invert');
  675. })
  676. }
  677. }
  678. Terminal.prototype = new Object();
  679. Terminal.prototype.command = function (command) {
  680. InTerminal('');
  681. var t = '<br>' + eval(command) + '<br>';
  682. this.Resultelement.innerHTML += t;
  683. API = undefined;
  684. console.log = consolebak;
  685. }
  686.  
  687. function Alarm(construct, title) {
  688. this.onalarmisdiscard = null; // 初始化 onalarmisdiscard 事件为 null
  689. this.obj = construct;
  690. this.title = '提示';
  691. if (title != undefined) {
  692. this.title = title;
  693. }
  694. }
  695.  
  696. // 定义 show 函数
  697. Alarm.prototype.show = function () {
  698. // 创建提示框元素
  699. var alarmBox = document.createElement('div');
  700. alarmBox.className = 'alarm-box';
  701. alarmBox.innerHTML = '<h4 id="alarm-title">' + this.title + '</h4><span id="alarm-text">' + this.obj + '</span>';
  702. // 创建关闭按钮元素
  703. var closeButton = document.createElement('div');
  704. closeButton.className = 'close-button';
  705. closeButton.innerText = '×';
  706. alarmBox.appendChild(closeButton);
  707. var styleElement = document.createElement('style');
  708. styleElement.classList.add('alarmbox-style');
  709. var cssCode = `
  710. .alarm-box {
  711. position: fixed;
  712. top: 20px;
  713. left: 0px;
  714. right: 0px;
  715. width: 300px;
  716. height: 500px
  717. margin: auto;
  718. padding: 20px;
  719. background-color: #f9f9f9;
  720. border-radius: 5px;
  721. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  722. z-index: 9999;
  723. overflow: auto; /* 添加滚动条 */
  724. }
  725. .alarm-box .close-button {
  726. position: absolute;
  727. top: 10px;
  728. right: 10px;
  729. width: 20px;
  730. height: 20px;
  731. line-height: 20px;
  732. text-align: center;
  733. cursor: pointer;
  734. color: #888;
  735. }
  736.  
  737. .alarm-box>* {
  738. font-family: "Microsoft YaHei Ui Light",ui-sans-serif,system-ui,Segoe UI;
  739. font-size: 95%;
  740. max-width: fix-content;
  741. }
  742. .alarm-box .close-button:hover {
  743. color: #000;
  744. }
  745. `;
  746. styleElement.appendChild(document.createTextNode(cssCode));
  747. document.head.appendChild(styleElement);
  748. document.body.appendChild(alarmBox);
  749. closeButton.onclick = 'this.parentNode.removeChild(this)';
  750. closeButton.addEventListener('click', async function () {
  751. await aluk('.alarmbox-style').RemoveX();
  752. await closeButton.parentElement.remove();
  753. if (this.onalarmisdiscard) {
  754. this.onalarmisdiscard();
  755. }
  756. }.bind(this));
  757. };