Browse Source

Merge pull request #633 from tophf/auto-updatable-template

improvement: auto-updatable default script template
Gerald 6 years ago
parent
commit
851b58ce60

+ 3 - 5
src/background/index.js

@@ -6,7 +6,7 @@ import {
   getRequestId, httpRequest, abortRequest, confirmInstall,
   newScript, parseMeta,
   setClipboard, checkUpdate,
-  getOption, getDefaultOption, setOption, hookOptions, getAllOptions,
+  getOption, setOption, hookOptions, getAllOptions,
   initialize, sendMessageOrIgnore,
 } from './utils';
 import { tabOpen, tabClose } from './utils/tabs';
@@ -22,15 +22,13 @@ import {
   setValueStore, updateValueStore, resetValueOpener, addValueOpener,
 } from './utils/values';
 import { setBadge } from './utils/icon';
+import { SCRIPT_TEMPLATE, resetScriptTemplate } from './utils/template-hook';
 
 const VM_VER = browser.runtime.getManifest().version;
 
 hookOptions((changes) => {
   if ('autoUpdate' in changes) autoUpdate();
-  const SCRIPT_TEMPLATE = 'scriptTemplate';
-  if (SCRIPT_TEMPLATE in changes && !changes[SCRIPT_TEMPLATE]) {
-    setOption(SCRIPT_TEMPLATE, getDefaultOption(SCRIPT_TEMPLATE));
-  }
+  if (SCRIPT_TEMPLATE in changes) resetScriptTemplate(changes);
   sendMessageOrIgnore({
     cmd: 'UpdateOptions',
     data: changes,

+ 10 - 4
src/background/utils/options.js

@@ -31,12 +31,18 @@ const defaults = {
   },
   scriptTemplate: `\
 // ==UserScript==
-// @name New Script
-// @namespace Violentmonkey Scripts
-// @match {{url}}
-// @grant none
+// @name        New script {{name}}
+// @namespace   Violentmonkey Scripts
+// @match       {{url}}
+// @grant       none
+// @version     1.0
+// @author      -
+// @description {{date}}
 // ==/UserScript==
 `,
+  // Enables automatic updates to the default template with new versions of VM
+  /** @type {?Boolean} this must be |null| for template-hook.js upgrade routine */
+  scriptTemplateEdited: null,
 };
 let changes = {};
 const hooks = initHooks();

+ 2 - 0
src/background/utils/script.js

@@ -75,6 +75,8 @@ export function getDefaultCustom() {
 export function newScript(data) {
   const state = {
     url: '*://*/*',
+    name: '',
+    date: new Date().toLocaleString(),
     ...data,
   };
   const code = getOption('scriptTemplate')

+ 39 - 0
src/background/utils/template-hook.js

@@ -0,0 +1,39 @@
+import { getDefaultOption, getOption, setOption } from './options';
+
+export const SCRIPT_TEMPLATE = 'scriptTemplate';
+const SCRIPT_TEMPLATE_EDITED = `${SCRIPT_TEMPLATE}Edited`;
+const INITIAL_TEMPLATE = `\
+// ==UserScript==
+// @name New Script
+// @namespace Violentmonkey Scripts
+// @match {{url}}
+// @grant none
+// ==/UserScript==
+`;
+
+// When updating from an old version, set the edited flag retroactively
+// TODO: remove this in 2020 as the majority of users will have an updated VM
+global.addEventListener('backgroundInitialized', () => {
+  if (getOption(SCRIPT_TEMPLATE_EDITED) == null) {
+    if (getOption(SCRIPT_TEMPLATE) === INITIAL_TEMPLATE) {
+      resetScriptTemplate();
+    } else {
+      setOption(SCRIPT_TEMPLATE_EDITED, true);
+    }
+  }
+}, { once: true });
+
+export function resetScriptTemplate(changes = {}) {
+  const defaultTemplate = getDefaultOption(SCRIPT_TEMPLATE);
+  let template = changes[SCRIPT_TEMPLATE];
+  if (!template) {
+    template = defaultTemplate;
+    changes[SCRIPT_TEMPLATE] = template;
+    setOption(SCRIPT_TEMPLATE, template);
+  }
+  const edited = template !== defaultTemplate;
+  if (edited !== changes[SCRIPT_TEMPLATE_EDITED]) {
+    changes[SCRIPT_TEMPLATE_EDITED] = edited;
+    setOption(SCRIPT_TEMPLATE_EDITED, edited);
+  }
+}

+ 1 - 1
src/common/ui/setting-text.vue

@@ -1,5 +1,5 @@
 <template>
-  <textarea v-model="value" @change="onChange" :disabled="disabled" />
+  <textarea class="monospace-font" v-model="value" @change="onChange" :disabled="disabled" />
 </template>
 
 <script>

+ 4 - 0
src/common/ui/style/style.css

@@ -242,6 +242,10 @@ button,
 
 .editor-code .CodeMirror {
   height: 100%;
+}
+
+.monospace-font,
+.editor-code .CodeMirror {
   /* Use `Courier New` to ensure `&nbsp;` has the same width as an original space. */
   font-family: "Fira Code", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", Courier, monospace;
 }

+ 1 - 0
src/popup/views/app.vue

@@ -222,6 +222,7 @@ export default {
           cmd: 'CacheNewScript',
           data: {
             url: currentTab.url.split('#')[0].split('?')[0],
+            name: `- ${domain}`,
           },
         })
       ) : Promise.resolve())