webext-content-scripts
Advanced tools
Comparing version 2.4.0 to 2.5.0
82
index.js
@@ -26,3 +26,7 @@ import chromeP from 'webext-polyfill-kinda'; | ||
} | ||
const nativeFunction = /^function \w+\(\) {[\n\s]+\[native code][\n\s]+}/; | ||
export async function executeFunction(target, function_, ...args) { | ||
if (nativeFunction.test(String(function_))) { | ||
throw new TypeError('Native functions need to be wrapped first, like `executeFunction(1, () => alert(1))`'); | ||
} | ||
const { frameId, tabId } = castTarget(target); | ||
@@ -81,37 +85,40 @@ if (gotScripting) { | ||
} | ||
function assertNoCode(files) { | ||
if (files.some(content => 'code' in content)) { | ||
throw new Error('chrome.scripting does not support injecting strings of `code`'); | ||
} | ||
} | ||
export async function executeScript({ tabId, frameId, files, allFrames, matchAboutBlank, runAt, }, { ignoreTargetErrors } = {}) { | ||
let lastInjection; | ||
for (let content of files) { | ||
if (typeof content === 'string') { | ||
content = { file: content }; | ||
const normalizedFiles = files.map(file => typeof file === 'string' ? { file } : file); | ||
if (gotScripting) { | ||
assertNoCode(normalizedFiles); | ||
const injection = chrome.scripting.executeScript({ | ||
target: { | ||
tabId, | ||
frameIds: arrayOrUndefined(frameId), | ||
allFrames, | ||
}, | ||
files: normalizedFiles.map(({ file }) => file), | ||
}); | ||
if (ignoreTargetErrors) { | ||
void catchTargetInjectionErrors(injection); | ||
} | ||
if (gotScripting) { | ||
if ('code' in content) { | ||
throw new Error('chrome.scripting does not support injecting strings of `code`'); | ||
} | ||
lastInjection = chrome.scripting.executeScript({ | ||
target: { | ||
tabId, | ||
frameIds: arrayOrUndefined(frameId), | ||
allFrames, | ||
}, | ||
files: [content.file], | ||
}); | ||
if (ignoreTargetErrors) { | ||
void catchTargetInjectionErrors(lastInjection); | ||
} | ||
return; | ||
} | ||
for (const content of normalizedFiles) { | ||
// Files are executed in order, but code isn’t, so it must wait the last script #31 | ||
if ('code' in content) { | ||
// eslint-disable-next-line no-await-in-loop -- On purpose, to serialize injection | ||
await lastInjection; | ||
} | ||
else { | ||
// Files are executed in order, but code isn’t, so it must wait the last script #31 | ||
if ('code' in content) { | ||
// eslint-disable-next-line no-await-in-loop -- On purpose, to serialize injection | ||
await lastInjection; | ||
} | ||
lastInjection = chromeP.tabs.executeScript(tabId, { | ||
...content, | ||
matchAboutBlank, | ||
allFrames, | ||
frameId, | ||
runAt, | ||
}); | ||
lastInjection = chromeP.tabs.executeScript(tabId, { | ||
...content, | ||
matchAboutBlank, | ||
allFrames, | ||
frameId, | ||
runAt, | ||
}); | ||
if (ignoreTargetErrors) { | ||
void catchTargetInjectionErrors(lastInjection); | ||
} | ||
@@ -134,3 +141,3 @@ } | ||
} | ||
async function injectContentScriptInSpecificTarget({ frameId, tabId, allFrames }, scripts, { ignoreTargetErrors } = {}) { | ||
async function injectContentScriptInSpecificTarget({ frameId, tabId, allFrames }, scripts, options = {}) { | ||
const injections = castArray(scripts).flatMap(script => [ | ||
@@ -144,3 +151,3 @@ insertCSS({ | ||
runAt: script.runAt ?? script.run_at, | ||
}), | ||
}, options), | ||
executeScript({ | ||
@@ -153,10 +160,5 @@ tabId, | ||
runAt: script.runAt ?? script.run_at, | ||
}), | ||
}, options), | ||
]); | ||
if (ignoreTargetErrors) { | ||
await catchTargetInjectionErrors(Promise.all(injections)); | ||
} | ||
else { | ||
await Promise.all(injections); | ||
} | ||
await Promise.all(injections); | ||
} | ||
@@ -163,0 +165,0 @@ // Sourced from: |
{ | ||
"name": "webext-content-scripts", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Utility functions to inject content scripts in WebExtensions, for Manifest v2 and v3", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
17628
4304
264