GMLibrary

GMLibary

目前为 2022-12-18 提交的版本。查看 最新版本

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

  1. // ==UserScript==
  2. // @name GMLibrary
  3. // @namespace https://greasyfork.org/users/28298
  4. // @version 1.5
  5. // @description GMLibary
  6. // @author Jerry
  7. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  8. // @grant GM_setClipboard
  9. // @grant GM_download
  10. // @grant GM_addStyle
  11. // @grant GM_notification
  12. // @grant GM_xmlhttpRequest
  13. // @noframes
  14. // @license GNU GPLv3
  15. // ==/UserScript==
  16.  
  17. // match violentmonkey supports * anywhere in url
  18. // GM_notification requires macOS to turn on notification for browser
  19. // https://violentmonkey.github.io/api/gm/
  20. // https://www.tampermonkey.net/documentation.php?ext=dhdg
  21. function addbutton(text,func,top,left,width,height) {
  22. //top, left, [width[, height]] in px
  23. // e.g., width 100px, height 25px
  24. // https://stackoverflow.com/a/1535421/2292993
  25. if (window.top != window.self) {
  26. return;
  27. } //don't run on frames or iframes
  28.  
  29. let btn = document.createElement("button");
  30. btn.innerHTML = text;
  31. document.body.appendChild(btn);
  32. btn.addEventListener("click", func);
  33.  
  34. btn.style.cssText = "border-radius: 5px; border:1px solid black; background-color:#D3D3D3; color:black";
  35. btn.style.position = 'absolute';
  36. btn.style.top = top+'px';
  37. btn.style.left = left+'px';
  38. if (width !== undefined) {btn.style.width = width+'px';}
  39. if (height !== undefined) {btn.style.height = height+'px';}
  40. console.log("top: " + top + 'px' + " left: " + left + 'px');
  41. }
  42.  
  43. // must call with await in async function; otherwise not working
  44. function asleep(ms) {
  45. setTimeout(()=>{console.log("Sleeping...");},3000);
  46. return new Promise(resolve => setTimeout(resolve, ms));
  47. }
  48.  
  49. function sleep(millis) {
  50. var date = new Date();
  51. var curDate = null;
  52. do { curDate = new Date(); }
  53. while(curDate-date < millis);
  54. }
  55.  
  56. function hget (url) {
  57. // https://wiki.greasespot.net/GM.xmlHttpRequest
  58. // https://stackoverflow.com/a/65561572/2292993
  59. return new Promise((resolve, reject) => {
  60. GM_xmlhttpRequest({
  61. method: "GET",
  62. url: url,
  63. // headers: {
  64. // "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
  65. // "Accept": "text/html" // If not specified, browser defaults will be used.
  66. // },
  67. onload: function(response) {
  68. var responseXML = null;
  69. if (!response.responseXML) {
  70. responseXML = new DOMParser()
  71. .parseFromString(response.responseText, "text/html");
  72. } else {
  73. responseXML = response.responseXML;
  74. }
  75. resolve(responseXML);
  76. // console.log([
  77. // response.status,
  78. // response.statusText,
  79. // response.readyState,
  80. // response.responseHeaders,
  81. // response.responseText,
  82. // response.finalUrl,
  83. // responseXML
  84. // ].join("\n"));
  85. },
  86. onerror: function(error) {
  87. reject(error);
  88. }
  89. });
  90. });
  91. }
  92.  
  93. // https://github.com/zevero/simpleWebstorage
  94. /*
  95. Wonder how this works?
  96. Storage is the Prototype of both localStorage and sessionStorage.
  97. Got it?
  98.  
  99. localStorage.set('myKey',{a:[1,2,5], b: 'ok'}); //can set a json Object
  100. localStorage.assign('myKey',{a:[6], c:42}); //shallow merge using Object.assign
  101. localStorage.has('myKey'); // --> true
  102. localStorage.get('myKey'); // --> {a:[6], b: 'ok', c:42}
  103. localStorage.keys(); // --> ['myKey']
  104. localStorage.remove('myKey'); // -
  105.  
  106. native:
  107. localStorage.clear();
  108. localStorage.length;
  109. */
  110. Storage.prototype.set = function(key, obj) {
  111. var t = typeof obj;
  112. if (t==='undefined' || obj===null ) this.removeItem(key);
  113. this.setItem(key, (t==='object')?JSON.stringify(obj):obj);
  114. return obj;
  115. };
  116. Storage.prototype.get = function(key) {
  117. var obj = this.getItem(key);
  118. try {
  119. var j = JSON.parse(obj);
  120. if (j && typeof j === "object") return j;
  121. } catch (e) { }
  122. return obj;
  123. };
  124. Storage.prototype.assign = function(key, obj_merge) {
  125. var obj = this.get(key);
  126. if (typeof obj !== "object" || typeof obj_merge !== "object") return null;
  127. Object.assign(obj, obj_merge);
  128. return this.set(key,obj);
  129. };
  130.  
  131. Storage.prototype.has = Storage.prototype.hasOwnProperty;
  132. Storage.prototype.remove = Storage.prototype.removeItem;
  133.  
  134. Storage.prototype.keys = function() {
  135. return Object.keys(this.valueOf());
  136. };