chrome.runtime.sendMessage在chrome扩展中不起作用
chrome.runtime.sendMessage not working in Chrome Extension
我正在尝试创建一个新的扩展。前一段时间我可以使用chrome.runtime.sendMessage函数,但现在,我已经尝试了所有功能,但它仍然无法将消息发送到后台脚本。控制台中填充了来自content-script.js
的日志消息,而不是来自background.js
的日志消息
内容脚本.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可以用作即时反馈,而不是计算的结果。
根据https://developer.chrome.com/extensions/messaging#simple如果从background.js中的onMessage
处理程序返回true,则可以异步调用sendResponse。
相关文章:
- XMLHttpRequest在移动设备上的chrome上不起作用
- ng disabled在chrome中不起作用.按钮在角度上未禁用
- 鼠标输入在 YUI 的 chrome 中不起作用
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- HTML5视频链接在Chrome中不起作用
- jQuery克隆函数在chrome中不起作用
- jQuery place attr src在Chrome中不起作用
- chrome中的onmouseover(下拉选项标记)事件不起作用-php,javascript
- .ajax调用在chrome中不起作用
- Javascript/AAJAX在Opera中不起作用,在FF/IE/Chrome中完美工作
- 正在处理在chrome扩展中不起作用的js
- MediaSource API demo不起作用(Chrome)
- Chrome 中表格的 CSV 导出不起作用 - JavaScript/AngularJS
- $.getJSON 在 Chrome 和 Safari 中不起作用
- OnClick 事件在 Chrome 中不起作用
- getUserMedia 在 Chrome v50.0.2661.86 上不起作用
- dataTransfer.set拖放的数据在chrome中不起作用
- 简单的鼠标悬停在 Chrome 中不起作用
- window.close();在Chrome和Firefox中不起作用
- Chrome 扩展程序权限不起作用