GMLibrary

GMLibary

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

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

  1. // ==UserScript==
  2. // @name GMLibrary
  3. // @namespace https://greasyfork.org/users/28298
  4. // @version 1.4
  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 sleep(ms) {
  45. // setTimeout(()=>{console.log("Sleeping...");},3000);
  46. // return new Promise(resolve => setTimeout(resolve, ms));
  47. // }
  48. function sleep(millis) {
  49. var date = new Date();
  50. var curDate = null;
  51. do { curDate = new Date(); }
  52. while(curDate-date < millis);
  53. }
  54.  
  55. function hget (url) {
  56. // https://wiki.greasespot.net/GM.xmlHttpRequest
  57. // https://stackoverflow.com/a/65561572/2292993
  58. return new Promise((resolve, reject) => {
  59. GM_xmlhttpRequest({
  60. method: "GET",
  61. url: url,
  62. // headers: {
  63. // "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
  64. // "Accept": "text/html" // If not specified, browser defaults will be used.
  65. // },
  66. onload: function(response) {
  67. var responseXML = null;
  68. if (!response.responseXML) {
  69. responseXML = new DOMParser()
  70. .parseFromString(response.responseText, "text/html");
  71. } else {
  72. responseXML = response.responseXML;
  73. }
  74. resolve(responseXML);
  75. // console.log([
  76. // response.status,
  77. // response.statusText,
  78. // response.readyState,
  79. // response.responseHeaders,
  80. // response.responseText,
  81. // response.finalUrl,
  82. // responseXML
  83. // ].join("\n"));
  84. },
  85. onerror: function(error) {
  86. reject(error);
  87. }
  88. });
  89. });
  90. }