Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将数组从内容脚本发送到弹出脚本_Javascript_Arrays_Google Chrome Extension_Message Passing - Fatal编程技术网

Javascript 如何将数组从内容脚本发送到弹出脚本

Javascript 如何将数组从内容脚本发送到弹出脚本,javascript,arrays,google-chrome-extension,message-passing,Javascript,Arrays,Google Chrome Extension,Message Passing,我正在尝试将页面上所有链接的数组发送到我的chrome扩展。我使用内容脚本获取数组数据,并使用消息将数据发送到扩展,不幸的是,从内容脚本接收到消息后,数组的数据不完整 我正在使用sendMessage函数注入我的内容脚本,该脚本然后收集页面的页面信息url和页面上的链接,然后通过消息发送 然后,侦听器在popup.js中接收该消息,然后将信息存储在对象中,不幸的是,该信息不完整 //Manifest.json { "name": "PHD SEO Tools Test", "ve

我正在尝试将页面上所有链接的数组发送到我的chrome扩展。我使用内容脚本获取数组数据,并使用消息将数据发送到扩展,不幸的是,从内容脚本接收到消息后,数组的数据不完整

我正在使用sendMessage函数注入我的内容脚本,该脚本然后收集页面的页面信息url和页面上的链接,然后通过消息发送

然后,侦听器在popup.js中接收该消息,然后将信息存储在对象中,不幸的是,该信息不完整

//Manifest.json
{
    "name": "PHD SEO Tools Test",
    "version": "1.0",
    "description": "SEO Tools Browser Extension First Draft",
    "permissions": [
        "tabs", 
        "activeTab",
        "storage"
    ],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "browser_action": {
        "default_popup": "popup.html"
    },
      "options_page": "options.html",
    "manifest_version": 2
}
正如您所看到的,URL从contentScript传递,数组从contentScript传递到popup,但不幸的是,数组中充满了空项

如何使链接数组显示如下实际内容:

(8) [link, link, link, link, link, link, link, link]
0: link
...
baseURI: "https://www.freecodecamp.org/news/"
href: "https://cdn.freecodecamp.org/news-assets/prism-1-16-
outerHTML: "<link rel="stylesheet" type="text/css" href="https://cdn.freecodecamp.org/news-assets/prism-1-16-
__proto__: HTMLLinkElement
...
1: link
2: link
3: link
4: link
5: link
6: link
7: link
length: 8
__proto__: Array(0)

无法发送DOM元素。仅支持简单类型,如字符串/数字、数组或此类类型的对象。您只需发送URL字符串即可。另外请注意,弹出窗口仅在显示时运行,因此您可能需要切换消息的方向-弹出窗口将通过chrome.tabs.sendMessage向内容脚本发送消息,内容脚本将在其onMessage中回复

承蒙

是的,不能将数组从弹出窗口传递到内容,反之亦然。解决方法是为数组创建一个foreach语句,并一次传递一个foreach语句

Popup.js:

 ary.forEach(element => {
      chrome.tabs.sendMessage(tabs[0].id, element);
 });
在希望接收阵列的脚本中,只需重建阵列

Content.js

 if(request != 'import' || request != 'scan'){
        arr.push(request);
 }
在上面的例子中,我只创建了两个命令。这两个命令是通过chrome.tabs.sendMessagetab[0]发送的


只要指定如果要发送的参数没有任何意义,但要添加到数组中,就可以成功地重建数组。上面的content.js就是我所说的例子。

您不能发送DOM元素。仅支持简单类型,如字符串/数字、数组或此类类型的对象。您只需发送URL字符串即可。另外请注意,弹出窗口仅在显示时运行,因此您可能需要切换消息的方向-弹出窗口将通过chrome.tabs.sendMessage向内容脚本发送消息,内容脚本将在其onMessage中回复。
(8) [link, link, link, link, link, link, link, link]
0: link
...
baseURI: "https://www.freecodecamp.org/news/"
href: "https://cdn.freecodecamp.org/news-assets/prism-1-16-
outerHTML: "<link rel="stylesheet" type="text/css" href="https://cdn.freecodecamp.org/news-assets/prism-1-16-
__proto__: HTMLLinkElement
...
1: link
2: link
3: link
4: link
5: link
6: link
7: link
length: 8
__proto__: Array(0)
 ary.forEach(element => {
      chrome.tabs.sendMessage(tabs[0].id, element);
 });
 if(request != 'import' || request != 'scan'){
        arr.push(request);
 }