寻找一个小小的指导。我正在按照这个指南来构建我自己的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,以查找新的message标记。如果找到一个,它会在扩展徽章上设置新的。
当然,我想要将$.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',更改为我自己的RSS feed,这是在谷歌的一点帮助下完成的。
以下是输出的示例。它相当长,所以我只提供了一个代码片段。
{
"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."我想我可以用谷歌的contentSnippet部分替换示例代码的message。
然而,这不起作用,这是我的问题:
我该怎么做呢?我在下面的小节中尝试过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'});
}
});最终,我希望能够将新单词更改为实际的未读计数。但我想我得开始做背景调查了。
发布于 2013-05-06 04:14:02
您需要迭代的内容是responseData/feed/entries,因此:
$.each(data.responseData.feed.entries,
function(key, entry) {
var snippet = entry.contentSnippet;
// whatever you need to do
}
);https://stackoverflow.com/questions/16388624
复制相似问题