Skip to content

Commit 014d62d

Browse files
cyfung1031CodFrmCopilot
authored
🐛 checkUserScriptsAvailable 兼容 Vivaldi (#859)
* checkUserScriptsAvailable 兼容 Vivaldi * checkUserScriptsAvailable 兼容 Vivaldi * Update src/pkg/utils/utils.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/pkg/utils/utils.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * 扩充->扩展 --------- Co-authored-by: wangyizhi <i@xloli.top> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: 王一之 <yz@ggnb.top>
1 parent e6bef8a commit 014d62d

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

src/pkg/utils/utils.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,23 @@ export async function openInCurrentTab(url: string, tabId?: number) {
9696
if (tab) {
9797
// 添加 openerTabId 有可能出现 Error "Tab opener must be in the same window as the updated tab."
9898
if (tab.id! >= 0) {
99-
// 如 Tab API 有提供 tab.id, 則指定 tab.id
99+
// 如 Tab API 有提供 tab.id, 则指定 tab.id
100100
createProperties.openerTabId = tab.id;
101101
if (tab.windowId! >= 0) {
102-
// 如 Tab API 有提供 tab.windowId, 則指定 tab.windowId
102+
// 如 Tab API 有提供 tab.windowId, 则指定 tab.windowId
103103
createProperties.windowId = tab.windowId;
104104
}
105105
}
106106
createProperties.index = tab.index + 1;
107107
}
108-
// 先嘗試以 openerTabId 和 windowId 打開
108+
// 先尝试以 openerTabId 和 windowId 打开
109109
try {
110110
await chrome.tabs.create(createProperties);
111111
return;
112112
} catch (e: any) {
113113
console.error("Error opening tab:", e);
114114
}
115-
// 失敗的話,刪去 openerTabId 和 windowId ,再次嘗試打開
115+
// 失败的话,删去 openerTabId 和 windowId ,再次尝试打开
116116
delete createProperties.openerTabId;
117117
delete createProperties.windowId;
118118
try {
@@ -171,18 +171,41 @@ export function errorMsg(e: any): string {
171171
return "";
172172
}
173173

174-
// 预计报错有机会在异步Promise裡发生,不一定是 chrome.userScripts.getScripts
174+
// 预计报错有机会在异步Promise里发生,不一定是 chrome.userScripts.getScripts
175175
export async function checkUserScriptsAvailable() {
176176
try {
177177
// Property access which throws if developer mode is not enabled.
178178
// Method call which throws if API permission or toggle is not enabled.
179179
chrome.userScripts;
180180
const ret: chrome.userScripts.RegisteredUserScript[] | any = await chrome.userScripts.getScripts({
181-
// 放不可能存在的id. 我们只需要知道API能否执行。不需要完整所有userScripts
182-
ids: ["undefined-id-1", "undefined-id-2"],
181+
ids: ["scriptcat-content", "undefined-id-3"],
183182
});
184-
// 返回一个阵列的话表示API能正常使用 (有执行权限)
185-
return ret !== undefined && ret !== null && typeof ret[Symbol.iterator] === "function";
183+
// 返回结果不是阵列的话表示API不可使用
184+
if (ret === undefined || ret === null || typeof ret[Symbol.iterator] !== "function") {
185+
return false;
186+
}
187+
188+
if (ret[0]) {
189+
// API内部处理实际给予扩展权限才会有返回Script
190+
// 含有 "scriptcat-content" 或 "undefined-id-3"
191+
return true;
192+
} else {
193+
// 没有 "scriptcat-content" 和 "undefined-id-3"
194+
// 进行 "undefined-id-3" 的注册反注册测试
195+
// Chrome MV3 的一部分浏览器(如 Vivaldi )没正确处理 MV3 UserScripts API 权限问题 (API内部处理没有给予扩展权限)
196+
// 此时会无法注册 (1. register 报错)
197+
await chrome.userScripts.register([
198+
{
199+
id: "undefined-id-3",
200+
js: [{ code: "void 0;" }],
201+
matches: ["https://not-found.scriptcat.org/"],
202+
world: "USER_SCRIPT",
203+
},
204+
]);
205+
// 清掉测试内容 (2. 如没有注入 undefined-id-3 成功,因脚本id不存在 unregister 报错)
206+
await chrome.userScripts.unregister({ ids: ["undefined-id-3"] });
207+
return true;
208+
}
186209
} catch {
187210
// Not available.
188211
return false;

0 commit comments

Comments
 (0)