Skip to content

Commit a526664

Browse files
committed
🐛 修复后台脚本中无法使用 GM_openInTab 的问题 #873
1 parent 0b53cb0 commit a526664

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/app/service/service_worker/gm_api.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import PermissionVerify, { PermissionVerifyApiGet } from "./permission_verify";
1212
import { cacheInstance } from "@App/app/cache";
1313
import EventEmitter from "eventemitter3";
1414
import { type RuntimeService } from "./runtime";
15-
import { getIcon, isFirefox, openInCurrentTab, cleanFileName } from "@App/pkg/utils/utils";
15+
import { getIcon, isFirefox, openInCurrentTab, cleanFileName, getCurrentTab } from "@App/pkg/utils/utils";
1616
import { type SystemConfig } from "@App/pkg/config/config";
1717
import i18next, { i18nName } from "@App/locales/locales";
1818
import FileSystemFactory from "@Packages/filesystem/factory";
@@ -854,9 +854,12 @@ export default class GMApi {
854854
const getNewTabId = async () => {
855855
const { tabId, windowId } = sender.getExtMessageSender();
856856
const active = options.active;
857-
const currentTab = await chrome.tabs.get(tabId);
857+
let currentTab: chrome.tabs.Tab | undefined;
858+
if (tabId !== -1) {
859+
currentTab = await chrome.tabs.get(tabId);
860+
}
858861
let newTabIndex = -1;
859-
if (options.incognito && !currentTab.incognito) {
862+
if (options.incognito && !currentTab?.incognito) {
860863
// incognito: "split" 在 normal 里不会看到 incognito
861864
// 只能创建新 incognito window
862865
// pinned 无效

src/pkg/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function parseStorageValue(str: string): unknown {
7979
}
8080

8181
// https://developer.chrome.com/docs/extensions/reference/api/tabs?hl=en#get_the_current_tab
82-
export async function getCurrentTab() {
82+
export async function getCurrentTab(): Promise<chrome.tabs.Tab | undefined> {
8383
// `tab` will either be a `tabs.Tab` instance or `undefined`.
8484
const [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true, windowType: "normal" });
8585
return tab;

0 commit comments

Comments
 (0)