Sfoglia il codice sorgente

fix: read active tab's URL for newScript hotkey (#1215)

tophf 5 anni fa
parent
commit
f0d4df246c
3 ha cambiato i file con 33 aggiunte e 16 eliminazioni
  1. 6 8
      src/background/utils/hotkeys.js
  2. 24 1
      src/background/utils/tabs.js
  3. 3 7
      src/popup/views/app.vue

+ 6 - 8
src/background/utils/hotkeys.js

@@ -1,15 +1,13 @@
 import { postInitialize } from './init';
 import { commands } from './message';
 
-const ROUTES = {
-  newScript: '#scripts/_new',
-  settings: '#settings',
-};
-
 postInitialize.push(() => {
   browser.commands.onCommand.addListener((cmd) => {
-    commands.TabOpen({
-      url: `${browser.runtime.getManifest().options_ui.page}${ROUTES[cmd] || ''}`,
-    });
+    if (cmd === 'newScript') {
+      commands.OpenEditor();
+    } else {
+      const route = cmd === 'settings' ? `#${cmd}` : '';
+      commands.TabOpen({ url: `/options/index.html${route}` });
+    }
   });
 });

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

@@ -1,4 +1,5 @@
 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';
@@ -7,7 +8,29 @@ import { getOption } from './options';
 const openers = {};
 
 Object.assign(commands, {
-  OpenEditor(pathId) {
+  /**
+   * @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
+   * @returns {Promise<{id: number}>}
+   */
+  async OpenEditor({ pathId, url, domain } = {}) {
+    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 id = domain && commands.CacheNewScript({
+        url: url.split(/[#?]/)[0],
+        name: `- ${domain}`,
+      });
+      pathId = `_new${id ? `/${id}` : ''}`;
+    }
     return commands.TabOpen({
       url: `/options/index.html#scripts/${pathId}`,
       maybeInWindow: true,

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

@@ -295,7 +295,7 @@ export default {
       window.close();
     },
     onEditScript(item) {
-      sendCmd('OpenEditor', item.data.props.id);
+      sendCmd('OpenEditor', { pathId: item.data.props.id });
       window.close();
     },
     onFindSameDomainScripts() {
@@ -330,12 +330,8 @@ export default {
       }
     },
     async onCreateScript() {
-      const { currentTab, domain } = this.store;
-      const id = domain && await sendCmd('CacheNewScript', {
-        url: currentTab.url.split(/[#?]/)[0],
-        name: `- ${domain}`,
-      });
-      sendCmd('OpenEditor', `_new${id ? `/${id}` : ''}`);
+      const { currentTab: { url }, domain } = this.store;
+      sendCmd('OpenEditor', { url, domain });
       window.close();
     },
     async onInjectionFailureFix() {