我试图用这个教程构建一个RSS提取程序的扩展方法“sendRequest将” -错误 - 无法调用未定义
http://www.lifehacker.com.au/2011/11/how-to-build-a-chrome-extension
但我想这是一个有点老谷歌已经做了一些改动。下载的扩展程序根本不起作用。我已经解决了许多错误,使工作,但即时stucked在此:
Uncaught TypeError: Cannot call method 'sendRequest' of undefined
这是我popup.html文件错误所在。
function fetch_feed() {
chrome.extension.sendRequest({'action' : 'fetch_feed', 'url' : 'http://spfc.terra.com.br/rss/news.xml'},
function(response) {
display_stories(response);
}
);
}
function display_stories(feed_data) {
var xml_doc = $.parseXML(feed_data);
$xml = $(xml_doc);
var items = $xml.find("item");
$('#popup').html('<img src="logo.png" id="logo" onclick="open_item(\'http://spfc.terra.com.br/\'); window.close();" /><br clear="all" />');
items.each(function(index, element) {
var post = parse_post(element);
var item = '';
var class2 = '';
if (index >= localStorage['unread_count']) {
// // console.log('visited');
item += '<div class="post read">';
}
else {
item += '<div class="post">'
}
item += '<span class="tag">' + post.tag + '</span>\
<a href="' + post.url + '">\
<div id="' + post.id + '" class="item" onclick="open_item(\'' + post.url + '\');">\
<img src="' + post.img + '" width="107" height="60" />\
<h4>' + post.title + '</h4>\
<span class="description">' + post.description + '...</span>\
</div>\
</a>';
item += '</div>';
$('#popup').append(item);
});
}
通过我也曾background.html错误的方式,在这一部分:
function fetch_feed(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = xhr.responseText;
callback(data);
} else {
callback(null);
}
}
}
// Note that any URL fetched here must be matched by a permission in
// the manifest.json file!
xhr.open('GET', url, true);
xhr.send();
}
function onRequest(request, sender, callback) {
if (request.action == 'fetch_feed') {
fetch_feed(request.url, callback);
}
}
chrome.extension.onRequest.addListener(onRequest);
是同样的错误,但与另一个属性:
Uncaught TypeError: Cannot read property 'onRequest' of undefined
附:我试图在询问和找不到解决方案之前找到解决方案。等待一些帮助来解决它。
感谢您的回答。我用sendMessage替换了sendRequest,用onMessage替换了onRequest(在background.html中),并且我得到了相同的错误: **未捕获的TypeError:无法调用未定义的**'sendMessage'方法** – 2013-03-26 19:34:23
@ThiagoDosReis对不起,我写了一个错误的函数应该是'chrome.runtime'而不是chrome.extension for'sendMessage'和'onMessage' – frisco 2013-03-27 10:46:30