Skip to content

Commit 08deb84

Browse files
committed
Revert "🐛 区分网页安装标签来源"
This reverts commit eb6dd93.
1 parent eb6dd93 commit 08deb84

4 files changed

Lines changed: 16 additions & 38 deletions

File tree

src/app/service/service_worker/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ScriptClient extends Client {
5252

5353
// 获取安装信息
5454
getInstallInfo(uuid: string) {
55-
return this.do<[boolean, ScriptInfo, { byWebRequest?: boolean; openedInNewTab?: boolean }]>("getInstallInfo", uuid);
55+
return this.do<[boolean, ScriptInfo, { byWebRequest?: boolean }]>("getInstallInfo", uuid);
5656
}
5757

5858
install(params: TScriptInstallParam): Promise<TScriptInstallReturn> {

src/app/service/service_worker/script.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ export type TScriptInstallReturn = {
7474
updatetime: number | undefined; // 实际生效的更新时间(时间戳,毫秒)
7575
};
7676

77-
type InstallPageOptions = {
78-
source: InstallSource;
79-
byWebRequest?: boolean;
80-
openedInNewTab?: boolean;
81-
};
82-
8377
export type TRestoreResult = {
8478
restored: string[];
8579
conflicts: { uuid: string; name: string }[];
@@ -143,7 +137,7 @@ export class ScriptService {
143137
// 读取脚本url内容, 进行安装
144138
const logger = this.logger.with({ url: targetUrl });
145139
logger.debug("install script");
146-
this.openInstallPageByUrl(targetUrl, { source: "user", byWebRequest: true, openedInNewTab: true })
140+
this.openInstallPageByUrl(targetUrl, { source: "user", byWebRequest: true })
147141
.catch((e) => {
148142
logger.error("install script error", Logger.E(e));
149143
// 不再重定向当前url
@@ -367,7 +361,7 @@ export class ScriptService {
367361

368362
public async openInstallPageByUrl(
369363
url: string,
370-
options: InstallPageOptions
364+
options: { source: InstallSource; byWebRequest?: boolean }
371365
): Promise<{ success: boolean; msg: string }> {
372366
try {
373367
const installPageUrl = await this.getInstallPageUrl(url, options);
@@ -380,7 +374,10 @@ export class ScriptService {
380374
}
381375
}
382376

383-
public async getInstallPageUrl(url: string, options: InstallPageOptions): Promise<string> {
377+
public async getInstallPageUrl(
378+
url: string,
379+
options: { source: InstallSource; byWebRequest?: boolean }
380+
): Promise<string> {
384381
const uuid = uuidv4();
385382
try {
386383
await this.openUpdateOrInstallPage(uuid, url, options, false);
@@ -1169,7 +1166,7 @@ export class ScriptService {
11691166
async openUpdateOrInstallPage(
11701167
uuid: string,
11711168
url: string,
1172-
options: InstallPageOptions,
1169+
options: { source: InstallSource; byWebRequest?: boolean },
11731170
update: boolean,
11741171
logger?: Logger
11751172
) {

src/pages/install/useInstallData.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,6 @@ describe("useInstallData 数据流编排", () => {
304304
expect(closeSpy).not.toHaveBeenCalled();
305305
});
306306

307-
it("webNavigation 新开标签即使带 byWebRequest 且 history.length > 1 也应关闭", async () => {
308-
const result = await setupReady({ byWebRequest: true, openedInNewTab: true });
309-
const closeSpy = vi.spyOn(window, "close").mockImplementation(() => {});
310-
const backSpy = vi.spyOn(window.history, "back").mockImplementation(() => {});
311-
vi.spyOn(window.history, "length", "get").mockReturnValue(2);
312-
313-
await act(async () => {
314-
await result.current.install();
315-
await new Promise((r) => setTimeout(r, 320));
316-
});
317-
318-
expect(closeSpy).toHaveBeenCalledOnce();
319-
expect(backSpy).not.toHaveBeenCalled();
320-
});
321-
322307
it("byWebRequest 但 history.length 为 1 时应关闭无处可退的标签", async () => {
323308
const result = await setupReady({ byWebRequest: true });
324309
const closeSpy = vi.spyOn(window, "close").mockImplementation(() => {});

src/pages/install/useInstallData.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,18 @@ const buildScriptInfo = (uuid: string, code: string, url: string, metadata: SCMe
125125
});
126126

127127
// 安装页可能是专为安装打开的新标签,也可能由 declarativeNetRequest 接管用户原标签。
128-
// webNavigation 入口同样带有 byWebRequest 标记,但它通过 chrome.tabs.create() 打开独立标签,
129-
// 因此必须额外保留 openedInNewTab 来源;只有真正接管用户原标签且确实有历史可退时才返回
128+
// 独立新标签可能继承多条历史,DNR 入口也可能没有上一页,因此必须同时检查入口与历史栈:
129+
// 仅在 DNR 接管且确实有历史可退时返回,否则关闭当前独立安装标签
130130
// install()/close() 等可能在短时间内被重复触发(如用户连续点击、close 与 install 的
131131
// setTimeout 前后脚打到),leaveInstallPageRunning 防止 back()/close() 被并发调用多次;
132132
// 推到 requestAnimationFrame 里执行,让触发它的那次交互(如按钮点击态)先完成一帧渲染。
133133
let leaveInstallPageRunning = false;
134-
const leaveInstallPage = (byWebRequest: boolean, openedInNewTab: boolean) => {
134+
const leaveInstallPage = (byWebRequest: boolean) => {
135135
if (leaveInstallPageRunning) return;
136136
leaveInstallPageRunning = true;
137137
requestAnimationFrame(() => {
138138
leaveInstallPageRunning = false;
139-
if (byWebRequest && !openedInNewTab && window.history.length > 1) {
139+
if (byWebRequest && window.history.length > 1) {
140140
window.history.back();
141141
} else {
142142
window.close();
@@ -191,7 +191,6 @@ export function useInstallData(): UseInstallData {
191191
const handleRef = useRef<FileSystemFileHandle | null>(null);
192192
const skillUuidRef = useRef<string | null>(null);
193193
const byWebRequestRef = useRef(false);
194-
const openedInNewTabRef = useRef(false);
195194

196195
useEffect(() => {
197196
const params = new URLSearchParams(location.search);
@@ -202,7 +201,6 @@ export function useInstallData(): UseInstallData {
202201
const urlIdx = location.search.indexOf("url=");
203202
const rawUrl = !uuid && urlIdx !== -1 ? location.search.slice(urlIdx + 4) : null;
204203
byWebRequestRef.current = params.get("byWebRequest") === "1";
205-
openedInNewTabRef.current = false;
206204
let cancelled = false;
207205

208206
const failed = (e: unknown) => {
@@ -269,7 +267,6 @@ export function useInstallData(): UseInstallData {
269267
if (code === undefined) throw new Error(t("install:script_info_load_failed"));
270268
info.code = code;
271269
byWebRequestRef.current = cached?.[2]?.byWebRequest === true;
272-
openedInNewTabRef.current = cached?.[2]?.openedInNewTab === true;
273270
await loadFromInfo(info, !!cached?.[0], cached?.[2] || {});
274271
} else if (rawUrl) {
275272
// .cat.md URL → Skill 安装流程(DNR 把 *.cat.md 重定向到安装页),不走脚本解析;仅 agent 启用时
@@ -374,8 +371,7 @@ export function useInstallData(): UseInstallData {
374371
await scriptClient.install({ script, code: info.code });
375372
notify.success(t("install:success"));
376373
}
377-
if (closeAfterInstall)
378-
setTimeout(() => leaveInstallPage(byWebRequestRef.current, openedInNewTabRef.current), 300);
374+
if (closeAfterInstall) setTimeout(() => leaveInstallPage(byWebRequestRef.current), 300);
379375
} catch (e) {
380376
notify.error(`${t("install:failed")}: ${(e as Error)?.message || String(e)}`);
381377
}
@@ -399,7 +395,7 @@ export function useInstallData(): UseInstallData {
399395
if (opts?.noMoreUpdates && info && !info.userSubscribe) {
400396
void scriptClient.setCheckUpdateUrl(info.uuid, false);
401397
}
402-
leaveInstallPage(byWebRequestRef.current, openedInNewTabRef.current);
398+
leaveInstallPage(byWebRequestRef.current);
403399
}, []);
404400

405401
// 监听文件变更后自动重装,并刷新视图代码
@@ -456,7 +452,7 @@ export function useInstallData(): UseInstallData {
456452
try {
457453
await agentClient.completeSkillInstall(uuid);
458454
notify.success(t("install:success"));
459-
setTimeout(() => leaveInstallPage(byWebRequestRef.current, openedInNewTabRef.current), 300);
455+
setTimeout(() => leaveInstallPage(byWebRequestRef.current), 300);
460456
} catch (e) {
461457
notify.error(`${t("install:failed")}: ${(e as Error)?.message || String(e)}`);
462458
}
@@ -465,7 +461,7 @@ export function useInstallData(): UseInstallData {
465461
const cancelSkill = useCallback(() => {
466462
const uuid = skillUuidRef.current;
467463
if (uuid) void agentClient.cancelSkillInstall(uuid);
468-
leaveInstallPage(byWebRequestRef.current, openedInNewTabRef.current);
464+
leaveInstallPage(byWebRequestRef.current);
469465
}, []);
470466

471467
// 重新触发加载(供加载失败后的重试按钮)

0 commit comments

Comments
 (0)