Browse Source

feat: add GreaseMonkey4 GM.* aliases (#651)

tophf 6 years ago
parent
commit
425ed688e2
2 changed files with 31 additions and 7 deletions
  1. 15 1
      src/injected/web/gm-api.js
  2. 16 6
      src/injected/web/gm-wrapper.js

+ 15 - 1
src/injected/web/gm-api.js

@@ -204,7 +204,21 @@ export function createGmApiProps() {
       target[k] = propertyFromValue(target[k]);
       target[k] = propertyFromValue(target[k]);
     });
     });
   });
   });
-  return { props, boundProps };
+  return {
+    props,
+    boundProps,
+    gm4: {
+      getResourceURL: { async: true },
+      getValue: { async: true },
+      deleteValue: { async: true },
+      setValue: { async: true },
+      listValues: { async: true },
+      xmlHttpRequest: { alias: 'xmlhttpRequest' },
+      notification: true,
+      openInTab: true,
+      setClipboard: true,
+    },
+  };
 }
 }
 
 
 export function propertyFromValue(value) {
 export function propertyFromValue(value) {

+ 16 - 6
src/injected/web/gm-wrapper.js

@@ -1,7 +1,7 @@
 import { INJECT_PAGE, INJECT_CONTENT, METABLOCK_RE } from '#/common/consts';
 import { INJECT_PAGE, INJECT_CONTENT, METABLOCK_RE } from '#/common/consts';
 import bridge from './bridge';
 import bridge from './bridge';
 import {
 import {
-  forEach, includes, match, objectKeys, map, defineProperties, filter, defineProperty,
+  forEach, includes, match, objectKeys, map, defineProperties, filter, defineProperty, slice,
 } from '../utils/helpers';
 } from '../utils/helpers';
 import { createGmApiProps, propertyFromValue } from './gm-api';
 import { createGmApiProps, propertyFromValue } from './gm-api';
 
 
@@ -47,7 +47,7 @@ export function wrapGM(script, code, cache) {
     };
     };
   }
   }
   const resources = script.meta.resources || {};
   const resources = script.meta.resources || {};
-  const gmInfo = {
+  const gmInfo = propertyFromValue({
     uuid: script.props.uuid,
     uuid: script.props.uuid,
     scriptMetaStr: code::match(METABLOCK_RE)[1] || '',
     scriptMetaStr: code::match(METABLOCK_RE)[1] || '',
     scriptWillUpdate: !!script.config.shouldUpdate,
     scriptWillUpdate: !!script.config.shouldUpdate,
@@ -69,24 +69,33 @@ export function wrapGM(script, code, cache) {
       unwrap: false, // deprecated, always `false`
       unwrap: false, // deprecated, always `false`
       version: script.meta.version || '',
       version: script.meta.version || '',
     },
     },
-  };
+  });
+  const gm4props = { info: gmInfo };
   const grantedProps = {
   const grantedProps = {
     unsafeWindow: propertyFromValue(unsafeWindow),
     unsafeWindow: propertyFromValue(unsafeWindow),
-    GM_info: propertyFromValue(gmInfo),
+    GM_info: gmInfo,
+    GM: propertyFromValue({}),
   };
   };
   let selfData;
   let selfData;
   if (!gmApi) gmApi = createGmApiProps();
   if (!gmApi) gmApi = createGmApiProps();
   grant::forEach((name) => {
   grant::forEach((name) => {
+    const gm4name = name::startsWith('GM.') && name::slice(3);
+    const gm4 = gmApi.gm4[gm4name];
+    if (gm4) name = `GM_${gm4.alias || gm4name}`;
     let prop = gmApi.boundProps[name];
     let prop = gmApi.boundProps[name];
     if (prop) {
     if (prop) {
       const gmFunction = prop.value;
       const gmFunction = prop.value;
       prop = { ...prop };
       prop = { ...prop };
-      prop.value = (...args) => gmFunction.apply(selfData, args);
+      prop.value = gm4 && gm4.async
+        ? (async (...args) => gmFunction.apply(selfData, args))
+        : ((...args) => gmFunction.apply(selfData, args));
       selfData = true;
       selfData = true;
     } else {
     } else {
       prop = gmApi.props[name];
       prop = gmApi.props[name];
     }
     }
-    if (prop) grantedProps[name] = prop;
+    if (!prop) return;
+    if (gm4) gm4props[gm4name] = prop;
+    else grantedProps[name] = prop;
   });
   });
   if (selfData) {
   if (selfData) {
     selfData = {
     selfData = {
@@ -98,6 +107,7 @@ export function wrapGM(script, code, cache) {
       urls: {},
       urls: {},
     };
     };
   }
   }
+  defineProperties(grantedProps.GM.value, gm4props);
   return {
   return {
     thisObj,
     thisObj,
     wrapper: defineProperties(gm, grantedProps),
     wrapper: defineProperties(gm, grantedProps),