Skip to content

Commit aa61335

Browse files
cyfung1031CodFrm
andauthored
🔥 弃掉 GM_openInTab({ useOpen: true }) (#867)
* 弃掉 `GM_openInTab({ useOpen: true })` * 处理lint问题和删除定义 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent c9f2350 commit aa61335

4 files changed

Lines changed: 52 additions & 77 deletions

File tree

src/app/service/offscreen/gm_api.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,6 @@ export default class GMApi {
179179
});
180180
}
181181

182-
async openInTab({ url }: { url: string }) {
183-
return window.open(url) !== undefined;
184-
}
185-
186182
textarea: HTMLTextAreaElement = document.createElement("textarea");
187183

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

212208
this.group.on("xmlHttpRequest", this.xmlHttpRequest.bind(this));
213-
this.group.on("openInTab", this.openInTab.bind(this));
214209
this.group.on("setClipboard", this.setClipboard.bind(this));
215210
}
216211
}

src/app/service/service_worker/gm_api.ts

Lines changed: 40 additions & 54 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, getCurrentTab, openInCurrentTab, cleanFileName } from "@App/pkg/utils/utils";
15+
import { getIcon, isFirefox, openInCurrentTab, cleanFileName } 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";
@@ -852,61 +852,47 @@ export default class GMApi {
852852
const url = request.params[0];
853853
const options = request.params[1];
854854
const getNewTabId = async () => {
855-
if (options.useOpen === true) {
856-
// 发送给offscreen页面处理 (使用window.open)
857-
const ok = await sendMessage(this.msgSender, "offscreen/gmApi/openInTab", { url });
858-
if (ok) {
859-
// 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签
860-
const tab = await getCurrentTab();
861-
return tab.id;
862-
} else {
863-
// 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭
864-
// 似乎在Firefox中禁止在background页面使用window.open(),强制返回null
865-
return false;
866-
}
867-
} else {
868-
const { tabId, windowId } = sender.getExtMessageSender();
869-
const active = options.active;
870-
const currentTab = await chrome.tabs.get(tabId);
871-
let newTabIndex = -1;
872-
if (options.incognito && !currentTab.incognito) {
873-
// incognito: "split" 在 normal 里不会看到 incognito
874-
// 只能创建新 incognito window
875-
// pinned 无效
876-
// insert 不重要
877-
await chrome.windows.create({
878-
url,
879-
incognito: true,
880-
focused: active,
881-
});
882-
return 0;
883-
}
884-
if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) {
885-
// insert 为 boolean 时,插入至当前Tab下一格 (TM行为)
886-
// insert 为 number 时,插入至相对位置 (SC独自)
887-
const insert = +options.insert;
888-
newTabIndex = currentTab.index + insert;
889-
if (newTabIndex < 0) newTabIndex = 0;
890-
}
891-
const createProperties = {
855+
const { tabId, windowId } = sender.getExtMessageSender();
856+
const active = options.active;
857+
const currentTab = await chrome.tabs.get(tabId);
858+
let newTabIndex = -1;
859+
if (options.incognito && !currentTab.incognito) {
860+
// incognito: "split" 在 normal 里不会看到 incognito
861+
// 只能创建新 incognito window
862+
// pinned 无效
863+
// insert 不重要
864+
await chrome.windows.create({
892865
url,
893-
active: active,
894-
} as chrome.tabs.CreateProperties;
895-
if (options.setParent) {
896-
// SC 预设 setParent: true 以避免不可预计的问题
897-
createProperties.openerTabId = tabId === -1 ? undefined : tabId;
898-
createProperties.windowId = windowId === -1 ? undefined : windowId;
899-
}
900-
if (options.pinned) {
901-
// VM/FM行为
902-
createProperties.pinned = true;
903-
} else if (newTabIndex >= 0) {
904-
// insert option; pinned 情况下无效
905-
createProperties.index = newTabIndex;
906-
}
907-
const tab = await chrome.tabs.create(createProperties);
908-
return tab.id;
866+
incognito: true,
867+
focused: active,
868+
});
869+
return 0;
870+
}
871+
if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) {
872+
// insert 为 boolean 时,插入至当前Tab下一格 (TM行为)
873+
// insert 为 number 时,插入至相对位置 (SC独自)
874+
const insert = +options.insert;
875+
newTabIndex = currentTab.index + insert;
876+
if (newTabIndex < 0) newTabIndex = 0;
877+
}
878+
const createProperties = {
879+
url,
880+
active: active,
881+
} as chrome.tabs.CreateProperties;
882+
if (options.setParent) {
883+
// SC 预设 setParent: true 以避免不可预计的问题
884+
createProperties.openerTabId = tabId === -1 ? undefined : tabId;
885+
createProperties.windowId = windowId === -1 ? undefined : windowId;
886+
}
887+
if (options.pinned) {
888+
// VM/FM行为
889+
createProperties.pinned = true;
890+
} else if (newTabIndex >= 0) {
891+
// insert option; pinned 情况下无效
892+
createProperties.index = newTabIndex;
909893
}
894+
const tab = await chrome.tabs.create(createProperties);
895+
return tab.id;
910896
};
911897
const tabId = await getNewTabId();
912898
if (tabId) {

src/template/scriptcat.d.tpl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ declare namespace CATType {
335335
// 文件修改时间
336336
updatetime: number;
337337
}
338+
339+
type CATFileStorageDetails = {
340+
baseDir: string;
341+
path: string;
342+
filename: any;
343+
file: FileStorageFileInfo;
344+
data?: string;
345+
};
338346
}
339347

340348
declare namespace GMTypes {
@@ -454,17 +462,10 @@ declare namespace GMTypes {
454462
* 默认值:false
455463
*/
456464
pinned?: boolean;
457-
458-
/**
459-
* 使用 `window.open` 打开新窗口,而不是浏览器 API。
460-
* 可以打开某些特殊的链接
461-
*
462-
* 相关:Issue #178
463-
* 默认值:false
464-
*/
465-
useOpen?: boolean;
466465
}
467466

467+
type SWOpenTabOptions = OpenTabOptions & Required<Pick<OpenTabOptions, "active">>;
468+
468469
interface XHRResponse {
469470
finalUrl?: string;
470471
readyState?: 0 | 1 | 2 | 3 | 4;
@@ -594,4 +595,6 @@ declare namespace GMTypes {
594595
closed?: boolean;
595596
name?: string;
596597
}
598+
599+
type GMClipboardInfo = string | { type?: string; mimetype?: string };
597600
}

src/types/scriptcat.d.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,6 @@ declare namespace GMTypes {
462462
* 默认值:false
463463
*/
464464
pinned?: boolean;
465-
466-
/**
467-
* 使用 `window.open` 打开新窗口,而不是浏览器 API。
468-
* 可以打开某些特殊的链接
469-
*
470-
* 相关:Issue #178
471-
* 默认值:false
472-
*/
473-
useOpen?: boolean;
474465
}
475466

476467
type SWOpenTabOptions = OpenTabOptions & Required<Pick<OpenTabOptions, "active">>;

0 commit comments

Comments
 (0)