0

寻求一点指导。我正在按照本指南构建我自己的 Google Chrome 扩展程序。

http://www.seomoz.org/blog/building-chrome-apps-and-extensions

它适用于他们的示例,但是当我尝试将其应用于我自己的 JSON 数据时,它不起作用。

他们的例子:

var messages = [];
var whens = [];

function checkNotifications(){
  $.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',function(data){
    var new_messages = [];
    var new_whens = [];
    $.each(data, function(key, val) {
      if (messages.indexOf(val['message']) == -1){
        chrome.browserAction.setBadgeText({'text': 'New'});
        chrome.browserAction.setBadgeBackgroundColor({'color': '#f00'});
      }
      new_messages.push(val['message']);
      new_whens.push(val['when']);
    });
    messages = new_messages;
    whens = new_whens;
  });
}

checkNotifications();
// 1800000 milliseconds is 30 minutes
setInterval(checkNotifications,1800000);

示例数据notifications.json如下所示:

[
    {"message": "<b>New blog post</b>: by David Sottimano - <a href='http://www.distilled.net/blog/miscellaneous/proxy-server-essential-information-for-seos/'>Proxy server essential information for SEOs</a>", "when": "6 days ago"},
    {"message": "<b>New module released</b>: Further SEO - <a href='http://www.distilled.net/u/linkbait/'>Linkbait that gets links</a>", "when": "11 days ago"},
    {"message": "<b>New blog post</b>: by Benjamin Estes - <a href='http://www.distilled.net/blog/web-analytics/segmenting-keywords-using-seotools/'>Segmenting keywords using SeoTools</a>", "when": "14 days ago"},
    {"message": "<b>New blog post</b>: by Will Critchlow - <a href='http://www.distilled.net/blog/conversion-rate-optimization/why-your-cro-tests-fail/'>Why your CRO tests fail</a>", "when": "16 days ago"} 
]

非常直截了当。它从 中获取数据notifications.json,并且本质foreach上对结果执行 a 以寻找新的 amessage标记。如果它找到一个,它会在扩展徽章上设置 NEW。

自然,我想将其更改$.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',为我自己的 RSS 提要,这是在 Google 的一点帮助下完成的,以转换为 JSON。

https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/thegearpost

这是输出的示例。有点长,所以我只提供一个片段。

{
  "responseData": 
  {
    "feed":
    {
      "feedUrl":"http://feeds.feedburner.com/thegearpost",
      "title":"TheGearPost",
      "link":"http://thegearpost.com",
      "author":"",
      "description":"The online gadget, gear and gift guide for men.",
      "type":"rss20",
      "entries":
      [
        {
          "title":"Man Crates",
          "link":"http://feedproxy.google.com/~r/thegearpost/~3/_WO8mJS7dUY/",
          "author":"Pat",
          "publishedDate":"Fri, 03 May 2013 12:19:10 -0700",
          "contentSnippet":"Call in your own care package with Man Crates."

我想我可以message用谷歌的contentSnippet部分替换示例代码。

但是,这不起作用,这是我的问题:

我将如何进行这项工作?我已经尝试过val['responseData.feed.contentSnippet'], val['feed.contentSnippet'],甚至只是val['contentSnippet']在下面的部分中,但没有一个有效。

$.each(data, function(key, val) {
  if (messages.indexOf(val['responseData.feed.contentSnippet']) == -1){
    chrome.browserAction.setBadgeText({'text': 'New'});
    chrome.browserAction.setBadgeBackgroundColor({'color': '#f00'});
  }
});

最终,我希望能够将单词 NEW 更改为实际的未读计数。但我认为我需要开始进行背景调查。

4

1 回答 1

0

The thing you need to iterate over is responseData/feed/entries, so:

$.each(data.responseData.feed.entries, 
  function(key, entry) {
    var snippet = entry.contentSnippet;
    // whatever you need to do
  }
);
于 2013-05-05T20:14:01.997 回答