错误 - 无法调用未定义的方法'sendRequest'

时间:2013-03-26 15:30:08

标签: google-chrome-extension

我正在尝试使用本教程构建RSS Fetcher扩展 -

http://www.lifehacker.com.au/2011/11/how-to-build-a-chrome-extension

但是我觉得这有点老了,谷歌已经做了一些改变。下载扩展程序根本不起作用。我已经解决了许多错误以使其工作但我坚持这一点:

  

未捕获的TypeError:无法调用未定义的方法'sendRequest'

这是我的 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);      

是相同的错误,但有另一个属性:

  

未捕获的TypeError:无法读取未定义的属性'onRequest'

P.S。在问之前,我试图找到解决方案,但不能。等待一些帮助来解决它。

1 个答案:

答案 0 :(得分:2)

不推荐使用

chrome.extension.sendRequest,您应该使用chrome.runtime.sendMessage将邮件传递到后台脚本

您可以在Chrome Message Passing

看到完整的API
相关问题