Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/app/service/content/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,33 @@ export class InjectRuntime {

externalMessage() {
// 对外接口白名单
const msg = this.msg;
for (let i = 0; i < ExternalWhitelist.length; i += 1) {
if (window.location.host.endsWith(ExternalWhitelist[i])) {
// 注入
(<{ external: any }>(<unknown>window)).external = window.external || {};
(<
{
external: {
Scriptcat: {
isInstalled: (name: string, namespace: string, callback: any) => void;
};
};
}
>(<unknown>window)).external.Scriptcat = {
async isInstalled(name: string, namespace: string, callback: any) {
const resp = await sendMessage(msg, "content/script/isInstalled", {
name,
namespace,
});
callback(resp);
},
};
(<{ external: { Tampermonkey: any } }>(<unknown>window)).external.Tampermonkey = (<
{ external: { Scriptcat: any } }
>(<unknown>window)).external.Scriptcat;
break;
const hostname = window.location.hostname;
if (
ExternalWhitelist.some(
// 如果当前页面的 hostname 是白名单的网域或其子网域
(t) => hostname.endsWith(t) && (hostname.length === t.length || hostname.endsWith(`.${t}`))
)
) {
const msg = this.msg;
// 注入
const external: External = window.external || (window.external = {} as External);
const scriptExpose: App.ExternalScriptCat = {
isInstalled(name: string, namespace: string, callback: (res: App.IsInstalledResponse | undefined) => unknown) {
sendMessage<App.IsInstalledResponse>(msg, "content/script/isInstalled", {
name,
namespace,
}).then(callback);
},
};
try {
external.Scriptcat = scriptExpose;
} catch {
// 无法注入到 external,忽略
}
try {
external.Tampermonkey = scriptExpose;
} catch {
// 无法注入到 external,忽略
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/app/service/service_worker/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,15 @@ export class ScriptService {
}
}

isInstalled({ name, namespace }: { name: string; namespace: string }) {
isInstalled({ name, namespace }: { name: string; namespace: string }): Promise<App.IsInstalledResponse> {
return this.scriptDAO.findByNameAndNamespace(name, namespace).then((script) => {
if (script) {
return { installed: true, version: script.metadata.version && script.metadata.version[0] };
return {
installed: true,
version: script.metadata.version && script.metadata.version[0],
} as App.IsInstalledResponse;
}
return { installed: false };
return { installed: false } as App.IsInstalledResponse;
});
}

Expand Down
47 changes: 47 additions & 0 deletions src/app/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
declare namespace App {
// window.external

export type ExternalTampermonkey = {
isInstalled(name: string, namespace: string, callback: (res: IsInstalledResponse | undefined) => unknown): void;
getVersion?: (callback: (res: GetVersionResponse | undefined) => unknown) => unknown;
openOptions?: (p1?: unknown, p2?: unknown) => unknown;
};

export type ExternalViolentmonkey = {
isInstalled(name: string, namespace: string): Promise<unknown>;
};

// GreasyFork项目中FireMonkey扩展的脚本与样式管理机制解析
// https://blog.csdn.net/gitblog_07112/article/details/148466939
export type ExternalFireMonkey = {
version: string;
} & (
| {
installedScriptVersion: string;
}
| {
installedStyleVersion: string;
}
| {
installedCSSVersion: string;
}
)?;

export type ExternalScriptCat = {
isInstalled(name: string, namespace: string, callback: (res: IsInstalledResponse | undefined) => unknown): void;
};

export type IsInstalledResponse =
| {
installed: true;
version: string | undefined;
}
| {
installed: false;
};

export type GetVersionResponse = {
version?: string;
id?: string | undefined;
};
}
13 changes: 13 additions & 0 deletions src/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare module "@App/types/scriptcat.d.ts";
declare module "*.tpl";
declare module "*.json";
declare module "*.yaml";
declare module "@App/app/types.d.ts";

declare const sandbox: Window;

Expand Down Expand Up @@ -64,3 +65,15 @@ declare namespace GMSend {
filename?: string;
}
}

declare namespace globalThis {
interface Window {
external?: External;
}
interface External {
Tampermonkey?: App.ExternalTampermonkey;
Violentmonkey?: App.ExternalViolentmonkey;
FireMonkey?: App.ExternalFireMonkey;
Scriptcat?: App.ExternalScriptCat;
}
}