Browse Source

refactor: extract getScriptPrettyUrl + constants

tophf 3 years ago
parent
commit
814876e5be

+ 2 - 2
src/background/index.js

@@ -1,12 +1,12 @@
 import '@/common/browser';
 import { getActiveTab, makePause, sendCmd } from '@/common';
-import { TIMEOUT_24HOURS, TIMEOUT_MAX } from '@/common/consts';
+import { TIMEOUT_24HOURS, TIMEOUT_MAX, extensionOrigin } from '@/common/consts';
 import { deepCopy } from '@/common/object';
 import * as tld from '@/common/tld';
 import * as sync from './sync';
 import { commands } from './utils';
 import { getData, getSizes, checkRemove } from './utils/db';
-import { extensionOrigin, initialize } from './utils/init';
+import { initialize } from './utils/init';
 import { getOption, hookOptions } from './utils/options';
 import './utils/clipboard';
 import './utils/hotkeys';

+ 0 - 3
src/background/utils/init.js

@@ -1,6 +1,3 @@
-export const extensionRoot = browser.runtime.getURL('/');
-export const extensionOrigin = extensionRoot.slice(0, -1);
-
 export const preInitialize = [];
 export const postInitialize = [];
 

+ 3 - 11
src/background/utils/preinject.js

@@ -1,4 +1,4 @@
-import { getScriptName, getUniqId, sendTabCmd, trueJoin } from '@/common';
+import { getScriptName, getScriptPrettyUrl, getUniqId, sendTabCmd, trueJoin } from '@/common';
 import {
   INJECT_AUTO, INJECT_CONTENT, INJECT_MAPPING, INJECT_PAGE,
   METABLOCK_RE,
@@ -7,7 +7,7 @@ import initCache from '@/common/cache';
 import { forEachEntry, objectPick, objectSet } from '@/common/object';
 import ua from '@/common/ua';
 import { getScriptsByURL, ENV_CACHE_KEYS, ENV_REQ_KEYS, ENV_SCRIPTS, ENV_VALUE_IDS } from './db';
-import { extensionRoot, postInitialize } from './init';
+import { postInitialize } from './init';
 import { commands } from './message';
 import { getOption, hookOptions } from './options';
 import { popupTabs } from './popup-tracker';
@@ -44,7 +44,6 @@ const KEY_EXPOSE = 'expose';
 const KEY_DEF_INJECT_INTO = 'defaultInjectInto';
 const KEY_IS_APPLIED = 'isApplied';
 const KEY_XHR_INJECT = 'xhrInject';
-const BAD_URL_CHAR = /[#/?]/g; // will be encoded to avoid splitting the URL in devtools UI
 const GRANT_NONE_VARS = '{GM,GM_info,unsafeWindow,cloneInto,createObjectIn,exportFunction}';
 const expose = {};
 let isApplied;
@@ -317,7 +316,6 @@ function prepareScript(script) {
   const code = this.code[id];
   const dataKey = getUniqId('VMin');
   const displayName = getScriptName(script);
-  const name = encodeURIComponent(displayName.replace(BAD_URL_CHAR, replaceWithFullWidthForm));
   const isContent = isContentRealm(script, forceContent);
   const pathMap = custom.pathMap || {};
   const reqs = meta.require.map(key => require[pathMap[key] || key]).filter(Boolean);
@@ -347,8 +345,7 @@ function prepareScript(script) {
     wrap && `})()${IS_FIREFOX ? `}catch(e){${dataKey}(e)}` : ''}}`,
     // 0 at the end to suppress errors about non-cloneable result of executeScript in FF
     IS_FIREFOX && ';0',
-    // Firefox lists .user.js among our own content scripts so a space at start will group them
-    `\n//# sourceURL=${extensionRoot}${IS_FIREFOX ? '%20' : ''}${name}.user.js#${id}`,
+    `\n//# sourceURL=${getScriptPrettyUrl(script, displayName)}`,
   ]::trueJoin('');
   cache.put(dataKey, injectedCode, TIME_KEEP_DATA);
   /** @type {VMInjection.Script} */
@@ -367,11 +364,6 @@ function prepareScript(script) {
   ];
 }
 
-function replaceWithFullWidthForm(s) {
-  // fullwidth range starts at 0xFF00, normal range starts at space char code 0x20
-  return String.fromCharCode(s.charCodeAt(0) - 0x20 + 0xFF00);
-}
-
 const resolveDataCodeStr = `(${function _(data) {
   /* `function` is required to compile `this`, and `this` is required because our safe-globals
    * shadows `window` so its name is minified and hence inaccessible here */

+ 1 - 1
src/background/utils/requests-core.js

@@ -1,7 +1,7 @@
 import { buffer2string, getUniqId, isEmpty, noop } from '@/common';
+import { extensionOrigin } from '@/common/consts';
 import { forEachEntry } from '@/common/object';
 import ua from '@/common/ua';
-import { extensionOrigin } from './init';
 
 let encoder;
 

+ 1 - 1
src/background/utils/tab-redirector.js

@@ -1,7 +1,7 @@
 import { request, noop, i18n, getUniqId } from '@/common';
+import { extensionRoot } from '@/common/consts';
 import ua from '@/common/ua';
 import cache from './cache';
-import { extensionRoot } from './init';
 import { commands } from './message';
 import { parseMeta, isUserScript } from './script';
 

+ 1 - 1
src/background/utils/tabs.js

@@ -1,6 +1,6 @@
 import { getActiveTab, noop, sendTabCmd, getFullUrl } from '@/common';
+import { extensionRoot } from '@/common/consts';
 import ua from '@/common/ua';
-import { extensionRoot } from './init';
 import { commands } from './message';
 import { getOption } from './options';
 

+ 3 - 2
src/common/consts.js

@@ -32,5 +32,6 @@ export const TIMEOUT_HOUR = 60 * 60 * 1000;
 export const TIMEOUT_24HOURS = 24 * 60 * 60 * 1000;
 export const TIMEOUT_WEEK = 7 * 24 * 60 * 60 * 1000;
 
-export const ICON_PREFIX = !process.env.IS_INJECTED
-  && browser.runtime.getURL('/public/images/icon');
+export const extensionRoot = !process.env.IS_INJECTED && browser.runtime.getURL('/') || '';
+export const extensionOrigin = extensionRoot.slice(0, -1);
+export const ICON_PREFIX = `${extensionRoot}public/images/icon`;

+ 20 - 1
src/common/index.js

@@ -1,6 +1,6 @@
 // SAFETY WARNING! Exports used by `injected` must make ::safe() calls and use __proto__:null
 
-import { browser, ICON_PREFIX } from '@/common/consts';
+import { browser, extensionRoot, ICON_PREFIX } from '@/common/consts';
 import { deepCopy } from './object';
 import { blob2base64, i18n, isDataUri, noop } from './util';
 
@@ -18,6 +18,10 @@ if (process.env.DEV && process.env.IS_INJECTED !== 'injected-web') {
 }
 
 export const defaultImage = `${ICON_PREFIX}128.png`;
+/** Will be encoded to avoid splitting the URL in devtools UI */
+const BAD_URL_CHAR = /[#/?]/g;
+/** Fullwidth range starts at 0xFF00, normal range starts at space char code 0x20 */
+const replaceWithFullWidthForm = s => String.fromCharCode(s.charCodeAt(0) - 0x20 + 0xFF00);
 
 export function initHooks() {
   const hooks = [];
@@ -158,6 +162,21 @@ export function getScriptName(script) {
     || `#${script.props.id ?? i18n('labelNoName')}`;
 }
 
+/** URL that shows the name of the script and opens in devtools sources or in our editor */
+export function getScriptPrettyUrl(script, displayName) {
+  return `${
+    extensionRoot
+  }${
+    // When called from prepareScript, adding a space to group scripts in one block visually
+    displayName && IS_FIREFOX ? '%20' : ''
+  }${
+    encodeURIComponent((displayName || getScriptName(script))
+    .replace(BAD_URL_CHAR, replaceWithFullWidthForm))
+  }.user.js#${
+    script.props.id
+  }`;
+}
+
 /**
  * @param {VMScript} script
  * @param {boolean} [all] - to return all two urls (1: check, 2: download)