ENSShared

Shared code for my scripts

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/446540/1061588/ENSShared.js

  1. const c = document.createElement("canvas");
  2. const ctx = c.getContext("2d");
  3.  
  4. const imageToBlob = (imageURL) => {
  5. const img = new Image;
  6. img.crossOrigin = "";
  7. img.src = imageURL;
  8. return new Promise(resolve => {
  9. img.onload = function () {
  10. c.width = this.naturalWidth;
  11. c.height = this.naturalHeight;
  12. ctx.drawImage(this, 0, 0);
  13. c.toBlob((blob) => {
  14. resolve(blob)
  15. }, "image/png", 0.75);
  16. };
  17. })
  18. }
  19.  
  20. const toggleAttribute = (elem, attr, val) => {
  21. const tgattr = (elem, attr, val) => {
  22. if (elem.hasAttribute(attr)) {
  23. let atrval = elem.getAttribute(attr);
  24. if (atrval.includes(val)) {
  25. let rep = atrval.replace(val, '');
  26. !rep.length ? elem.removeAttribute(attr) : elem.setAttribute(attr, rep);
  27. } else elem.setAttribute(attr, elem.getAttribute(attr) + val);
  28. } else elem.setAttribute(attr, val);
  29. }
  30.  
  31. if (elem instanceof Array) {
  32. for (let key in elem) {
  33. tgattr(elem[key], attr, val);
  34. }
  35. } else tgattr(elem, attr, val);
  36. }
  37.  
  38. const awaitSelector = (selector) => {
  39. return new Promise((resolve) => {
  40. const nodes = [];
  41. setInterval(() => {
  42. if (nodes.length !== 0) {
  43. clearInterval(tm);
  44. resolve(nodes);
  45. }
  46. nodes.splice(nodes.length, 0, ...document.querySelectorAll(selector));
  47. }, 100);
  48. });
  49. }

QingJ © 2025

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