I have this code in the popup.js file on my Chrome Extension:
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: myFunc,
});
function myFunc() {
// do something
}
However, I would like to pass a parameter from the executeScript to myFunc. Something like this:
chrome.scripting.executeScript({
argument: {"arg1", "arg2"}
target: { tabId: tab.id },
function: connectCmd,
});
function myFunc(arg1: string, arg2: string) {
// do something
}
or something equivalent. Is there any way to do this?
Per the documentation the args (array of arguments) property is available since Chrome 92.
JSON.parse(JSON.stringify(args)) meaning that you can't transfer classes, Set, Map, and other nontrivial objects.func property in Chrome 92+, which is the new preferred alias of function.chrome.scripting.executeScript({
args: ['foo', 'bar']
target: { tabId: tab.id },
func: myFunc,
});
function myFunc(arg1, arg2) {
console.log(arg1, arg2); // will print `foo bar` in console of the web page
}
In the older Chrome you'll have to use messaging.