Aluk-js

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

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

QingJ © 2025

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