简体   繁体   English

chrome.scripting.executeScript - 意外属性:“参数”

[英]chrome.scripting.executeScript - Unexpected property: 'arguments'

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):我正在尝试将 chrome.scripting.executeScript 用于我在 ManifestV3 中构建的 chrome 扩展,并遵循此处的 Google 文档(见下图):

在此处输入图像描述

I have added an 'arguments' property to pass in the title of the current tab to my function.我添加了一个“参数”属性以将当前选项卡的标题传递给我的 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!任何帮助将不胜感激,谢谢!

There is a workaround for this that you can use until they implement the feature usingchrome.storage .在他们使用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()保存参数,然后在使用chrome.storage.sync.get()注入的 function 中检索它。

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

The property name being used is incorrect.使用的属性名称不正确。 The new API uses the property "args".新的 API 使用属性“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 .您可以在此处找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM