Skip to content

Commit e3b6604

Browse files
committed
🐛 修复vscode连接问题
1 parent 94d9926 commit e3b6604

6 files changed

Lines changed: 34 additions & 9 deletions

File tree

example/gm_xhr.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ GM_xmlhttpRequest({
2525
method: "POST",
2626
responseType: "blob",
2727
data: data,
28+
cookie: "ceshi=123",
29+
anonymous: true,
2830
headers: {
2931
referer: "http://www.example.com/",
3032
origin: "www.example.com",

src/app/service/offscreen/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class OffscreenManager {
5555

5656
const gmApi = new GMApi(this.windowServer.group("gmApi"));
5757
gmApi.init();
58-
const vscodeConnect = new VSCodeConnect(this.windowServer.group("vscodeConnect"));
58+
const vscodeConnect = new VSCodeConnect(this.windowServer.group("vscodeConnect"), this.extensionMessage);
5959
vscodeConnect.init();
6060

6161
this.windowServer.on("createObjectURL", (data: Blob) => {

src/app/service/offscreen/vscode-connect.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import LoggerCore from "@App/app/logger/core";
22
import Logger from "@App/app/logger/logger";
3-
import { prepareScriptByCode } from "@App/pkg/utils/script";
4-
import { Group } from "@Packages/message/server";
3+
import { Group, MessageSend } from "@Packages/message/server";
4+
import { ScriptClient } from "../service_worker/client";
5+
import { v5 as uuidv5 } from "uuid";
56

67
// 在offscreen下与scriptcat-vscode建立websocket连接
78
// 需要在vscode中安装scriptcat-vscode插件
@@ -14,7 +15,14 @@ export class VSCodeConnect {
1415

1516
connectVSCodeTimer: any;
1617

17-
constructor(private group: Group) {}
18+
scriptClient: ScriptClient;
19+
20+
constructor(
21+
private group: Group,
22+
private send: MessageSend
23+
) {
24+
this.scriptClient = new ScriptClient(this.send);
25+
}
1826

1927
connect({ url, reconnect }: { url: string; reconnect: boolean }) {
2028
// 如果已经连接,断开重连
@@ -64,9 +72,8 @@ export class VSCodeConnect {
6472
switch (data.action) {
6573
case "onchange": {
6674
// 调用安装脚本接口
67-
// const code = data.data.script;
68-
// const prepareScript = await prepareScriptByCode(code, "", uuidv5(data.data.uri, uuidv5.URL), true);
69-
// this.scriptManager.event.upsertHandler(prepareScript.script, "vscode");
75+
const code = data.data.script;
76+
this.scriptClient.installByCode(uuidv5(data.data.uri, uuidv5.URL), code, "vscode");
7077
break;
7178
}
7279
default:

src/app/service/service_worker/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export class ScriptClient extends Client {
8989
return this.do("importByUrl", url);
9090
}
9191

92+
installByCode(uuid: string, code: string, upsertBy: InstallSource = "user") {
93+
return this.do("installByCode", { uuid, code, upsertBy });
94+
}
95+
9296
async formatUrl(url: string) {
9397
try {
9498
const newUrl = new URL(url.replace(/\/$/, ""));

src/app/service/service_worker/script.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fetchScriptInfo, prepareScriptByCode } from "@App/pkg/utils/script";
2-
import { v4 as uuidv4 } from "uuid";
2+
import { v4 as uuidv4, v5 as uuidv5 } from "uuid";
33
import { Group } from "@Packages/message/server";
44
import Logger from "@App/app/logger/logger";
55
import LoggerCore from "@App/app/logger/core";
@@ -174,6 +174,17 @@ export class ScriptService {
174174
return Promise.resolve(prepareScript.script);
175175
}
176176

177+
// 直接通过code静默安装脚本
178+
async installByCode(param: { uuid: string; code: string; upsertBy: InstallSource }) {
179+
const prepareScript = await prepareScriptByCode(param.code, "", param.uuid, true);
180+
this.installScript({
181+
script: prepareScript.script,
182+
code: param.code,
183+
upsertBy: param.upsertBy,
184+
});
185+
return Promise.resolve(prepareScript.script);
186+
}
187+
177188
// 获取安装信息
178189
getInstallInfo(uuid: string) {
179190
return Cache.getInstance().get(CacheKey.scriptInstallInfo(uuid));
@@ -563,6 +574,7 @@ export class ScriptService {
563574
this.group.on("isInstalled", this.isInstalled.bind(this));
564575
this.group.on("sortScript", this.sortScript.bind(this));
565576
this.group.on("importByUrl", this.importByUrl.bind(this));
577+
this.group.on("installByCode", this.installByCode.bind(this));
566578

567579
// 定时检查更新, 每10分钟检查一次
568580
chrome.alarms.create("checkScriptUpdate", {

src/app/service/service_worker/system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class SystemService {
1616
// 如果开启了自动连接vscode,则自动连接
1717
// 使用tx来确保service_worker恢复时不会再执行
1818
Cache.getInstance().tx("vscodeReconnect", async (init) => {
19-
if (init) {
19+
if (!init) {
2020
if (await this.systemConfig.getVscodeReconnect()) {
2121
// 调用连接
2222
vscodeConnect.connect({

0 commit comments

Comments
 (0)