| 60 | |
| 61 | /**@param {string[]} scripts*/ |
| 62 | export async function injectScript(...scripts) { |
| 63 | const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); |
| 64 | try { |
| 65 | await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: scripts }); |
| 66 | } catch (error) { |
| 67 | const errMsg = |
| 68 | "Cannot access contents of the page. Extension manifest must request permission to access the respective host."; |
| 69 | if (error.message === errMsg) { |
| 70 | const { id } = await chrome.tabs.create({ url: "/permission/index.html", index: tab.index + 1 }); |
| 71 | await new Promise((r) => setTimeout(r, 1000)); |
| 72 | const granted = await chrome.tabs.sendMessage(id, "requestHostPermission"); |
| 73 | if (granted) await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: scripts }); |
| 74 | } |
| 75 | console.info(error); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /**@param {(...args: any[]) => any} func*/ |
| 80 | export async function injectFuncScript(func, ...args) { |