我正在尝试创建一个新的扩展.我曾经能够使用chrome.runtime.sendMessage函数,但是现在,我已经尝试了所有内容,但它仍然无法将消息发送到后台脚本.控制台正在填充来自content-script.js
但不是来自的日志消息background.js
内容的script.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" ] } }
任何帮助表示赞赏:)
谢谢!