Browse Source

fix: click on notification + deduplicate tld usage

side effect: moves 11kB (15%) from common.js to bg/index.js
tophf 5 years ago
parent
commit
ff8b0ae8a6
4 changed files with 24 additions and 31 deletions
  1. 12 1
      src/background/index.js
  2. 4 14
      src/background/utils/tabs.js
  3. 6 13
      src/popup/index.js
  4. 2 3
      src/popup/views/app.vue

+ 12 - 1
src/background/index.js

@@ -1,6 +1,7 @@
-import { makePause, sendCmd } from '#/common';
+import { getActiveTab, makePause, sendCmd } from '#/common';
 import { TIMEOUT_24HOURS, TIMEOUT_MAX } from '#/common/consts';
 import { deepCopy, forEachEntry, objectSet } from '#/common/object';
+import * as tld from '#/common/tld';
 import ua from '#/common/ua';
 import * as sync from './sync';
 import { commands } from './utils';
@@ -82,6 +83,16 @@ Object.assign(commands, {
     }
     return res;
   },
+  /** @return {Promise<Object>} */
+  async GetTabDomain() {
+    const tab = await getActiveTab() || {};
+    const url = tab.pendingUrl || tab.url || '';
+    const host = url.match(/^https?:\/\/([^/]+)|$/)[1];
+    return {
+      tab,
+      domain: host && tld.getDomain(host) || host,
+    };
+  },
   /**
    * Timers in content scripts are shared with the web page so it can clear them.
    * await sendCmd('SetTimeout', 100) in injected/content

+ 4 - 14
src/background/utils/tabs.js

@@ -1,5 +1,4 @@
 import { getActiveTab, noop, sendTabCmd, getFullUrl } from '#/common';
-import * as tld from '#/common/tld';
 import ua from '#/common/ua';
 import { extensionRoot } from './init';
 import { commands } from './message';
@@ -10,23 +9,14 @@ const openers = {};
 Object.assign(commands, {
   /**
    * @param {string} [pathId] - path or id to add to #scripts route in dashboard,
-     if absent a new script will be created for `url` and `domain`
-   * @param {string} [url] - url to create a new script for, defaults to active tab's url
-   * @param {string} [domain] - url domain part, if absent it's extracted from url
+     if absent a new script will be created for active tab's URL
    * @returns {Promise<{id: number}>}
    */
-  async OpenEditor({ pathId, url, domain } = {}) {
+  async OpenEditor(pathId) {
     if (!pathId) {
-      if (!url) {
-        const tab = await getActiveTab() || {};
-        url = tab.pendingUrl || tab.url || '';
-      }
-      if (!domain) {
-        domain = url.match(/^https?:\/\/([^/]+)|$/)[1];
-        domain = domain && tld.getDomain(domain) || domain;
-      }
+      const { tab, domain } = await commands.GetTabDomain();
       const id = domain && commands.CacheNewScript({
-        url: url.split(/[#?]/)[0],
+        url: (tab.pendingUrl || tab.url).split(/[#?]/)[0],
         name: `- ${domain}`,
       });
       pathId = `_new${id ? `/${id}` : ''}`;

+ 6 - 13
src/popup/index.js

@@ -1,16 +1,14 @@
 import Vue from 'vue';
-import { getActiveTab, i18n, sendCmdDirectly } from '#/common';
+import { i18n, sendCmdDirectly } from '#/common';
 import { INJECT_PAGE, INJECTABLE_TAB_URL_RE } from '#/common/consts';
 import handlers from '#/common/handlers';
 import { loadScriptIcon } from '#/common/load-script-icon';
 import { forEachValue, mapEntry } from '#/common/object';
-import * as tld from '#/common/tld';
 import '#/common/ui/style';
 import App from './views/app';
 import { mutex, store } from './utils';
 
 mutex.init();
-tld.initTLD();
 Vue.prototype.i18n = i18n;
 
 const vm = new Vue({
@@ -63,19 +61,14 @@ if (!CSS.supports?.('list-style-type', 'disclosure-open')) {
   document.styleSheets[0].insertRule('.excludes-menu ::-webkit-details-marker {display:none}');
 }
 
-getActiveTab()
-.then(async (tab) => {
-  const { url } = tab;
+sendCmdDirectly('GetTabDomain')
+.then(async ({ tab, domain }) => {
   store.currentTab = tab;
+  store.domain = domain;
   browser.runtime.connect({ name: `${tab.id}` });
-  if (/^https?:\/\//i.test(url)) {
-    const matches = url.match(/:\/\/([^/]*)/);
-    const domain = matches[1];
-    store.domain = tld.getDomain(domain) || domain;
-  }
-  if (!INJECTABLE_TAB_URL_RE.test(url)) {
+  if (!INJECTABLE_TAB_URL_RE.test(tab.url)) {
     store.injectable = false;
   } else {
-    store.blacklisted = await sendCmdDirectly('TestBlacklist', url);
+    store.blacklisted = await sendCmdDirectly('TestBlacklist', tab.url);
   }
 });

+ 2 - 3
src/popup/views/app.vue

@@ -295,7 +295,7 @@ export default {
       window.close();
     },
     onEditScript(item) {
-      sendCmd('OpenEditor', { pathId: item.data.props.id });
+      sendCmd('OpenEditor', item.data.props.id);
       window.close();
     },
     onFindSameDomainScripts() {
@@ -330,8 +330,7 @@ export default {
       }
     },
     async onCreateScript() {
-      const { currentTab: { url }, domain } = this.store;
-      sendCmd('OpenEditor', { url, domain });
+      sendCmd('OpenEditor');
       window.close();
     },
     async onInjectionFailureFix() {