12

I am trying to use chrome.scripting.executeScript for a chrome extension I'm building in ManifestV3 and following the Google documentation here (see image below):

enter image description here

I have added an 'arguments' property to pass in the title of the current tab to my function. However, I am getting the following error message:

TypeError: Error in invocation of scripting.executeScript(scripting.ScriptInjection injection, optional function callback): Error at parameter 'injection': Unexpected property: 'arguments'.

Here is my code:

chrome.tabs.query({ active: true }, function (tabs) {
  let tab = tabs[0];
  chrome.scripting.executeScript(
    {
      target: { tabId: tab.id },
      function: myFunction,
      arguments: [tab.title],
    },
    (injectionResults) => displaySearch(injectionResults[0].result)
  );
});

Any help would be appreciated, thanks!

4
  • Maybe this will help ScriptInjection Commented Mar 24, 2021 at 21:05
  • 2
    crbug.com/1166720, it's not yet implemented. Commented Mar 25, 2021 at 3:49
  • Found any workaround to pass arguments? Commented Apr 9, 2021 at 16:08
  • not arguments , it's args like args: [param1, param2, param3]. Commented Oct 1, 2021 at 4:49

2 Answers 2

9

The property name being used is incorrect. The new API uses the property "args". For example:

function greet(greeting) {
   console.log(`${greeting}, World!`);
}

chrome.scripting.executeScript({
     target: {tabId: tab.id},
     function: greet,
     args: ['Hello']
});

Output: Hello, World!

You can find more information here.

Sign up to request clarification or add additional context in comments.

2 Comments

They changed it recently. At the time this was posted, Google hadn't implemented it yet and the documentation was unclear.
I agree, Omer
5

There is a workaround for this that you can use until they implement the feature using chrome.storage. What you need to do is first save the argument with chrome.storage.sync.set(), then retrieve it inside the function you're injecting using chrome.storage.sync.get().

chrome.storage.sync.set({ myVariable: valueOfVariable });

chrome.tabs.query({ active: true }, function (tabs) {
  let tab = tabs[0];
  chrome.scripting.executeScript(
    {
      target: { tabId: tab.id },
      function: myFunction,
    }
  );
});

function myFunction() {
  chrome.storage.sync.get(["myVariable"], ({ myVariable }) => {
    // Do stuff
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.