| 1 | |
| 2 | |
| 3 | |
| 4 | const targetsByTab = new Map(); |
| 5 | globalThis.manageCageExtensions = async function (input) { |
| 6 | const items = (await chrome.management.getAll()).filter(item => item.id !== chrome.runtime.id && item.type === 'extension'); |
| 7 | if (input.mode === 'list') return { items: items.map(({ id, name, description, version, enabled, mayDisable, optionsUrl, permissions, hostPermissions, installType }) => |
| 8 | ({ id, name, description, version, enabled, mayDisable, optionsUrl, permissions, hostPermissions, installType })) }; |
| 9 | const entry = items.find(item => item.id === input.id); |
| 10 | if (!entry) throw new Error('扩展不存在'); |
| 11 | if (input.mode === 'options') return { url: entry.optionsUrl || '' }; |
| 12 | if (!entry.mayDisable) throw new Error('此扩展由浏览器管理'); |
| 13 | if (!['enable', 'disable'].includes(input.mode)) throw new Error('扩展操作无效'); |
| 14 | await chrome.management.setEnabled(input.id, input.mode === 'enable'); |
| 15 | return { ok: true }; |
| 16 | }; |
| 17 | let managedSplit; |
| 18 | const faviconVersions = new Map(); |
| 19 | chrome.tabs.onUpdated.addListener((tabId, changes) => { |
| 20 | if (!("favIconUrl" in changes || "url" in changes || changes.status === "complete")) return; |
| 21 | faviconVersions.set(tabId, (faviconVersions.get(tabId) || 0) + 1); |
| 22 | void chrome.debugger.getTargets().then(targets => { |
| 23 | const target = targets.find(item => item.tabId === tabId); |
| 24 | if (target) globalThis.cageFaviconChanged?.(target.id); |
| 25 | }).catch(() => {}); |
| 26 | }); |
| 27 | chrome.tabs.onRemoved.addListener(tabId => faviconVersions.delete(tabId)); |
| 28 | |
| 29 | |
| 30 | |
| 31 | globalThis.readCageFavicon = async function (targetId) { |
| 32 | try { |
| 33 | const targets = await chrome.debugger.getTargets(); |
| 34 | const tabId = targets.find(target => target.id === targetId)?.tabId; |
| 35 | if (tabId == null) return null; |
| 36 | const tab = await chrome.tabs.get(tabId); |
| 37 | const version = faviconVersions.get(tabId); |
| 38 | if (!/^https?:/.test(tab.url || "") || !tab.favIconUrl) return { url: tab.url, favicon: "" }; |
| 39 | const source = new URL(chrome.runtime.getURL("/_favicon/")); |
| 40 | source.searchParams.set("pageUrl", tab.url); |
| 41 | source.searchParams.set("size", "32"); |
| 42 | source.searchParams.set("version", String(version || 0)); |
| 43 | const image = new Image(); |
| 44 | image.src = source.href; |
| 45 | await image.decode(); |
| 46 | const current = await chrome.tabs.get(tabId); |
| 47 | if (current.url !== tab.url || current.favIconUrl !== tab.favIconUrl || faviconVersions.get(tabId) !== version) return null; |
| 48 | const canvas = document.createElement("canvas"); |
| 49 | canvas.width = canvas.height = 32; |
| 50 | canvas.getContext("2d").drawImage(image, 0, 0, 32, 32); |
| 51 | const favicon = canvas.toDataURL("image/png"); |
| 52 | return { url: tab.url, favicon: favicon.length <= 8192 ? favicon : "" }; |
| 53 | } catch { |
| 54 | return null; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | chrome.tabs.onActivated.addListener(({ tabId }) => { |
| 59 | const target = targetsByTab.get(tabId); |
| 60 | if (target) globalThis.cagePaneActivated?.(target); |
| 61 | }); |
| 62 | |
| 63 | chrome.tabs.onUpdated.addListener((id, changes) => { |
| 64 | const target = targetsByTab.get(id); |
| 65 | if (target && "splitViewId" in changes && changes.splitViewId !== managedSplit) { |
| 66 | targetsByTab.clear(); |
| 67 | managedSplit = null; |
| 68 | globalThis.cagePaneSeparated?.(target); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | globalThis.setCageSplit = async function (left, right) { |
| 73 | if (!chrome.tabs.createSplit) throw new Error("Chromium 155 or later is required"); |
| 74 | const targets = await chrome.debugger.getTargets(); |
| 75 | const findTab = id => targets.find(t => t.id === id)?.tabId; |
| 76 | const pair = left ? await Promise.all([left, right].map(id => chrome.tabs.get(findTab(id)))) : []; |
| 77 | if (pair.length && pair[0].incognito !== pair[1].incognito) throw new Error("Separate browser profiles"); |
| 78 | targetsByTab.clear(); |
| 79 | const affected = new Set(pair.map(tab => tab.splitViewId)); |
| 80 | if (managedSplit != null) affected.add(managedSplit); |
| 81 | if (!pair.length && managedSplit == null) { |
| 82 | |
| 83 | const [active] = await chrome.tabs.query({ active: true, lastFocusedWindow: true }); |
| 84 | if (active) affected.add(active.splitViewId); |
| 85 | } |
| 86 | const open = await chrome.tabs.query({}); |
| 87 | for (const id of affected) |
| 88 | if (id >= 0 && open.some(tab => tab.splitViewId === id)) await chrome.tabs.unsplit(id); |
| 89 | managedSplit = null; |
| 90 | if (!pair.length) return; |
| 91 | const [a, b] = pair; |
| 92 | const index = a.index + (b.windowId === a.windowId && b.index < a.index ? 0 : 1); |
| 93 | if (a.windowId !== b.windowId || b.index !== index) |
| 94 | await chrome.tabs.move(b.id, { windowId: a.windowId, index }); |
| 95 | managedSplit = await chrome.tabs.createSplit([a.id, b.id]); |
| 96 | targetsByTab.set(a.id, left); |
| 97 | targetsByTab.set(b.id, right); |
| 98 | }; |