Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 0 additions & 5 deletions src/app/service/offscreen/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ export default class GMApi {
});
}

async openInTab({ url }: { url: string }) {
return window.open(url) !== undefined;
}

textarea: HTMLTextAreaElement = document.createElement("textarea");

clipboardData: { type?: string; data: string } | undefined;
Expand Down Expand Up @@ -210,7 +206,6 @@ export default class GMApi {
});

this.group.on("xmlHttpRequest", this.xmlHttpRequest.bind(this));
this.group.on("openInTab", this.openInTab.bind(this));
this.group.on("setClipboard", this.setClipboard.bind(this));
}
}
92 changes: 39 additions & 53 deletions src/app/service/service_worker/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { cacheInstance } from "@App/app/cache";
import EventEmitter from "eventemitter3";
import { type RuntimeService } from "./runtime";
import { getIcon, isFirefox, getCurrentTab, openInCurrentTab, cleanFileName } from "@App/pkg/utils/utils";

Check failure on line 15 in src/app/service/service_worker/gm_api.ts

View workflow job for this annotation

GitHub Actions / Run tests

'getCurrentTab' is defined but never used. Allowed unused vars must match /^_/u
import { type SystemConfig } from "@App/pkg/config/config";
import i18next, { i18nName } from "@App/locales/locales";
import FileSystemFactory from "@Packages/filesystem/factory";
Expand Down Expand Up @@ -852,61 +852,47 @@
const url = request.params[0];
const options = request.params[1];
const getNewTabId = async () => {
if (options.useOpen === true) {
// 发送给offscreen页面处理 (使用window.open)
const ok = await sendMessage(this.msgSender, "offscreen/gmApi/openInTab", { url });
if (ok) {
// 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签
const tab = await getCurrentTab();
return tab.id;
} else {
// 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭
// 似乎在Firefox中禁止在background页面使用window.open(),强制返回null
return false;
}
} else {
const { tabId, windowId } = sender.getExtMessageSender();
const active = options.active;
const currentTab = await chrome.tabs.get(tabId);
let newTabIndex = -1;
if (options.incognito && !currentTab.incognito) {
// incognito: "split" 在 normal 里不会看到 incognito
// 只能创建新 incognito window
// pinned 无效
// insert 不重要
await chrome.windows.create({
url,
incognito: true,
focused: active,
});
return 0;
}
if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) {
// insert 为 boolean 时,插入至当前Tab下一格 (TM行为)
// insert 为 number 时,插入至相对位置 (SC独自)
const insert = +options.insert;
newTabIndex = currentTab.index + insert;
if (newTabIndex < 0) newTabIndex = 0;
}
const createProperties = {
const { tabId, windowId } = sender.getExtMessageSender();
const active = options.active;
const currentTab = await chrome.tabs.get(tabId);
let newTabIndex = -1;
if (options.incognito && !currentTab.incognito) {
// incognito: "split" 在 normal 里不会看到 incognito
// 只能创建新 incognito window
// pinned 无效
// insert 不重要
await chrome.windows.create({
url,
active: active,
} as chrome.tabs.CreateProperties;
if (options.setParent) {
// SC 预设 setParent: true 以避免不可预计的问题
createProperties.openerTabId = tabId === -1 ? undefined : tabId;
createProperties.windowId = windowId === -1 ? undefined : windowId;
}
if (options.pinned) {
// VM/FM行为
createProperties.pinned = true;
} else if (newTabIndex >= 0) {
// insert option; pinned 情况下无效
createProperties.index = newTabIndex;
}
const tab = await chrome.tabs.create(createProperties);
return tab.id;
incognito: true,
focused: active,
});
return 0;
}
if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) {
// insert 为 boolean 时,插入至当前Tab下一格 (TM行为)
// insert 为 number 时,插入至相对位置 (SC独自)
const insert = +options.insert;
newTabIndex = currentTab.index + insert;
if (newTabIndex < 0) newTabIndex = 0;
}
const createProperties = {
url,
active: active,
} as chrome.tabs.CreateProperties;
if (options.setParent) {
// SC 预设 setParent: true 以避免不可预计的问题
createProperties.openerTabId = tabId === -1 ? undefined : tabId;
createProperties.windowId = windowId === -1 ? undefined : windowId;
}
if (options.pinned) {
// VM/FM行为
createProperties.pinned = true;
} else if (newTabIndex >= 0) {
// insert option; pinned 情况下无效
createProperties.index = newTabIndex;
}
const tab = await chrome.tabs.create(createProperties);
return tab.id;
};
const tabId = await getNewTabId();
if (tabId) {
Expand Down
6 changes: 2 additions & 4 deletions src/template/scriptcat.d.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,11 @@ declare namespace GMTypes {
pinned?: boolean;

Copy link

Copilot AI Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增的 SWOpenTabOptions 类型缺少注释说明。建议添加 JSDoc 注释解释此类型的用途,例如说明这是 Service Worker 内部使用的 OpenTabOptions 变体,其中 active 字段为必需。

Suggested change
/**
* Service Worker internal variant of {@link OpenTabOptions} where the `active` field is required.
* Used to ensure that Service Worker logic always specifies whether the new tab should be active.
*/

Copilot uses AI. Check for mistakes.
/**
* 使用 `window.open` 打开新窗口,而不是浏览器 API。
* 可以打开某些特殊的链接
* 【弃用参数】本来用于打开本地特殊协定链接。升级MV3版后已完全不需使用。
*
* 相关:Issue #178
* 默认值:false
*/
useOpen?: boolean;
useOpen?: never;
Comment thread
CodFrm marked this conversation as resolved.
Outdated
}

interface XHRResponse {
Expand Down
6 changes: 2 additions & 4 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,11 @@ declare namespace GMTypes {
pinned?: boolean;

/**
* 使用 `window.open` 打开新窗口,而不是浏览器 API。
* 可以打开某些特殊的链接
* 【弃用参数】本来用于打开本地特殊协定链接。升级MV3版后已完全不需使用。
*
* 相关:Issue #178
* 默认值:false
*/
useOpen?: boolean;
useOpen?: never;
}

type SWOpenTabOptions = OpenTabOptions & Required<Pick<OpenTabOptions, "active">>;
Expand Down
Loading