grant-none-shim.js

https://gist.github.com/arantius/3123124

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

  1. /*
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2014 Anthony Lieuallen
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23.  
  24.  
  25. This script is intended to be used with @require, for Greasemonkey scripts
  26. using "@grant none". It emulates the GM_ APIs as closely as possible, using
  27. modern browser features like DOM storage.
  28.  
  29. Scripts should plan to remove usage of GM_ APIs, but this shim offers a
  30. short-term workaround to gain the benefits of running in the security
  31. restriction free "@grant none" mode before that is completed.
  32.  
  33. Read the comments on each function to learn if its emulation is good enough
  34. for your purposes.
  35.  
  36. NOT IMPLEMENTED:
  37. * GM_getResourceText
  38. * GM_openInTab
  39. * GM_registerMenuCommand
  40. */
  41.  
  42. function GM_addStyle(aCss) {
  43. 'use strict';
  44. let head = document.getElementsByTagName('head')[0];
  45. if (head) {
  46. let style = document.createElement('style');
  47. style.setAttribute('type', 'text/css');
  48. style.textContent = aCss;
  49. head.appendChild(style);
  50. return style;
  51. }
  52. return null;
  53. }
  54.  
  55. const GM_log = console.log;
  56.  
  57. // This naive implementation will simply fail to do cross-domain requests,
  58. // just like any javascript in any page would.
  59. function GM_xmlhttpRequest(aOpts) {
  60. 'use strict';
  61. let req = new XMLHttpRequest();
  62.  
  63. __setupRequestEvent(aOpts, req, 'abort');
  64. __setupRequestEvent(aOpts, req, 'error');
  65. __setupRequestEvent(aOpts, req, 'load');
  66. __setupRequestEvent(aOpts, req, 'progress');
  67. __setupRequestEvent(aOpts, req, 'readystatechange');
  68.  
  69. req.open(aOpts.method, aOpts.url, !aOpts.synchronous,
  70. aOpts.user || '', aOpts.password || '');
  71. if (aOpts.overrideMimeType) {
  72. req.overrideMimeType(aOpts.overrideMimeType);
  73. }
  74. if (aOpts.headers) {
  75. for (let prop in aOpts.headers) {
  76. if (Object.prototype.hasOwnProperty.call(aOpts.headers, prop)) {
  77. req.setRequestHeader(prop, aOpts.headers[prop]);
  78. }
  79. }
  80. }
  81. let body = aOpts.data ? aOpts.data : null;
  82. if (aOpts.binary) {
  83. return req.sendAsBinary(body);
  84. } else {
  85. return req.send(body);
  86. }
  87. }
  88.  
  89. function __setupRequestEvent(aOpts, aReq, aEventName) {
  90. 'use strict';
  91. if (!aOpts['on' + aEventName]) return;
  92.  
  93. aReq.addEventListener(aEventName, function(aEvent) {
  94. let responseState = {
  95. responseText: aReq.responseText,
  96. responseXML: aReq.responseXML,
  97. readyState: aReq.readyState,
  98. responseHeaders: null,
  99. status: null,
  100. statusText: null,
  101. finalUrl: null
  102. };
  103. switch (aEventName) {
  104. case "progress":
  105. responseState.lengthComputable = aEvent.lengthComputable;
  106. responseState.loaded = aEvent.loaded;
  107. responseState.total = aEvent.total;
  108. break;
  109. case "error":
  110. break;
  111. default:
  112. if (4 != aReq.readyState) break;
  113. responseState.responseHeaders = aReq.getAllResponseHeaders();
  114. responseState.status = aReq.status;
  115. responseState.statusText = aReq.statusText;
  116. break;
  117. }
  118. aOpts['on' + aEventName](responseState);
  119. });
  120. }
  121.  
  122. const __GM_STORAGE_PREFIX = [
  123. '', GM_info.script.namespace, GM_info.script.name, ''].join('***');
  124.  
  125. // All of the GM_*Value methods rely on DOM Storage's localStorage facility.
  126. // They work like always, but the values are scoped to a domain, unlike the
  127. // original functions. The content page's scripts can access, set, and
  128. // remove these values. A
  129. function GM_deleteValue(aKey) {
  130. 'use strict';
  131. localStorage.removeItem(__GM_STORAGE_PREFIX + aKey);
  132. }
  133.  
  134. function GM_getValue(aKey, aDefault) {
  135. 'use strict';
  136. let val = localStorage.getItem(__GM_STORAGE_PREFIX + aKey)
  137. if (null === val && 'undefined' != typeof aDefault) return aDefault;
  138. return val;
  139. }
  140.  
  141. function GM_listValues() {
  142. 'use strict';
  143. let prefixLen = __GM_STORAGE_PREFIX.length;
  144. let values = [];
  145. let i = 0;
  146. for (let i = 0; i < localStorage.length; i++) {
  147. let k = localStorage.key(i);
  148. if (k.substr(0, prefixLen) === __GM_STORAGE_PREFIX) {
  149. values.push(k.substr(prefixLen));
  150. }
  151. }
  152. return values;
  153. }
  154.  
  155. function GM_setValue(aKey, aVal) {
  156. 'use strict';
  157. localStorage.setItem(__GM_STORAGE_PREFIX + aKey, aVal);
  158. }
  159.  
  160. function GM_getResourceURL(aName) {
  161. 'use strict';
  162. return 'greasemonkey-script:' + GM_info.uuid + '/' + aName;
  163. }

QingJ © 2025

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