Mercurial > ambnews-firefox
view ambnews/content/ambnews.js @ 17:9d0dac426ba8
Fixed a bug whereby if you're logged out of a feed requiring authentication, an annoying dialog box would pop up asking for auth info (reported in http://www.toolness.com/wp/?p=158#comment-955).
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sat, 23 Aug 2008 12:41:10 -0700 |
parents | 2f58ecd38d7a |
children |
line wrap: on
line source
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Ambient News. * * The Initial Developer of the Original Code is Mozilla. * Portions created by the Initial Developer are Copyright (C) 2007 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Atul Varma <avarma@mozilla.com> * Ben Basson <ben@basson.at> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ var AmbNews = { url: function url(spec) { if (typeof(spec) != "string") // Assume that a URI object was passed in, so just return it back. return spec; var classObj = Components.classes["@mozilla.org/network/io-service;1"]; var ios = classObj.getService(Components.interfaces.nsIIOService); return ios.newURI(spec, null, null); }, getFeed: function AmbNews_getFeed(url, callback) { function onXml(text) { var feed = null; if (text) { var resultListener = { handleResult: function resultListener_handleResult(aResult) { var nsIFeed = aResult.doc; if (!nsIFeed) return; feed = {}; var Ci = Components.interfaces; nsIFeed.QueryInterface(Ci.nsIFeed); feed.link = nsIFeed.link; feed.title = nsIFeed.title; feed.entries = []; for (var i = 0; i < nsIFeed.items.length; i++) { var item = nsIFeed.items.queryElementAt(i, Ci.nsIFeedEntry); feed.entries.push(item); } } }; var fp = AmbNews.__getFeedProcessor(); fp.listener = resultListener; fp.parseFromString(text, AmbNews.url(url)); } callback(feed); } AmbNews.__getXml(url, onXml); }, __getXml: function AmbNews_getXml(url, callback) { var feedUrl = AmbNews.url(url); var annSvc = AmbNews.__getAnnSvc(); if (annSvc.pageHasAnnotation(feedUrl, "ambnews/feed-xml")) { var xml = annSvc.getPageAnnotation(feedUrl, "ambnews/feed-xml"); // TODO: Perhaps re-enable this, but only let it live for a // few minutes? //callback(xml); //return; } var request = new XMLHttpRequest(); request.mozBackgroundRequest = true; request.open("GET", url, true); var onRscFunc = function getXml_onReadyStateChange() { if (request.readyState == 4) { if (request.status == 200) { if (request.responseText) { annSvc.setPageAnnotation(feedUrl, "ambnews/feed-xml", request.responseText, 0, annSvc.EXPIRE_DAYS); callback(request.responseText); } else callback(""); } else callback(""); } }; request.onreadystatechange = onRscFunc; request.send(null); }, __getFeedProcessor: function AmbNews_getFeedProcessor() { var Cc = Components.classes; var Ci = Components.interfaces; var fp = Cc["@mozilla.org/feed-processor;1"] .createInstance(Ci.nsIFeedProcessor); return fp; }, __getAnnSvc: function AmbNews_getAnnSvc() { var Cc = Components.classes; var annSvc = Cc["@mozilla.org/browser/annotation-service;1"] .getService(Components.interfaces.nsIAnnotationService); return annSvc; }, onLinkAdded: function AmbNews_onLinkAdded(aEvent) { if (aEvent.target.rel == "alternate" && (aEvent.target.type == "application/rss+xml" || aEvent.target.type == "application/atom+xml")) { var annSvc = AmbNews.__getAnnSvc(); var page = AmbNews.url(aEvent.target.baseURI); var feed = AmbNews.url(aEvent.target.href); annSvc.setPageAnnotation(page, "ambnews/feed", feed.spec, 0, annSvc.EXPIRE_WEEKS); } }, initBrowser: function AmbNews_initBrowser() { // Listen for the addition of new <link> elements on pages. window.addEventListener("DOMLinkAdded", AmbNews.onLinkAdded, false); // Whenever the user opens a new tab, have it point to // about:news by default, and select the URL. // The following code was taken from the "New Tab Homepage 0.4" // extension's 'chrome/content/tabhomepage.js' file. Thanks, // Ben Basson, for figuring this out for me. gBrowser.removeEventListener("NewTab", BrowserOpenTab, false); function onOpenTab(aEvent) { var newTab = gBrowser.addTab("about:news"); gBrowser.selectedTab = newTab; if (gURLBar) setTimeout(function() { gURLBar.select(); }, 0); if (aEvent) aEvent.stopPropagation(); return newTab; } window.BrowserOpenTab = onOpenTab; gBrowser.addEventListener("NewTab", onOpenTab, false); } };