diff --git a/build/pack.js b/build/pack.js index eb2e1a43..f20de9b0 100644 --- a/build/pack.js +++ b/build/pack.js @@ -33,16 +33,16 @@ str = str.replace(/"version": "(.*?)"/, `"version": "${manifest.version}"`); fs.writeFileSync("./src/manifest.json", str); // 处理configSystem version -let configSystem = fs.readFileSync("./src/pkg/config/config.ts").toString(); +let configSystem = fs.readFileSync("./src/app/const.ts").toString(); // 如果是由github action的分支触发的构建,在版本中再加上commit id if (process.env.GITHUB_REF_TYPE === "branch") { package.version += `+${process.env.GITHUB_SHA.substring(0, 7)}`; } configSystem = configSystem.replace( - /\/\* version \*\/ "(.*?)"/, - `/* version */ "${package.version}"` + /ExtVersion = "(.*?)";/, + `ExtVersion = "${package.version}";` ); -fs.writeFileSync("./src/pkg/config/config.ts", configSystem); +fs.writeFileSync("./src/app/const.ts", configSystem); execSync("npm run build", { stdio: "inherit" }); diff --git a/src/app/const.ts b/src/app/const.ts new file mode 100644 index 00000000..dcc508fb --- /dev/null +++ b/src/app/const.ts @@ -0,0 +1,12 @@ +export const ExtVersion = "0.10.0-alpha.1"; + +export const Server = "https://sc.icodef.com/"; + +export const ExternalWhitelist = [ + "greasyfork.org", + "scriptcat.org", + "tampermonkey.net.cn", + "openuserjs.org", +]; + +export const ExternalMessage = "externalMessage"; diff --git a/src/app/service/system/manager.ts b/src/app/service/system/manager.ts index 0ac8bcb3..26dd2b50 100644 --- a/src/app/service/system/manager.ts +++ b/src/app/service/system/manager.ts @@ -1,5 +1,7 @@ +import { ExternalMessage, ExtVersion, Server } from "@App/app/const"; import IoC from "@App/app/ioc"; import { MessageHander } from "@App/app/message/message"; +import { ScriptDAO } from "@App/app/repo/scripts"; import { SystemConfig } from "@App/pkg/config/config"; import Manager from "../manager"; @@ -8,17 +10,18 @@ import Manager from "../manager"; export class SystemManager extends Manager { systemConfig: SystemConfig; + scriptDAO: ScriptDAO; + constructor(message: MessageHander, systemConfig: SystemConfig) { - super(message); + super(message, "system"); + this.scriptDAO = new ScriptDAO(); this.systemConfig = systemConfig; } init() { // 两小时检查一次更新 const checkUpdate = () => { - fetch( - `${this.systemConfig.server}api/v1/system/version?version=${this.systemConfig.version}` - ) + fetch(`${Server}api/v1/system/version?version=${ExtVersion}`) .then((resp) => resp.json()) .then((resp: { data: { notice: string; version: string } }) => { chrome.storage.local.get(["notice"], (items) => { @@ -50,6 +53,33 @@ export class SystemManager extends Manager { } }); } + // 处理 + this.message.setHandler( + ExternalMessage, + async ( + _action, + { + action, + name, + namespace, + }: { action: string; name: string; namespace: string } + ) => { + if (action === "isInstalled") { + const script = await this.scriptDAO.findByNameAndNamespace( + name, + namespace + ); + if (script) { + return Promise.resolve({ + installed: true, + version: script.metadata.version && script.metadata.version[0], + }); + } + return Promise.resolve({ installed: false }); + } + return Promise.resolve(false); + } + ); } getNotice(): Promise<{ notice: string; isRead: boolean }> { diff --git a/src/inject.ts b/src/inject.ts index f918fb3c..1a5a4077 100644 --- a/src/inject.ts +++ b/src/inject.ts @@ -18,8 +18,6 @@ const logger = new LoggerCore({ message.setHandler("pageLoad", (_action, data) => { logger.logger().debug("inject start"); - - const runtime = new InjectRuntime(data.scripts, flag); - + const runtime = new InjectRuntime(message, data.scripts, flag); runtime.start(); }); diff --git a/src/pages/components/ScriptStorage/index.tsx b/src/pages/components/ScriptStorage/index.tsx index 0af9a343..36cc917d 100644 --- a/src/pages/components/ScriptStorage/index.tsx +++ b/src/pages/components/ScriptStorage/index.tsx @@ -45,7 +45,6 @@ const ScriptStorage: React.FC<{ // 监听值变化 const channel = valueCtrl.watchValue(script); channel.setHandler((value: Value) => { - console.log("value changed", value); setData((prev) => { const index = prev.findIndex((item) => item.key === value.key); if (index === -1) { diff --git a/src/pages/components/UserConfigPanel/index.tsx b/src/pages/components/UserConfigPanel/index.tsx index c899af04..32aa4083 100644 --- a/src/pages/components/UserConfigPanel/index.tsx +++ b/src/pages/components/UserConfigPanel/index.tsx @@ -159,7 +159,7 @@ const UserConfigPanel: React.FC<{ placeholder={item.description} > {options!.map((option) => ( - + {option} ))} diff --git a/src/pages/components/layout/MainLayout.tsx b/src/pages/components/layout/MainLayout.tsx index 204e3a35..eb1f0518 100644 --- a/src/pages/components/layout/MainLayout.tsx +++ b/src/pages/components/layout/MainLayout.tsx @@ -75,17 +75,17 @@ const MainLayout: React.FC<{ - + 添加后台脚本 - + 添加定时脚本 - + 链接导入 diff --git a/src/pages/components/layout/Sider.tsx b/src/pages/components/layout/Sider.tsx index 6aa91ffc..af92ef63 100644 --- a/src/pages/components/layout/Sider.tsx +++ b/src/pages/components/layout/Sider.tsx @@ -26,7 +26,7 @@ if (!hash.length) { const Sider: React.FC = () => { const [menuSelect, setMenuSelect] = useState(hash); - const [collapsed, setCollapsed] = useState(true); + const [collapsed, setCollapsed] = useState(false); return ( diff --git a/src/pages/options/routes/Logger.tsx b/src/pages/options/routes/Logger.tsx index af9a1f9a..af878e97 100644 --- a/src/pages/options/routes/Logger.tsx +++ b/src/pages/options/routes/Logger.tsx @@ -147,9 +147,6 @@ function Subscribe() { ))} + , + ); })} @@ -517,9 +521,9 @@ function ScriptEditor() { > 已安装脚本 - {scriptList.map((script, index) => ( + {scriptList.map((script) => (