Browse Source

refactor: inline VM_VER + always expose VM on greasyfork

tophf 6 years ago
parent
commit
70281f56d5

+ 3 - 0
scripts/webpack.conf.js

@@ -7,10 +7,13 @@ const projectConfig = require('./plaid.conf');
 const mergedConfig = shallowMerge(defaultOptions, projectConfig);
 
 const INIT_FUNC_NAME = 'VMInitInjection';
+// Copied from gulpfile.js: strip alphabetic suffix
+const VM_VER = require('../package.json').version.replace(/-[^.]*/, '');
 
 const definitions = new webpack.DefinePlugin({
   'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
   'process.env.DEBUG': JSON.stringify(process.env.DEBUG || false),
+  'process.env.VM_VER': JSON.stringify(VM_VER),
 });
 const minimizerOptions = {
   cache: true,

+ 0 - 3
src/background/index.js

@@ -19,7 +19,6 @@ import './utils/tabs';
 import './utils/tester';
 import './utils/update';
 
-const VM_VER = browser.runtime.getManifest().version;
 const popupTabs = {}; // { tabId: 1 }
 let isApplied;
 let injectInto;
@@ -40,7 +39,6 @@ Object.assign(commands, {
   async GetData() {
     const data = await getData();
     data.sync = sync.getStates();
-    data.version = VM_VER;
     return data;
   },
   /** @return {Promise<Object>} */
@@ -54,7 +52,6 @@ Object.assign(commands, {
       ua,
       isFirefox: ua.isFirefox,
       isPopupShown: popupTabs[tab.id],
-      version: VM_VER,
     };
     if (isApplied) {
       const key = getPreinjectKey(url, isTop);

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

@@ -88,7 +88,7 @@ Object.assign(commands, {
 
 preInitialize.push(async () => {
   const { version: lastVersion } = await browser.storage.local.get('version');
-  const { version } = browser.runtime.getManifest();
+  const version = process.env.VM_VER;
   if (!lastVersion) await patchDB();
   if (version !== lastVersion) browser.storage.local.set({ version });
   const data = await browser.storage.local.get();

+ 1 - 1
src/injected/web/gm-wrapper.js

@@ -75,7 +75,7 @@ function makeGmInfo({ config, meta, props }, code, resources, injectInto) {
     scriptMetaStr: code::match(METABLOCK_RE)[1] || '',
     scriptWillUpdate: !!config.shouldUpdate,
     scriptHandler: 'Violentmonkey',
-    version: bridge.version,
+    version: process.env.VM_VER,
     injectInto,
     platform: { ...bridge.ua },
     script: {

+ 17 - 0
src/injected/web/index.js

@@ -1,5 +1,6 @@
 import { INJECT_PAGE, INJECT_CONTENT } from '#/common/consts';
 import { bindEvents } from '../utils';
+import { defineProperty } from '../utils/helpers';
 import bridge from './bridge';
 import store from './store';
 import './gm-values';
@@ -31,6 +32,9 @@ export default function initialize(
         bridge.post('Pong');
       },
     });
+    if (window.location.host === 'greasyfork.org') {
+      exposeVM();
+    }
   }
   document.addEventListener('DOMContentLoaded', async () => {
     store.state = 1;
@@ -49,3 +53,16 @@ bridge.addHandlers({
     bridge.callbacks[callbackId]?.(payload);
   },
 });
+
+function exposeVM() {
+  const Violentmonkey = {};
+  defineProperty(Violentmonkey, 'version', {
+    value: process.env.VM_VER,
+  });
+  defineProperty(Violentmonkey, 'isInstalled', {
+    value: (name, namespace) => bridge.send('CheckScript', { name, namespace }),
+  });
+  defineProperty(window.external, 'Violentmonkey', {
+    value: Violentmonkey,
+  });
+}

+ 0 - 19
src/injected/web/load-scripts.js

@@ -20,12 +20,6 @@ bridge.addHandlers({
     const end = [];
     bridge.isFirefox = data.isFirefox;
     bridge.ua = data.ua;
-    bridge.version = data.version;
-    if ([
-      'greasyfork.org',
-    ].includes(window.location.host)) {
-      exposeVM();
-    }
     // reset load and checkLoad
     bridge.load = () => {
       bridge.load = noop;
@@ -114,16 +108,3 @@ bridge.addHandlers({
     }
   },
 });
-
-function exposeVM() {
-  const Violentmonkey = {};
-  defineProperty(Violentmonkey, 'getVersion', {
-    value: async () => ({ version: bridge.version }),
-  });
-  defineProperty(Violentmonkey, 'isInstalled', {
-    value: (name, namespace) => bridge.send('CheckScript', { name, namespace }),
-  });
-  defineProperty(window.external, 'Violentmonkey', {
-    value: Violentmonkey,
-  });
-}

+ 1 - 1
src/options/views/tab-about.vue

@@ -26,7 +26,7 @@
 
 <script>
 const data = {
-  version: browser.runtime.getManifest().version,
+  version: process.env.VM_VER,
   language: browser.i18n.getUILanguage(),
 };