From 429dd55657ef50f20231c9e414f1b24c2cad1d80 Mon Sep 17 00:00:00 2001 From: CodFrm Date: Fri, 14 Jan 2022 15:24:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=BB=BA=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E9=A1=B5=E9=9D=A2URL=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/background.ts | 2 +- src/template/normal.tpl | 7 +- src/types/main.d.ts | 1 + src/views/components/ResizableEditor.vue | 11 ++- src/views/pages/Option/index.tsx | 27 ++++-- .../pages/Option/tabs/ScriptTab/Editor.vue | 9 +- .../pages/Option/tabs/ScriptTab/index.vue | 2 + src/views/pages/Popup/index.vue | 85 +++++++++++-------- 9 files changed, 96 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 37128b16..7780d09b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scriptcat", - "version": "0.8.0", + "version": "0.8.1", "description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!", "scripts": { "lint": "eslint --ext .js,.ts,.tsx tests/ src/", diff --git a/src/background.ts b/src/background.ts index 6a59b71f..a3b2e3db 100644 --- a/src/background.ts +++ b/src/background.ts @@ -10,7 +10,7 @@ import { migrate } from './model/migrate'; import { SCRIPT_STATUS_ENABLE, Script, SCRIPT_STATUS_DISABLE } from './model/do/script'; import { MapCache } from './pkg/storage/cache/cache'; import { get } from './pkg/utils/utils'; -import { ExtVersion, Server } from './apps/config'; +import { Server } from './apps/config'; import { Subscribe, SUBSCRIBE_STATUS_ENABLE } from './model/do/subscribe'; import { UserManager } from './apps/user/manager'; import { ToolsManager } from './apps/tools/manager'; diff --git a/src/template/normal.tpl b/src/template/normal.tpl index 86a9ebda..4058475c 100644 --- a/src/template/normal.tpl +++ b/src/template/normal.tpl @@ -4,6 +4,11 @@ // @version 0.1.0 // @description try to take over the world! // @author You +// @match {{match}} // ==/UserScript== -// 在此处键入代码…… +(function() { + 'use strict'; + + // Your code here... +})(); \ No newline at end of file diff --git a/src/types/main.d.ts b/src/types/main.d.ts index fa3f00d5..db95c1fe 100644 --- a/src/types/main.d.ts +++ b/src/types/main.d.ts @@ -45,6 +45,7 @@ interface INewScript { scriptId: number; tabKey: string | number; template?: 'normal' | 'crontab' | 'background'; + param?: AnyMap } interface IUpdateMeta { diff --git a/src/views/components/ResizableEditor.vue b/src/views/components/ResizableEditor.vue index 00cef7d7..373080b9 100644 --- a/src/views/components/ResizableEditor.vue +++ b/src/views/components/ResizableEditor.vue @@ -36,6 +36,7 @@ export default class ResizableEditor extends Vue { // 可以选择默认模板 @Prop() template!: 'normal' | 'crontab' | 'background'; // @Prop({ default: "crontab" }) template!: "normal" | "crontab" | "background"; + @Prop() param?: AnyMap; // 页面上存在多个editor实例时,contentKeyService会报错 uniqueEditorId = `container${String(Math.random()).slice(2)}`; @@ -75,7 +76,7 @@ export default class ResizableEditor extends Vue { return; } - let template: typeof crontabTpl; + let template: string; switch (this.template) { case 'normal': @@ -90,6 +91,14 @@ export default class ResizableEditor extends Vue { template = backgroundTpl; break; } + console.log(this.param); + for (const key in this.param) { + template = template.replaceAll( + '{{' + key + '}}', + this.param[key] + ); + } + template = template.replaceAll(/\s+\/\/\s?@.*?\s+{{.*?}}/g, ''); this.editor = editor.create(edit, { language: this.language, diff --git a/src/views/pages/Option/index.tsx b/src/views/pages/Option/index.tsx index 906d8e23..6d60f6fb 100644 --- a/src/views/pages/Option/index.tsx +++ b/src/views/pages/Option/index.tsx @@ -67,6 +67,7 @@ export default class App extends Vue { generateScriptTab( scriptId: number, template: 'normal' | 'crontab' | 'background' = 'crontab', + param?: AnyMap ): ITabItem { const tabKey = Math.random(); @@ -81,7 +82,7 @@ export default class App extends Vue { height: '100%', }} > - + ), closable: true, @@ -183,7 +184,21 @@ export default class App extends Vue { this.handleEditScript({ scriptId: parseInt(query.id as string) }); } else if (query?.target === 'initial') { // 新建脚本 - eventBus.$emit(EventType.NewScript, { template: 'normal' } as any); + chrome.storage.local.get(["activeTabUrl"], (items) => { + console.log(items); + if (items['activeTabUrl']) { + chrome.storage.local.remove('activeTabUrl'); + eventBus.$emit(EventType.NewScript, { + template: 'normal', param: { + match: items['activeTabUrl'].url + } + } as any); + } else { + eventBus.$emit(EventType.NewScript, { + template: 'normal' + } as any); + } + }); } }); // deubg用的bg @@ -253,20 +268,16 @@ export default class App extends Vue { } /** 在原PlusTab中保存了脚本时触发 */ - handleNewScript({ scriptId, tabKey, template }: INewScript) { + handleNewScript({ scriptId, tabKey, template, param }: INewScript) { console.log('handleNewScript'); // 需要知道是哪一个tab触发了保存事件,因为可能同时打开了多个“新建脚本” const scriptTabIndex = this.allTabs.findIndex((tab) => tab.tabKey == tabKey); - console.log({ - scriptTabIndex, - }); - this.updateTab({ // -1时,说明目前并没有打开“新建脚本”,只有固定的3个基础页面,需要新开,即initial index: scriptTabIndex === -1 ? this.allTabs.length : scriptTabIndex, - newTab: this.generateScriptTab(scriptId, template), + newTab: this.generateScriptTab(scriptId, template, param), }); this.$nextTick(() => { diff --git a/src/views/pages/Option/tabs/ScriptTab/Editor.vue b/src/views/pages/Option/tabs/ScriptTab/Editor.vue index 38c8537d..ea028572 100644 --- a/src/views/pages/Option/tabs/ScriptTab/Editor.vue +++ b/src/views/pages/Option/tabs/ScriptTab/Editor.vue @@ -56,7 +56,11 @@
- +
@@ -98,6 +102,7 @@ export default class CloseButton extends Vue { @Prop() scriptId!: number; @Prop() script!: Script; @Prop() template!: 'normal' | 'crontab' | 'background'; + @Prop() param?: AnyMap; @Prop() onMetaChange!: boolean; hasInitial = false; @@ -122,7 +127,7 @@ export default class CloseButton extends Vue { var file = fileInput!.files![0]; var reader = new FileReader(); let _this = this; - reader.onload = function () { + reader.onload = function() { _this.editor.setValue(this.result); }; reader.readAsText(file, 'utf-8'); diff --git a/src/views/pages/Option/tabs/ScriptTab/index.vue b/src/views/pages/Option/tabs/ScriptTab/index.vue index c6112bdc..91e3874e 100644 --- a/src/views/pages/Option/tabs/ScriptTab/index.vue +++ b/src/views/pages/Option/tabs/ScriptTab/index.vue @@ -14,6 +14,7 @@ :script="script" :scriptId="scriptId" :template="template" + :param="param" :onMetaChange="onMetaChange" @initial-script="handleInitialSctipt" @save-script="handleSaveScript" @@ -92,6 +93,7 @@ export default class ScriptTab extends Vue { script: Script =