Javascript 作为对象传递的Chrome扩展消息
我正在将一条消息从内容脚本发送到后台页面。消息是一个URL,然后在后台解析ajax。当我将检索到的数据记录到Javascript 作为对象传递的Chrome扩展消息,javascript,jquery,google-chrome,google-chrome-extension,google-chrome-app,Javascript,Jquery,Google Chrome,Google Chrome Extension,Google Chrome App,我正在将一条消息从内容脚本发送到后台页面。消息是一个URL,然后在后台解析ajax。当我将检索到的数据记录到background.js中时,控制台日志是纯html的,但当我将消息发送回我的内容脚本时,消息突然出现在一个对象中,我不知道为什么 这是我的密码: Content_Script.js: Background.js: chrome.runtime.onMessage.addListener( 功能(消息、发送者、sendResponse){ getStats(消息、发送者、sendResp
background.js
中时,控制台日志是纯html的,但当我将消息发送回我的内容脚本时,消息突然出现在一个对象中,我不知道为什么
这是我的密码:
Content_Script.js:
Background.js:
chrome.runtime.onMessage.addListener(
功能(消息、发送者、sendResponse){
getStats(消息、发送者、sendResponse);
返回true;
});
函数getStats(消息、发件人、sendResponse){
$.ajax({
url:message.greeting,
数据类型:“文本”,
成功:功能(数据){
var info=$(“”).html(数据)[0].getElementsByTagName(“表”)[1];
如果(信息!=未定义){
console.log(info);//将纯HTML记录到控制台中。。
sendResponse({再见:信息});//返回消息。
}
}
});
}
我为重要部分添加了评论。。我似乎无法理解这一点,这让我发疯。有什么想法吗?谢谢 不能通过消息传递DOM节点;数据必须是JSON可序列化的,而DOM节点则不是
只传递您真正需要的数据(例如,
textContent
),然后在另一端重建节点。您不能通过消息传递DOM节点;数据必须是JSON可序列化的,而DOM节点则不是
仅传递您真正需要的数据(例如,
textContent
),然后在另一侧重建节点。如何将其配置为传递纯文本或类似内容?“配置”?无论你把什么东西塞进sendResponse
都会被传递。我如何配置它来传递纯文本或类似的东西?无论你把什么东西塞进sendResponse
都可以通过。
chrome.runtime.sendMessage({greeting: URL}, function(response) {
console.log(response.farewell); //logs an object: Object {farewell: Object}
$('#stats-table2').append(response.farewell); //doesn't output anything.
});
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
getStats(message, sender, sendResponse);
return true;
});
function getStats(message, sender, sendResponse){
$.ajax({
url: message.greeting,
dataType: 'text',
success: function(data) {
var info = $("<div>").html(data)[0].getElementsByTagName("table")[1];
if(info != undefined) {
console.log(info); //logs pure HTML into the console..
sendResponse({farewell:info}); //sends message back.
}
}
});
}