Looking for a little guidance. I'm following this guide to build my own Google Chrome extension.
寻找一些指导。我正在遵循这个指南来建立我自己的谷歌Chrome扩展。
http://www.seomoz.org/blog/building-chrome-apps-and-extensions
http://www.seomoz.org/blog/building-chrome-apps-and-extensions
It works great with their examples, but when I try to apply it to my own JSON data, it's not working.
它可以很好地处理他们的示例,但是当我尝试将它应用到我自己的JSON数据时,它就不起作用了。
Their example:
他们的例子:
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);
The sample data notifications.json
looks like:
样本数据的通知。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"}
]
Very straight forward. It gets the data from notifications.json
, and essentially does a foreach
on the results looking for new a message
tag. If it finds one, it sets NEW on the extension badge.
很直接。它从通知中获取数据。它本质上是在结果上为每个查找新的消息标记做一个foreach。如果它找到一个,它会在扩展标记上设置新的。
Naturally I want to change the $.getJSON('http://dl.dropbox.com/u/31988864/notifications.json',
to my own RSS feed, which is done with a little help from Google to convert to JSON.
当然,我希望将$. getjson ('http://dl.dropbox.com/u/31988864/notifications.json')更改为我自己的RSS提要,这是在谷歌的一点帮助下完成的,以便将其转换为JSON。
https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http:/ /feeds.feedburner.com/thegearpost
Here's a sample of the output. It's rather long, so I'm only providing a snippet.
这是输出的一个示例。时间相当长,所以我只提供一个片段。
{
"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."
I'm thinking I can replace the sample code's message
with Google's contentSnippet
section.
我想我可以用谷歌的contentSnippet部分替换示例代码的消息。
However, this doesn't work, and this is my question:
然而,这行不通,这是我的问题:
How would I make this work? I've tried val['responseData.feed.contentSnippet']
, val['feed.contentSnippet']
, and even just val['contentSnippet']
in the below section, but none of it is working.
我该怎么做呢?我试着val[' responseData.feed。contentSnippet '],val的饲料。[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'});
}
});
Ultimately I'd like to be able to change the words NEW to an actual unread count. But I think I need to get the background checks working to begin with.
最后,我希望能够将新单词改为实际的未读计数。但是我想我需要从背景调查开始。
1 个解决方案
#1
0
The thing you need to iterate over is responseData/feed/entries, so:
您需要迭代的东西是responseData/feed/条目,所以:
$.each(data.responseData.feed.entries,
function(key, entry) {
var snippet = entry.contentSnippet;
// whatever you need to do
}
);
#1
0
The thing you need to iterate over is responseData/feed/entries, so:
您需要迭代的东西是responseData/feed/条目,所以:
$.each(data.responseData.feed.entries,
function(key, entry) {
var snippet = entry.contentSnippet;
// whatever you need to do
}
);