Просмотр исходного кода

fix: revert `global` from eb4a8d0a

...because it won't work for injectPageSandbox
tophf 2 лет назад
Родитель
Сommit
ef65805c6a

+ 1 - 5
.eslintrc.js

@@ -96,15 +96,11 @@ module.exports = {
 function makeOverrides() {
   /* Note that `injected` uses several more `common` files indirectly, but we check just these
    * two automatically because they are trivial by design and must always pass the check */
-  const GLOBALS_SHARED = {
-    ...getGlobals('*'),
-    global: false, // defined in webpack.conf
-  };
+  const GLOBALS_SHARED = getGlobals('*');
   const GLOBALS_INJECTED = {
     ...getGlobals('injected'),
     PAGE_MODE_HANDSHAKE: false,
     VAULT_ID: false,
-    global: false, // defined in webpack.conf
   };
   function getGlobals(path) {
     const res = {};

+ 1 - 5
scripts/webpack.conf.js

@@ -92,13 +92,9 @@ const defsObj = {
   'process.env.DEV': JSON.stringify(!isProd),
   'process.env.TEST': JSON.stringify(process.env.BABEL_ENV === 'test'),
 };
-/** `window` and `document` are unforgeable so we extract them primarily to improve minification.
- * The document's value can change only in about:blank but we don't inject there. */
-const setUnforgeablesHeader = 'const global = this, { document, window } = global;';
 // avoid running webpack bootstrap in a potentially hacked environment
 // after documentElement was replaced which triggered reinjection of content scripts
 const skipReinjectionHeader = `{
-  ${setUnforgeablesHeader}
   const INIT_FUNC_NAME = '${INIT_FUNC_NAME}';
   if (window[INIT_FUNC_NAME] !== 1)`;
 
@@ -137,7 +133,7 @@ const modify = (page, entry, init) => modifyWebpackConfig(
 module.exports = Promise.all([
   modify((config) => {
     addWrapperWithGlobals('common', config, defsObj, getGlobals => ({
-      header: () => `{ ${setUnforgeablesHeader} ${getGlobals()}`,
+      header: () => `{ ${getGlobals()}`,
       footer: '}',
       test: /^(?!injected|public).*\.js$/,
     }));

+ 3 - 0
src/common/safe-globals-shared.js

@@ -2,9 +2,12 @@
 
 /**
  * This file is used first by the entire `src` including `injected`.
+ * `global` is used instead of WebPack's polyfill which we disable in webpack.conf.js.
  * Not exporting NodeJS built-in globals as this file is imported in the test scripts.
  */
 
+const global = process.env.TEST ? globalThis : this; // eslint-disable-line no-undef
+const { window } = global; // it's unforgeable so we extract it primarily to improve minification
 export const VIOLENTMONKEY = 'Violentmonkey';
 export const AUTO = 'auto';
 export const CONTENT = 'content';

+ 4 - 1
src/injected/content/safe-globals.js

@@ -30,7 +30,6 @@ export const SafeError = Error;
 export const ResponseProto = SafeResponse[PROTO];
 export const hasOwnProperty = safeApply.call.bind(({}).hasOwnProperty);
 export const { forEach, includes } = []; // `push` is unsafe as it may call a setter; use safePush()
-export const { getElementsByTagName } = document;
 export const { then } = SafePromise[PROTO];
 export const { indexOf: stringIndexOf, slice } = '';
 export const safeCharCodeAt = safeApply.call.bind(''.charCodeAt); // faster than str::charCodeAt
@@ -58,4 +57,8 @@ export const isInstance = (instance, safeOriginalProto) => {
   }
 };
 export const isPromise = (proto => val => isInstance(val, proto))(SafePromise[PROTO]);
+/** It's unforgeable so we extract it primarily to improve minification.
+ * The document's value can change only in about:blank but we don't inject there. */
+const { document } = global;
+export const { getElementsByTagName } = document;
 export let IS_FIREFOX = !chrome.app;