Skip to content
Merged
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
42 changes: 33 additions & 9 deletions src/pkg/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ export async function openInCurrentTab(url: string, tabId?: number) {
if (tab) {
// 添加 openerTabId 有可能出现 Error "Tab opener must be in the same window as the updated tab."
if (tab.id! >= 0) {
// 如 Tab API 有提供 tab.id, 則指定 tab.id
// 如 Tab API 有提供 tab.id, 则指定 tab.id
createProperties.openerTabId = tab.id;
if (tab.windowId! >= 0) {
// 如 Tab API 有提供 tab.windowId, 則指定 tab.windowId
// 如 Tab API 有提供 tab.windowId, 则指定 tab.windowId
createProperties.windowId = tab.windowId;
}
}
createProperties.index = tab.index + 1;
}
// 先嘗試以 openerTabId 和 windowId 打開
// 先尝试以 openerTabId 和 windowId 打开
try {
await chrome.tabs.create(createProperties);
return;
} catch (e: any) {
console.error("Error opening tab:", e);
}
// 失敗的話,刪去 openerTabId 和 windowId ,再次嘗試打開
// 失败的话,删去 openerTabId 和 windowId ,再次尝试打开
delete createProperties.openerTabId;
delete createProperties.windowId;
try {
Expand Down Expand Up @@ -171,18 +171,42 @@ export function errorMsg(e: any): string {
return "";
}

// 预计报错有机会在异步Promise裡发生,不一定是 chrome.userScripts.getScripts
// 预计报错有机会在异步Promise里发生,不一定是 chrome.userScripts.getScripts
export async function checkUserScriptsAvailable() {
try {
// Property access which throws if developer mode is not enabled.
// Method call which throws if API permission or toggle is not enabled.
chrome.userScripts;
const ret: chrome.userScripts.RegisteredUserScript[] | any = await chrome.userScripts.getScripts({
// 放不可能存在的id. 我们只需要知道API能否执行。不需要完整所有userScripts
ids: ["undefined-id-1", "undefined-id-2"],
ids: ["scriptcat-content"],
});
// 返回一个阵列的话表示API能正常使用 (有执行权限)
return ret !== undefined && ret !== null && typeof ret[Symbol.iterator] === "function";
// 返回一个阵列的话表示API可使用 (但API内部处理未必有给予扩充权限)
if (ret !== undefined && ret !== null && typeof ret[Symbol.iterator] === "function") {
//
} else {
return false;
}
Comment thread
CodFrm marked this conversation as resolved.
Outdated

const scripts = ret as chrome.userScripts.RegisteredUserScript[];
if (scripts.length > 0) {
// API内部处理实际给予扩充权限才会有返回
return true;
} else {
// Chrome MV3 的一部份浏览器没正确处理 MV3 UserScripts API 权限问题 (API内部处理没有给予扩充权限)
// 处理 Vivaldi 兼容
// Vivaldi 会无法注册 (1. register 报错)
await chrome.userScripts.register([
{
id: "undefined-id-3",
js: [{ code: "void 0;" }],
matches: ["https://not-found.scriptcat.org/"],
world: "USER_SCRIPT",
},
]);
// 清掉测试内容 (2. 如没有注入undefined-id-3成功,unregister 报错)
await chrome.userScripts.unregister({ ids: ["undefined-id-3"] });
return true;
}
} catch {
// Not available.
return false;
Expand Down
Loading