chrome.runtime.sendMessage在Chrome扩展程序中不起作用

普拉纳夫·吉图里

我正在尝试创建一个新的扩展。我可以使用chrome.runtime.sendMessage函数,但是现在,我已经尝试了所有操作,但仍然无法将消息发送到后台脚本。控制台将填充来自content-script.js而不是来自的日志消息background.js

内容脚本

console.log("Hello World!s");
$(document).ready(function() {
    console.log("DOM READY!");
    $(document.documentElement).keydown(function (e) {
        console.log("Key Has Been Pressed!");
        chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
                if (response.fileData) {
                    alert("Contents Of Text File = ");
                }
                else {
                    console.log("No Response Received");
                }
            })

    })
});

background.js

console.log("Atleast reached background.js")
        chrome.runtime.onMessage.addListener (
            function (request, sender, sendResponse) {
                console.log("Reached Background.js");
                if (request.Message == "getTextFile") {
                    console.log("Entered IF Block");
                        $.get("http://localhost:8000/quicklyusercannedspeechbucket/helloWorld1", function(response) {
                    console.log(response);
                    sendResponse({fileData: response})
                })
            }
            else {
                console.log("Did not receive the response!!!")
            }
        }
    );

manifest.json

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "version": "0.1",
  "content_scripts": [ {
    "all_frames": true,
    "js": [ "jquery-2.1.4.min.js", "content-script.js" ],
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ]
  } ],
  "permissions": [ "http://*/*", "https://*/*", "storage" ],
  "background": {
    "scripts": [
      "jquery-2.1.4.min.js",
      "background.js"
    ]
  }
}

任何帮助表示赞赏:)

谢谢!

盖塔诺

您需要更改代码,以便在background.js中必须更改行为:

console.log("Atleast reached background.js")
chrome.runtime.onMessage.addListener (
    function (request, sender, sendResponse) {
        console.log("Reached Background.js");
        if (request.Message == "getTextFile") {
            console.log("Entered IF Block");
            $.get("http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1", function(response) {
                console.log(response);

                // to send back your response  to the current tab
                chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                    chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) {
                        ;
                    });
                });


            })
        }
        else {
            console.log("Did not receive the response!!!")
        }
    }
);

对于内容脚本,您需要执行以下操作:

console.log("Hello World!s");
$(document).ready(function() {
    console.log("DOM READY!");
    $(document.documentElement).keydown(function (e) {
        console.log("Key Has Been Pressed!");
        chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
            ;
        })

    })
});


// accept messages from background
chrome.runtime.onMessage.addListener (function (request, sender, sendResponse) {
    alert("Contents Of Text File = " + request.fileData);
});

sendResponse可以用作即时反馈,而不必作为计算的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

sendMessage 在 Chrome 扩展中不起作用

Chrome扩展程序runtime.sendmessage等待响应

chrome.runtime.onMessage中的sendResponse不起作用

Chrome消息传递:chrome.runtime.sendMessage在最新版本上不起作用49

chrome.runtime.sendMessage在重新加载Chrome扩展程序后从内容脚本抛出异常

jQuery Chrome扩展程序不起作用

Chrome扩展程序getUrl在注入的文件中不起作用

字体在Chrome扩展程序中不起作用

Chrome扩展程序中的Google Analytics(分析)不起作用

字体在Google Chrome扩展程序中不起作用

removeChild在Chrome扩展程序中不起作用

Chrome扩展程序:为什么不起作用?

Chrome扩展程序逻辑不起作用

jQuery在Chrome扩展程序弹出窗口中不起作用

Chrome扩展程序内容脚本不起作用

Google Chrome扩展程序入门教程不起作用

我的Chrome扩展程序在Edge上不起作用

[]循环检查按钮Chrome扩展程序不起作用

带有 Chrome 扩展程序的 onClick 不起作用

Chrome扩展程序:SendMessage问题

点击事件在Chrome扩展程序的“后台”页面中不起作用

browser.tabs.removeCSS 在 Chrome 扩展程序中不起作用?

chrome扩展程序中的浏览器事件监听器不起作用

点击事件在Chrome扩展程序选项页面中不起作用

为什么 .click() 在 Chrome 扩展中不起作用?

$ {document).click()在chrome扩展中不起作用

chrome.tabs.sendMessage在sendResponse工作时不起作用

chrome.runtime.reload阻止扩展

chrome 扩展 - 我如何等待 chrome.runtime 功能?