简体   繁体   English

从扩展程序到内容脚本的Chrome扩展消息传递

[英]Chrome extension messaging from popup to content script

I'm trying to send a message from my popup.js to a content.js and I just don't get why it doesn't work. 我试图将消息从我的popup.js发送到content.js,但我不明白为什么它不起作用。

Here is my set up: 这是我的设置:

manifest.json 的manifest.json

{
    "manifest_version": 2,

    "name": "Form Builder",
    "description": "This extension populates a form.",
    "version": "0.1",

    "content_scripts": [{
        "all_frames": true,
        "matches": ["http://*/*", "https://*/*"],
        "js" : [
            "/js/jquery/jquery.min.js", 
            "/js/formLoader.js",
            "/js/content.js"
        ]
    }],

    "options_page": "/html/options.html",
    "permissions": ["https://localhost:8443/", 
                    "tabs", "activeTab", "storage", "background"
    ],
    "icons": { "16": "/images/icons/wb_icon_16.png",
               "48": "/images/icons/wb_icon_48.png",
              "128": "/images/icons/wb_icon_128.png" 
    },
    "browser_action": {
        "default_popup": "/html/popup.html"
    }
}

content.js content.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    sendResponse("received message: " + request);
    formLoader.setFormData(request, sendResponse);

    // Why isn't this getting executed when I send a message from popup?

    sendResponse("Thank you");
});

popup.js popup.js

var popup = {
    // a few parts snipped out for brevity
    sendMessage: function(message) {
        chrome.tabs.sendMessage(tab.id, message, function(message) {
            console.log(chrome.runtime.lastError);
            popup.handleCallbacks(message);
        });
    },
    handleCallbacks: function (message)  { 
        console.log("handleCallbacks: " + message);
    }
}

formLoader.js formLoader.js

var formLoader = {
    init : function() {
        $("#PostingBody").val("formLoader.init");
    },
    setFormData: function(data, callback) {
        callback("[formLoader.js] got data: " + data);
    }
}

formLoader.init();

When I do something like: 当我做类似的事情时:

popup.sendMessage(data);

In my console I get: 在我的控制台中,我得到:

Object {message: "Could not establish connection. Receiving end does not exist."}
handleCallbacks: undefined

Why isn't there a receiving end? 为什么没有接收端? What am I doing wrong with the listener in the content.js? 我对content.js中的侦听器有什么错?

Thanks to: This answer from another question 感谢: 来自另一个问题的答案

For Chrome 26+, use chrome.runtime.onMessage and chrome.runtime.sendMessage. 对于Chrome 26+,请使用chrome.runtime.onMessage和chrome.runtime.sendMessage。

even though I'm positive I've found several pages and references that fail to mention this. 即使我很肯定,我也发现了几页并没有提及此内容的参考。

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

相关问题 Chrome 扩展程序中的内容脚本和弹出脚本之间的消息传递 - Messaging between content script and popup script in Chrome extension Chrome扩展程序:如何响应内容脚本中的“弹出”操作? - Chrome extension: How to respond to the action from “popup” in the content script? 如何确定Chrome扩展程序是否位于内容脚本的弹出窗口中? - How can I determine if a Chrome extension is in a popup from the content script? 将控件从弹出窗口转移到内容脚本 - Google Chrome扩展程序 - Transfer control from popup to content script - Google Chrome Extension chrome扩展,可将消息从弹出窗口发送到内容脚本 - chrome extension to Send Message from popup to content script Chrome 扩展传递变量从 popup.js 到内容脚本 - Chrome Extension Pass Variable from popup.js to content script 从内容脚本到Google Chrome扩展程序中的弹出窗口,点击事件均不起作用 - Click event is not working from content script to popup on Google Chrome Extension Chrome 扩展 - 从弹出窗口到内容脚本的消息传递 - Chrome Extension - Message Passing from Popup to Content Script 在popup.html和content.js之间的Chrome扩展消息传递 - Chrome extension messaging between popup.html and content.js Chrome 扩展内容脚本消息传递反应应用程序在赛普拉斯环境中不起作用 - Chrome extension content script messaging react application not working in Cypress environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM