admin管理员组

文章数量:1220933

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):

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!

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):

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!

Share Improve this question asked Mar 24, 2021 at 20:25 Richard PalmerRichard Palmer 1531 gold badge1 silver badge6 bronze badges 4
  • Maybe this will help ScriptInjection – Alexander Lonberg Commented Mar 24, 2021 at 21:05
  • 2 crbug.com/1166720, it's not yet implemented. – woxxom Commented Mar 25, 2021 at 3:49
  • Found any workaround to pass arguments? – Sohaib Commented Apr 9, 2021 at 16:08
  • not arguments , it's args like args: [param1, param2, param3]. – xjlin0 Commented Oct 1, 2021 at 4:49
Add a comment  | 

2 Answers 2

Reset to default 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.

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
  });
}

本文标签: javascriptchromescriptingexecuteScriptUnexpected property 39arguments39Stack Overflow