寻找一点指导。我遵循本指南构建自己的Google Chrome浏览器扩展程序。如何解析JavaScript中的这个JSON?
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
获得数据,并且基本上在寻找新的message
标记的结果上执行foreach
。如果找到一个,它会在扩展徽章上设置NEW。
当然,我想将$.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',
更改为我自己的RSS源,这是通过Google的一些帮助转换为JSON完成的。
这里的输出的一个示例。这是相当长的,所以我只提供一个片段。
{
"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改为实际未读数。但我认为我需要开始工作的背景调查。
感谢。还是行不通。我会将if语句更改为if(messages.indexOf(entry ['contentSnippet'])== -1){'? – Pat 2013-05-05 21:03:57
或'(messages.indexOf(entry.contentSnippet)<0)' – 2013-05-05 21:37:38
奇怪。使用示例数据,Chrome徽章将在刷新时显示NEW。我没有得到任何东西,这导致我相信它仍然无法正常工作。 – Pat 2013-05-05 21:52:51