Skip to content Skip to sidebar Skip to footer

Chrome.scripting.executescript Pass Parameter

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 so

Solution 1:

Per the documentation the args (array of arguments) property is available since Chrome 92.

  • These arguments must be JSON-serializable i.e. string, number, boolean, null, array/object that consists of the mentioned types. In short the injected code will see the same thing as you can see by running JSON.parse(JSON.stringify(args)) meaning that you can't transfer classes, Set, Map, and other nontrivial objects.
  • You can use 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,
});

functionmyFunc(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.

Post a Comment for "Chrome.scripting.executescript Pass Parameter"