Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.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 来自默认\u弹出窗口的数据未通过后台脚本传递到内容\u脚本_Javascript_Google Chrome_Firefox_Google Chrome Extension_Firefox Addon Webextensions - Fatal编程技术网

Javascript 来自默认\u弹出窗口的数据未通过后台脚本传递到内容\u脚本

Javascript 来自默认\u弹出窗口的数据未通过后台脚本传递到内容\u脚本,javascript,google-chrome,firefox,google-chrome-extension,firefox-addon-webextensions,Javascript,Google Chrome,Firefox,Google Chrome Extension,Firefox Addon Webextensions,我正在创建一个Firefox插件(比如chrome扩展)。我可以将默认_弹出窗口中的用户选择值传递到后台脚本,但不能从那里传递到内容脚本 我的manifest.json { "manifest_version": 2, "name": "Beautiful Website", "description": "Customize website color based on User selection", "version": "1.0", "permissions": ["

我正在创建一个Firefox插件(比如chrome扩展)。我可以将默认_弹出窗口中的用户选择值传递到后台脚本,但不能从那里传递到内容脚本

我的manifest.json

{
  "manifest_version": 2,
  "name": "Beautiful Website",
  "description": "Customize website color based on User selection",
  "version": "1.0",
  "permissions": ["notifications"],
  "background": {
    "scripts": ["background_script.js"]
  },
  "browser_action": {
    "default_icon": "icons/logo_64.png",
    "default_title": "Beautiful Website",
    "default_popup": "index.html"
  },
  "content_scripts":[
    {
        "matches": ["<all_urls>"],
        "js": ["cs.js"]
    }
  ]
}
我的背景_script.js

function handleFormSelection(data){
  browser.notifications.create({
      "type": "basic",
      "title": "Updated Color Page",
      "message": data.userColor.toUpperCase()
  })

}

browser.runtime.onMessage.addListener(handleFormSelection);

browser.browserAction.onClicked.addListener(sendData)

function sendData(tab){
    browser.tabs.sendMessage(tab.id, {data:'dummyData'})
}
内容脚本(cs.js)是

问题是无法获取要发送的方法/将从用户接收到的颜色发送到内容脚本的方式


有人能帮我做最后一部分吗

当您有默认弹出窗口时,不会调用browserAction.onClicked,因此直接在弹出脚本中使用browser.tabs.sendMessage,而不是browser.runtime.sendMessage更改了弹出脚本(index_scr.js),如您所说@wOxxOm。但它给出了错误:tabs.sendMessage的参数类型不正确<代码>变量表单=document.querySelector(“表单”);表格.附录列表(“提交”,sendColor,false);函数sendColor(tab){var data=new FormData(form);var output=“”;for(const entry of data){output=entry[1];};browser.tabs.sendMessage(tab.id,{userColor:output})event.preventDefault();}嗯,调试它。@wOxxOm我调试了它,它正在获得表单的提交。选项卡为submit{target:,isTrusted:true,currentTarget:,eventPhase:2,气泡:true,可取消:true,defaultPrevented:false,Composited:false,timeStamp:0,cancelBubble:false,originalTarget:}嗯,请参阅browser.tabs.sendMessage的文档-第一个参数是一个整数选项卡id,可以通过browser.tabs.query获得({active:true,currentWindow:true}),查找示例。
var form = document.querySelector("form");

form.addEventListener("submit", function(event) {
    var data = new FormData(form);
    var output = "";
    for (const entry of data) {
      output = entry[1];
    };

    browser.runtime.sendMessage({
      userColor: `${output}`
    })

    event.preventDefault();
  }, false);
function handleFormSelection(data){
  browser.notifications.create({
      "type": "basic",
      "title": "Updated Color Page",
      "message": data.userColor.toUpperCase()
  })

}

browser.runtime.onMessage.addListener(handleFormSelection);

browser.browserAction.onClicked.addListener(sendData)

function sendData(tab){
    browser.tabs.sendMessage(tab.id, {data:'dummyData'})
}
function letsDoThis(data){
    console.log('Inside CS ', data);
}

browser.runtime.onMessage.addListener(letsDoThis)