GM API script

Allows use of GM_getValue and GM_setValue, with @grant none

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/10208/150762/GM%20API%20script.js

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