【发布时间】:2015-11-27 06:00:28
【问题描述】:
我正在从内容脚本向后台页面发送一条消息。该消息是一个 URL,然后在后台进行 ajax 解析。当我 console.log 检索到 background.js 的数据时,控制台日志是纯 html,但是当我将消息发送回我的内容脚本时,消息突然出现在一个对象中,我不确定为什么。
这是我的代码:
Content_Script.js:
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.
});
Background.js:
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.
}
}
});
}
我已经为重要部分添加了 cmets。我似乎无法弄清楚这一点,这让我发疯了。有任何想法吗?谢谢!
【问题讨论】:
标签: javascript jquery google-chrome google-chrome-extension google-chrome-app