Skip to content

Commit 9ea0708

Browse files
committed
♻️ 调整打开更新页面代码
1 parent 25cec66 commit 9ea0708

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/app/service/service_worker/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { SystemService } from "./system";
1616
import { type Logger, LoggerDAO } from "@App/app/repo/logger";
1717
import { localePath, t } from "@App/locales/locales";
1818
import { InfoNotification } from "@App/pkg/utils/utils";
19+
import { isVideoPlayingOrInactive } from "./utils";
1920

2021
// service worker的管理器
2122
export default class ServiceWorkerManager {
@@ -158,20 +159,12 @@ export default class ServiceWorkerManager {
158159
if (details.reason === "install") {
159160
chrome.tabs.create({ url: "https://docs.scriptcat.org" + localePath });
160161
} else if (details.reason === "update") {
161-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
162-
const lastError = chrome.runtime.lastError;
163-
if (lastError) {
164-
console.error("chrome.runtime.lastError in chrome.tabs.query:", lastError);
165-
// 查詢 tabs 失敗不进行后续处理
166-
}
167-
// 如果有激活窗口, 则打开更新文档
168-
// 如果当前窗口正在播放 audio, 则在后台打开
169-
const active = tabs.length === 0 ? false : tabs[0].audible === true ? false : true;
162+
isVideoPlayingOrInactive().then((ok) => {
170163
chrome.tabs.create({
171164
url: `https://docs.scriptcat.org/docs/change/${
172165
ExtVersion.includes("-") ? "beta-changelog/" : ""
173166
}#${ExtVersion}`,
174-
active: active,
167+
active: !ok,
175168
});
176169
InfoNotification(t("ext_update_notification"), t("ext_update_notification_desc", { version: ExtVersion }));
177170
});

src/app/service/service_worker/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,22 @@ export function parseUrlSRI(url: string): {
111111

112112
return { url: urls[0], hash };
113113
}
114+
115+
// 检查是否正在播放视频,或者窗口未激活
116+
export function isVideoPlayingOrInactive() {
117+
return new Promise<boolean>((resolve) => {
118+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
119+
const lastError = chrome.runtime.lastError;
120+
if (lastError) {
121+
console.error("chrome.runtime.lastError in chrome.tabs.query:", lastError);
122+
resolve(false);
123+
return;
124+
}
125+
if (tabs.length === 0 || tabs[0].audible === true || !tabs[0].active) {
126+
resolve(true);
127+
} else {
128+
resolve(false);
129+
}
130+
});
131+
});
132+
}

0 commit comments

Comments
 (0)