【发布时间】:2013-04-29 14:39:50
【问题描述】:
寻求一些指导。我正在按照本指南构建我自己的 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 标记。如果它找到一个,它会在扩展徽章上设置 NEW。
当然,我想将$.getJSON('http://dl.dropbox.com/u/31988864/notifications.json', 更改为我自己的 RSS 提要,这是在 Google 的帮助下转换为 JSON 完成的。
这是一个输出示例。比较长,所以我只提供一个sn-p。
{
"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."
我想我可以用 Google 的 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'});
}
});
最终,我希望能够将单词 NEW 更改为实际的未读计数。但我认为我需要开始进行背景调查。
【问题讨论】:
标签: javascript json google-chrome-extension