Mercurial > ambnews-firefox
annotate ambnews/content/ambnews.js @ 16:34656f9d24a9
Added .hgignore file.
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Sat, 23 Aug 2008 09:24:00 -0700 |
| parents | 2f58ecd38d7a |
| children | 9d0dac426ba8 |
| rev | line source |
|---|---|
|
11
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
1 /* ***** BEGIN LICENSE BLOCK ***** |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
3 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
4 * The contents of this file are subject to the Mozilla Public License Version |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
5 * 1.1 (the "License"); you may not use this file except in compliance with |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
6 * the License. You may obtain a copy of the License at |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
7 * http://www.mozilla.org/MPL/ |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
8 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
9 * Software distributed under the License is distributed on an "AS IS" basis, |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
11 * for the specific language governing rights and limitations under the |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
12 * License. |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
13 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
14 * The Original Code is Ambient News. |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
15 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
16 * The Initial Developer of the Original Code is Mozilla. |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
17 * Portions created by the Initial Developer are Copyright (C) 2007 |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
18 * the Initial Developer. All Rights Reserved. |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
19 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
20 * Contributor(s): |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
21 * Atul Varma <avarma@mozilla.com> |
|
14
2f58ecd38d7a
Added Ben Basson as a contributor, since I used his code for the new tab override behavior.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
22 * Ben Basson <ben@basson.at> |
|
11
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
23 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
24 * Alternatively, the contents of this file may be used under the terms of |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
25 * either the GNU General Public License Version 2 or later (the "GPL"), or |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
27 * in which case the provisions of the GPL or the LGPL are applicable instead |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
28 * of those above. If you wish to allow use of your version of this file only |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
29 * under the terms of either the GPL or the LGPL, and not to allow others to |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
30 * use your version of this file under the terms of the MPL, indicate your |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
31 * decision by deleting the provisions above and replace them with the notice |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
32 * and other provisions required by the GPL or the LGPL. If you do not delete |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
33 * the provisions above, a recipient may use your version of this file under |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
34 * the terms of any one of the MPL, the GPL or the LGPL. |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
35 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
36 * ***** END LICENSE BLOCK ***** */ |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
37 |
| 1 | 38 var AmbNews = { |
| 39 url: function url(spec) { | |
| 40 if (typeof(spec) != "string") | |
| 41 // Assume that a URI object was passed in, so just return it back. | |
| 42 return spec; | |
| 43 var classObj = Components.classes["@mozilla.org/network/io-service;1"]; | |
| 44 var ios = classObj.getService(Components.interfaces.nsIIOService); | |
| 45 return ios.newURI(spec, null, null); | |
| 46 }, | |
| 47 getFeed: function AmbNews_getFeed(url, callback) { | |
| 48 function onXml(text) { | |
| 49 var feed = null; | |
| 50 if (text) { | |
| 51 var resultListener = { | |
| 52 handleResult: function resultListener_handleResult(aResult) { | |
|
10
f07bf657f483
Display of Vanilla forum threads is better now; fixed some display bugs; updated TODOs; documented more code.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
53 var nsIFeed = aResult.doc; |
|
f07bf657f483
Display of Vanilla forum threads is better now; fixed some display bugs; updated TODOs; documented more code.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
54 if (!nsIFeed) |
|
f07bf657f483
Display of Vanilla forum threads is better now; fixed some display bugs; updated TODOs; documented more code.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
55 return; |
| 1 | 56 feed = {}; |
| 57 var Ci = Components.interfaces; | |
| 58 nsIFeed.QueryInterface(Ci.nsIFeed); | |
| 59 feed.link = nsIFeed.link; | |
| 60 feed.title = nsIFeed.title; | |
| 61 feed.entries = []; | |
| 62 for (var i = 0; i < nsIFeed.items.length; i++) { | |
| 63 var item = nsIFeed.items.queryElementAt(i, Ci.nsIFeedEntry); | |
| 64 feed.entries.push(item); | |
| 65 } | |
| 66 } | |
| 67 }; | |
| 68 var fp = AmbNews.__getFeedProcessor(); | |
| 69 fp.listener = resultListener; | |
| 70 fp.parseFromString(text, AmbNews.url(url)); | |
| 71 } | |
| 72 callback(feed); | |
| 73 } | |
| 74 AmbNews.__getXml(url, onXml); | |
| 75 }, | |
| 76 __getXml: function AmbNews_getXml(url, callback) { | |
| 77 var feedUrl = AmbNews.url(url); | |
| 78 var annSvc = AmbNews.__getAnnSvc(); | |
| 79 | |
|
5
8cafc4c42c29
Changed page annotations from AmbNews/something to ambnews/something to match the way other paths are done, but it appears that page annotations are case-insensitive anyways, which is cool I guess.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
80 if (annSvc.pageHasAnnotation(feedUrl, "ambnews/feed-xml")) { |
|
8cafc4c42c29
Changed page annotations from AmbNews/something to ambnews/something to match the way other paths are done, but it appears that page annotations are case-insensitive anyways, which is cool I guess.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
81 var xml = annSvc.getPageAnnotation(feedUrl, "ambnews/feed-xml"); |
| 1 | 82 // TODO: Perhaps re-enable this, but only let it live for a |
| 83 // few minutes? | |
| 84 //callback(xml); | |
| 85 //return; | |
| 86 } | |
| 87 | |
| 88 var request = new XMLHttpRequest(); | |
| 89 request.open("GET", url, true); | |
| 90 | |
| 91 var onRscFunc = function getXml_onReadyStateChange() { | |
| 92 if (request.readyState == 4) { | |
| 93 if (request.status == 200) { | |
| 94 if (request.responseText) { | |
| 95 annSvc.setPageAnnotation(feedUrl, | |
|
5
8cafc4c42c29
Changed page annotations from AmbNews/something to ambnews/something to match the way other paths are done, but it appears that page annotations are case-insensitive anyways, which is cool I guess.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
96 "ambnews/feed-xml", |
| 1 | 97 request.responseText, |
| 98 0, | |
| 99 annSvc.EXPIRE_DAYS); | |
| 100 callback(request.responseText); | |
| 101 } else | |
| 102 callback(""); | |
| 103 } else | |
| 104 callback(""); | |
| 105 } | |
| 106 }; | |
| 107 | |
| 108 request.onreadystatechange = onRscFunc; | |
| 109 request.send(null); | |
| 110 }, | |
| 111 __getFeedProcessor: function AmbNews_getFeedProcessor() { | |
| 112 var Cc = Components.classes; | |
| 113 var Ci = Components.interfaces; | |
| 114 var fp = Cc["@mozilla.org/feed-processor;1"] | |
| 115 .createInstance(Ci.nsIFeedProcessor); | |
| 116 return fp; | |
| 117 }, | |
| 118 __getAnnSvc: function AmbNews_getAnnSvc() { | |
| 119 var Cc = Components.classes; | |
| 120 var annSvc = Cc["@mozilla.org/browser/annotation-service;1"] | |
| 121 .getService(Components.interfaces.nsIAnnotationService); | |
| 122 return annSvc; | |
| 123 }, | |
| 124 onLinkAdded: function AmbNews_onLinkAdded(aEvent) { | |
| 125 if (aEvent.target.rel == "alternate" && | |
| 126 (aEvent.target.type == "application/rss+xml" || | |
| 127 aEvent.target.type == "application/atom+xml")) { | |
| 128 var annSvc = AmbNews.__getAnnSvc(); | |
| 129 var page = AmbNews.url(aEvent.target.baseURI); | |
| 130 var feed = AmbNews.url(aEvent.target.href); | |
|
5
8cafc4c42c29
Changed page annotations from AmbNews/something to ambnews/something to match the way other paths are done, but it appears that page annotations are case-insensitive anyways, which is cool I guess.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
131 annSvc.setPageAnnotation(page, "ambnews/feed", |
| 1 | 132 feed.spec, 0, annSvc.EXPIRE_WEEKS); |
| 133 } | |
|
2
88b12596346d
Added an about:news handler and made the default page for new tabs about:news.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
134 }, |
|
13
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
135 initBrowser: function AmbNews_initBrowser() { |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
136 // Listen for the addition of new <link> elements on pages. |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
137 window.addEventListener("DOMLinkAdded", AmbNews.onLinkAdded, false); |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
138 |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
139 // Whenever the user opens a new tab, have it point to |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
140 // about:news by default, and select the URL. |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
141 |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
142 // The following code was taken from the "New Tab Homepage 0.4" |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
143 // extension's 'chrome/content/tabhomepage.js' file. Thanks, |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
144 // Ben Basson, for figuring this out for me. |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
145 gBrowser.removeEventListener("NewTab", BrowserOpenTab, false); |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
146 function onOpenTab(aEvent) { |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
147 var newTab = gBrowser.addTab("about:news"); |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
148 gBrowser.selectedTab = newTab; |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
149 if (gURLBar) |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
150 setTimeout(function() { gURLBar.select(); }, 0); |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
151 if (aEvent) |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
152 aEvent.stopPropagation(); |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
153 |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
154 return newTab; |
|
2
88b12596346d
Added an about:news handler and made the default page for new tabs about:news.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
155 } |
|
13
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
156 window.BrowserOpenTab = onOpenTab; |
|
fe30d1d71ee5
Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
157 gBrowser.addEventListener("NewTab", onOpenTab, false); |
| 1 | 158 } |
| 159 }; |
