简体   繁体   English

WebExtension消息从弹出脚本到内容脚本

[英]WebExtension message from popup script to content script

I am new to Firefox extensions. 我是Firefox扩展的新手。 I am trying to send a message from the popup script to the content script when a button is clicked, but I receive this error: 单击按钮时,我试图将消息从弹出脚本发送到内容脚本,但是我收到此错误:

Could not establish connection. Receiving end does not exist.

I am not getting any other errors, so I am not sure why this error is occuring. 我没有收到任何其他错误,所以我不确定为什么会发生此错误。

The popup script: 弹出脚本:

document.getElementById("rec").addEventListener("click", (e) => {
    var query = browser.tabs.query({currentWindow: true, active : true});
    var tab = query.then(getTab,onError);

    function getTab(tabs) {
        for (let tab of tabs){
            send(tab.id);
        }
    }

    function onError(error) {
      console.log(`Error: ${error}`);
    }

    function send(tab){
        browser.tabs.executeScript(tab, {
        file: "/content_scripts/recorder.js",})
        .then(function () { browser.tabs.sendMessage(tab, {record: "start"}) })
        .catch(console.error.bind(console));
    }
});

The content script: 内容脚本:

(function() {
    if (window.hasRun) {
        return;
    }
    window.hasRun = true;

    browser.runTime.onMessage.addListener(notify);
    function notify(message){
        alert(message.record);
    }
})();

manifest.json: manifest.json:

{

  "manifest_version": 2,
  "name": "TW Recorder",
  "version": "1.0",

  "description": "Recorder.",

  "icons": {
    "48": "icons/border-48.png"
  },
  "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest"
  ],
  "browser_action": {
    "default_icon": "icons/border-48.png",
    "default_title": "Recorder",
    "default_popup": "popup/main.html"
      },

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content_scripts/jquery-3.3.1.min.js","content_scripts/recorder.js"]
    }
  ]

}

The corrected content script (changed runTime to runtime): 更正后的内容脚本(将运行时更改为运行时):

(function() {
    if (window.hasRun) {
        return;
    }
    window.hasRun = true;

    browser.runtime.onMessage.addListener(notify);
    function notify(message){
        alert(message.record);
    }
})();

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

相关问题 将内容脚本中的消息发送到Firefox WebExtension中的浏览器操作? - Sending message from content script to browseraction in firefox webextension? 从内容脚本向弹出脚本发送消息 - send a message from content script to popup script 从弹出脚本到后台脚本的 firefox webextension 消息传递 - firefox webextension messaging from popup script to background script 如何将消息从弹出脚本传递到后台脚本到内容脚本 - How to pass message from popup script to background script to content script 将消息从弹出窗口发送到内容脚本? - Send message from popup to content script? Firefox WebExtension 内容脚本中的“this”沙盒化 - Sandboxed `this` in Firefox WebExtension content script chrome扩展,可将消息从弹出窗口发送到内容脚本 - chrome extension to Send Message from popup to content script 如何将消息从 popup.html 发送到内容脚本? - How to send the message from popup.html to content script? Chrome 扩展 - 从弹出窗口到内容脚本的消息传递 - Chrome Extension - Message Passing from Popup to Content Script 如何将消息从内容脚本发送到弹出脚本以显示在 popup.html 上 - How can i send a message from content script to popup script to appear on popup.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM