Переглянути джерело

refactor: reuse URL constants

tophf 1 рік тому
батько
коміт
42fadf760a

+ 2 - 2
src/background/sync/dropbox.js

@@ -1,5 +1,5 @@
 import { dumpQuery, getUniqId, loadQuery } from '@/common';
-import { FORM_URLENCODED } from '@/common/consts';
+import { FORM_URLENCODED, VM_HOME } from '@/common/consts';
 import {
   getURI, getItemFilename, BaseService, isScriptFile, register,
   openAuthPage,
@@ -9,7 +9,7 @@ import {
 
 const config = {
   client_id: process.env.SYNC_DROPBOX_CLIENT_ID,
-  redirect_uri: 'https://violentmonkey.github.io/auth_dropbox.html',
+  redirect_uri: VM_HOME + 'auth_dropbox.html',
 };
 
 const escRE = /[\u007f-\uffff]/g; // eslint-disable-line no-control-regex

+ 1 - 1
src/background/sync/googledrive.js

@@ -18,7 +18,7 @@ const config = {
   // Google OAuth for native app only allows loopback IP address for callback URL.
   // The URL will be intercepted and blocked so the port doesn't matter.
   redirect_uri: 'http://127.0.0.1:45678/',
-  // redirect_uri: 'https://violentmonkey.github.io/auth_googledrive.html',
+  // redirect_uri: VM_HOME + 'auth_googledrive.html',
   scope: 'https://www.googleapis.com/auth/drive.appdata',
 };
 const UNAUTHORIZED = { status: 'UNAUTHORIZED' };

+ 2 - 2
src/background/sync/onedrive.js

@@ -1,6 +1,6 @@
 // Reference: https://dev.onedrive.com/README.htm
 import { dumpQuery, noop } from '@/common';
-import { FORM_URLENCODED } from '@/common/consts';
+import { FORM_URLENCODED, VM_HOME } from '@/common/consts';
 import { objectGet } from '@/common/object';
 import {
   getURI, getItemFilename, BaseService, isScriptFile, register,
@@ -10,7 +10,7 @@ import {
 const config = {
   client_id: process.env.SYNC_ONEDRIVE_CLIENT_ID,
   client_secret: process.env.SYNC_ONEDRIVE_CLIENT_SECRET,
-  redirect_uri: 'https://violentmonkey.github.io/auth_onedrive.html',
+  redirect_uri: VM_HOME + 'auth_onedrive.html',
 };
 
 const OneDrive = BaseService.extend({

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

@@ -582,8 +582,8 @@ export async function updateScriptInfo(id, data) {
 function parseMetaWithErrors(src) {
   const isObj = isObject(src);
   const custom = isObj && src.custom || getDefaultCustom();
-  const meta = parseMeta(isObj ? src.code : src);
   const errors = [];
+  const meta = parseMeta(isObj ? src.code : src, false, errors);
   if (meta) {
     testerBatch(errors);
     testScript('', { meta, custom });

+ 2 - 0
src/common/consts.js

@@ -41,3 +41,5 @@ export const UA_PROPS = ['userAgent', 'brands', 'mobile', 'platform'];
 export const TL_AWAIT = 'topLevelAwait';
 export const UNWRAP = 'unwrap';
 export const FETCH_OPTS = 'fetchOpts';
+export const VM_HOME = 'https://violentmonkey.github.io/';
+export const VM_DOCS_MATCHING = VM_HOME + 'api/matching/';

+ 6 - 1
src/common/ui/index.js

@@ -3,6 +3,7 @@ import Modal from 'vueleton/lib/modal';
 import { trueJoin } from '@/common';
 import { i18n } from '@/common/util';
 import Message from './message';
+import { VM_HOME } from '@/common/consts';
 
 /** Showing unexpected errors in UI so that the users can notify us */
 addEventListener('error', e => showUnhandledError(e.error));
@@ -160,7 +161,11 @@ export const getActiveElement = () => document.activeElement;
 /** @param {MouseEvent|KeyboardEvent} e */
 export const hasKeyModifiers = e => e.shiftKey || e.ctrlKey || e.metaKey || e.altKey;
 export const externalEditorInfoUrl =
-  'https://violentmonkey.github.io/posts/how-to-edit-scripts-with-your-favorite-editor/';
+  VM_HOME + 'posts/how-to-edit-scripts-with-your-favorite-editor/';
+export const EXTERNAL_LINK_PROPS = {
+  target: '_blank',
+  rel: 'noopener noreferrer',
+};
 const { getAsFileSystemHandle } = DataTransferItem.prototype;
 
 if (getAsFileSystemHandle) {

+ 9 - 2
src/options/views/edit/help.vue

@@ -2,8 +2,7 @@
   <div class="edit-help mb-2c">
     <div>
       <h3 v-html="i18n('editHelpDocumention')"/>
-      <a href="https://violentmonkey.github.io/api/"
-         rel="noopener noreferrer" target="_blank">violentmonkey.github.io/api/</a>
+      <a :href="API_URL" v-bind="EXTERNAL_LINK_PROPS" v-text="API_TEXT"/>
     </div>
     <div class="keyboard">
       <h3 v-text="i18n('editHelpKeyboard')"/>
@@ -15,6 +14,14 @@
   </div>
 </template>
 
+<script>
+import { VM_HOME } from '@/common/consts';
+import { EXTERNAL_LINK_PROPS } from '@/common/ui';
+
+const API_URL = VM_HOME + 'api/';
+const API_TEXT = API_URL.split('://')[1];
+</script>
+
 <script setup>
 defineProps({
   hotkeys: Array

+ 4 - 4
src/options/views/edit/index.vue

@@ -17,8 +17,7 @@
          class="text-upper text-right text-red"/>
       <div v-else class="edit-hint text-right ellipsis">
         <a :href="externalEditorInfoUrl"
-           target="_blank"
-           rel="noopener noreferrer"
+           v-bind="EXTERNAL_LINK_PROPS"
            v-text="i18n('editHowToHint')"/>
       </div>
       <div class="mr-1">
@@ -83,7 +82,7 @@
       </locale-group>
       <p v-for="e in errors" :key="e" v-text="e" class="text-red"/>
       <p class="my-1" v-if="errors">
-        <a :href="urlMatching" target="_blank" rel="noopener noreferrer" v-text="urlMatching"/>
+        <a :href="VM_DOCS_MATCHING" v-bind="EXTERNAL_LINK_PROPS" v-text="VM_DOCS_MATCHING"/>
       </p>
     </div>
   </div>
@@ -95,17 +94,18 @@ import {
   debounce, formatByteLength, getScriptName, getScriptUpdateUrl, i18n, isEmpty,
   nullBool2string, sendCmdDirectly, trueJoin,
 } from '@/common';
+import { VM_DOCS_MATCHING } from '@/common/consts';
 import { deepCopy, deepEqual, objectPick } from '@/common/object';
 import { externalEditorInfoUrl, focusMe, getActiveElement, showMessage } from '@/common/ui';
 import { keyboardService } from '@/common/keyboard';
 import options from '@/common/options';
 import { getUnloadSentry } from '@/common/router';
+import { EXTERNAL_LINK_PROPS } from '@/common/ui';
 import {
   kDownloadURL, kExclude, kExcludeMatch, kHomepageURL, kIcon, kInclude, kMatch, kName, kOrigExclude, kOrigExcludeMatch,
   kOrigInclude, kOrigMatch, kUpdateURL,
 } from '../../utils';
 
-const urlMatching = 'https://violentmonkey.github.io/api/matching/';
 const CUSTOM_PROPS = {
   [kName]: '',
   [kHomepageURL]: '',

+ 2 - 3
src/options/views/script-item.vue

@@ -112,8 +112,7 @@
                  :disabled="!url" :content="title" align="start">
           <a
             class="btn-ghost"
-            target="_blank"
-            rel="noopener noreferrer"
+            v-bind="EXTERNAL_LINK_PROPS"
             :href="url"
             :tabIndex="url ? tabIndex : -1">
             <icon :name="icon"/>
@@ -147,7 +146,7 @@
 
 <script>
 import { formatTime, getLocaleString, getScriptHome, getScriptSupportUrl, i18n } from '@/common';
-import { getActiveElement, showConfirmation } from '@/common/ui';
+import { EXTERNAL_LINK_PROPS, getActiveElement, showConfirmation } from '@/common/ui';
 import { isInput, keyboardService, toggleTip } from '@/common/keyboard';
 import { kDescription, store, TOGGLE_OFF, TOGGLE_ON } from '../utils';
 

+ 20 - 10
src/options/views/tab-about.vue

@@ -8,31 +8,41 @@
     <div>
       <label v-text="i18n('labelRelated')"></label>
       <ul>
-        <li><a href="https://violentmonkey.github.io" target="_blank" rel="noopener noreferrer" v-text="i18n('labelHomepage')"></a></li>
-        <li><a href="https://github.com/violentmonkey/violentmonkey/issues" target="_blank" rel="noopener noreferrer" v-text="i18n('labelFeedback')"></a></li>
-        <li><a href="https://github.com/violentmonkey/violentmonkey/graphs/contributors" target="_blank" rel="noopener noreferrer" v-text="i18n('labelContributors')"></a></li>
-        <li><a href="https://violentmonkey.github.io/privacy/" target="_blank" rel="noopener noreferrer" v-text="i18n('labelPrivacyPolicy')"></a></li>
+        <li v-for="(text, url) in LINKS" :key="url">
+          <a :href="url" v-bind="EXTERNAL_LINK_PROPS" v-text="text"/>
+        </li>
       </ul>
     </div>
     <div>
       <label v-text="i18n('labelCurrentLang')"></label>
       <span class="current" v-text="language"></span> |
-      <a href="https://violentmonkey.github.io/localization/" target="_blank" rel="noopener noreferrer" v-text="i18n('labelHelpTranslate')"></a>
+      <a :href="VM_HOME + 'localization/'" v-bind="EXTERNAL_LINK_PROPS" v-text="i18n('labelHelpTranslate')"/>
     </div>
   </div>
 </template>
 
 <script setup>
+import { i18n } from '@/common';
+import { VM_HOME } from '@/common/consts';
+import { EXTERNAL_LINK_PROPS } from '@/common/ui';
+
 const name = extensionManifest.name;
 const version = process.env.VM_VER;
 const language = browser.i18n.getUILanguage();
+const GITHUB = 'https://github.com/violentmonkey/violentmonkey/';
+const LINKS = {
+  [VM_HOME]: i18n('labelHomepage'),
+  [GITHUB + 'issues']: i18n('labelFeedback'),
+  [GITHUB + 'graphs/contributors']: i18n('labelContributors'),
+  [VM_HOME + 'privacy/']: i18n('labelPrivacyPolicy'),
+};
 </script>
 
 <style>
-  .current {
-    color: green;
-    @media (prefers-color-scheme: dark) {
-      color: greenyellow;
-    }
+.current {
+  color: green;
+  @media (prefers-color-scheme: dark) {
+    color: greenyellow;
   }
+}
 </style>

+ 3 - 3
src/options/views/tab-installed.vue

@@ -20,8 +20,8 @@
                   tabindex="0"
                   @click.prevent="handleEditScript('_new')"
                 />
-                <a class="dropdown-menu-item" v-text="i18n('installFrom', 'OpenUserJS')" href="https://openuserjs.org/" target="_blank" rel="noopener noreferrer"></a>
-                <a class="dropdown-menu-item" v-text="i18n('installFrom', 'GreasyFork')" href="https://greasyfork.org/scripts" target="_blank" rel="noopener noreferrer"></a>
+                <a class="dropdown-menu-item" v-text="i18n('installFrom', 'OpenUserJS')" href="https://openuserjs.org/" v-bind="EXTERNAL_LINK_PROPS"/>
+                <a class="dropdown-menu-item" v-text="i18n('installFrom', 'GreasyFork')" href="https://greasyfork.org/scripts" v-bind="EXTERNAL_LINK_PROPS"/>
                 <a
                   class="dropdown-menu-item"
                   v-text="i18n('buttonInstallFromURL')"
@@ -172,7 +172,7 @@
 import { computed, reactive, nextTick, onMounted, watch, ref, onBeforeUnmount } from 'vue';
 import { i18n, sendCmdDirectly, debounce, ensureArray, makePause, trueJoin } from '@/common';
 import options from '@/common/options';
-import { getActiveElement, isTouch, showConfirmation, showMessage, vFocus } from '@/common/ui';
+import { EXTERNAL_LINK_PROPS, getActiveElement, isTouch, showConfirmation, showMessage, vFocus } from '@/common/ui';
 import hookSetting from '@/common/hook-setting';
 import { forEachKey } from '@/common/object';
 import { setRoute, lastRoute } from '@/common/router';

+ 4 - 4
src/options/views/tab-settings/index.vue

@@ -54,7 +54,7 @@
             <select v-for="opt in ['defaultInjectInto']" v-model="settings[opt]" :key="opt">
               <option v-for="(_, mode) in items[opt]" :key="mode" v-text="mode" />
             </select>
-            <a class="ml-1" href="https://violentmonkey.github.io/posts/inject-into-context/" target="_blank" rel="noopener noreferrer" v-text="i18n('learnInjectionMode')"></a>
+            <a class="ml-1" :href="VM_HOME + 'posts/inject-into-context/'" v-bind="EXTERNAL_LINK_PROPS" v-text="i18n('learnInjectionMode')"/>
           </label>
           <tooltip :content="i18n('labelXhrInjectHint')">
             <setting-check name="xhrInject">
@@ -77,7 +77,7 @@
             <setting-check v-for="([key, host]) in expose" :key="host"
                            :name="`expose.${key}`" class="ml-2 mr-1c valign-tb">
               <span v-text="host" />
-              <a :href="`https://${host}`" target="_blank" rel="noopener noreferrer">&nearr;</a>
+              <a :href="`https://${host}`" v-bind="EXTERNAL_LINK_PROPS">&nearr;</a>
             </setting-check>
           </locale-group>
         </div>
@@ -110,11 +110,11 @@
 
 <script>
 import { i18n } from '@/common';
-import { KNOWN_INJECT_INTO } from '@/common/consts';
+import { KNOWN_INJECT_INTO, VM_HOME } from '@/common/consts';
 import options from '@/common/options';
 import { kUpdateEnabledScriptsOnly } from '@/common/options-defaults';
 import { keyboardService } from '@/common/keyboard';
-import { focusMe, getActiveElement } from '@/common/ui';
+import { EXTERNAL_LINK_PROPS, focusMe, getActiveElement } from '@/common/ui';
 import { hookSettingsForUI } from '@/common/ui/util';
 
 const items = {

+ 4 - 3
src/options/views/tab-settings/vm-blacklist.vue

@@ -2,8 +2,8 @@
   <section>
     <h3 v-text="i18n('labelBlacklist')"/>
     <p>
-      <a href="https://violentmonkey.github.io/posts/smart-rules-for-blacklist/#blacklist-patterns"
-         v-text="i18n('learnBlacklist')" target="_blank" rel="noopener noreferrer"/>
+      <a :href="VM_HOME + 'posts/smart-rules-for-blacklist/#blacklist-patterns'"
+         v-text="i18n('learnBlacklist')" v-bind="EXTERNAL_LINK_PROPS"/>
     </p>
     <VmBlacklistBody :name="BLACKLIST" :desc="i18n('descBlacklist')"/>
     <VmBlacklistBody :name="BLACKLIST_NET" :desc="i18n('descBlacklistNet')"/>
@@ -11,7 +11,8 @@
 </template>
 
 <script>
-import { BLACKLIST, BLACKLIST_NET } from '@/common/consts';
+import { BLACKLIST, BLACKLIST_NET, VM_HOME } from '@/common/consts';
+import { EXTERNAL_LINK_PROPS } from '@/common/ui';
 </script>
 
 <script setup>

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

@@ -147,7 +147,7 @@
               <summary><icon name="info"/></summary>
               <small>{{i18n('menuExcludeHint')}} {{i18n('labelRelated')}}<a
                 v-text="i18n('labelExcludeMatch')" target="_blank"
-                href="https://violentmonkey.github.io/api/matching/"/>
+                :href="VM_DOCS_MATCHING"/>
               </small>
             </details>
           </div>
@@ -197,7 +197,7 @@
     <div v-if="extras" ref="$extras" class="extras-menu">
       <a v-for="[url, text] in activeLinks"
          :key="url" :href="url" :data-message="url" tabindex="0" v-text="text"
-         rel="noopener noreferrer" target="_blank"/>
+         v-bind="EXTERNAL_LINK_PROPS"/>
       <div v-text="i18n('menuExclude')" tabindex="0" @click="onExclude"/>
       <div v-text="extras.data.config.removed ? i18n('buttonRestore') : i18n('buttonRemove')"
            tabindex="0"
@@ -212,6 +212,7 @@
 
 <script setup>
 import { computed, nextTick, onActivated, onMounted, reactive, ref } from 'vue';
+import { VM_DOCS_MATCHING } from '@/common/consts';
 import options from '@/common/options';
 import optionsDefaults, {
   kFiltersPopup, kPopupWidth, kUpdateEnabledScriptsOnly,
@@ -222,7 +223,7 @@ import {
 } from '@/common';
 import handlers from '@/common/handlers';
 import { objectPick } from '@/common/object';
-import { getActiveElement } from '@/common/ui';
+import { EXTERNAL_LINK_PROPS, getActiveElement } from '@/common/ui';
 import Icon from '@/common/ui/icon';
 import SettingsPopup from '@/common/ui/settings-popup.vue';
 import { keyboardService, isInput, handleTabNavigation } from '@/common/keyboard';