Mercurial > ambnews-firefox
annotate ambnews/content/ambnews.js @ 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.
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Sat, 23 Aug 2008 09:15:27 -0700 |
| parents | f259d52d361e |
| children | 2f58ecd38d7a |
| 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> |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
22 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
23 * 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
|
24 * 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
|
25 * 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
|
26 * 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
|
27 * 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
|
28 * 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
|
29 * 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
|
30 * 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
|
31 * 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
|
32 * 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
|
33 * 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
|
34 * |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
35 * ***** END LICENSE BLOCK ***** */ |
|
f259d52d361e
Added tri-license headers to JS files.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
36 |
| 1 | 37 var AmbNews = { |
| 38 url: function url(spec) { | |
| 39 if (typeof(spec) != "string") | |
| 40 // Assume that a URI object was passed in, so just return it back. | |
| 41 return spec; | |
| 42 var classObj = Components.classes["@mozilla.org/network/io-service;1"]; | |
| 43 var ios = classObj.getService(Components.interfaces.nsIIOService); | |
| 44 return ios.newURI(spec, null, null); | |
| 45 }, | |
| 46 getFeed: function AmbNews_getFeed(url, callback) { | |
| 47 function onXml(text) { | |
| 48 var feed = null; | |
| 49 if (text) { | |
| 50 var resultListener = { | |
| 51 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
|
52 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
|
53 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
|
54 return; |
| 1 | 55 feed = {}; |
| 56 var Ci = Components.interfaces; | |
| 57 nsIFeed.QueryInterface(Ci.nsIFeed); | |
| 58 feed.link = nsIFeed.link; | |
| 59 feed.title = nsIFeed.title; | |
| 60 feed.entries = []; | |
| 61 for (var i = 0; i < nsIFeed.items.length; i++) { | |
| 62 var item = nsIFeed.items.queryElementAt(i, Ci.nsIFeedEntry); | |
| 63 feed.entries.push(item); | |
| 64 } | |
| 65 } | |
| 66 }; | |
| 67 var fp = AmbNews.__getFeedProcessor(); | |
| 68 fp.listener = resultListener; | |
| 69 fp.parseFromString(text, AmbNews.url(url)); | |
| 70 } | |
| 71 callback(feed); | |
| 72 } | |
| 73 AmbNews.__getXml(url, onXml); | |
| 74 }, | |
| 75 __getXml: function AmbNews_getXml(url, callback) { | |
| 76 var feedUrl = AmbNews.url(url); | |
| 77 var annSvc = AmbNews.__getAnnSvc(); | |
| 78 | |
|
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
|
79 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
|
80 var xml = annSvc.getPageAnnotation(feedUrl, "ambnews/feed-xml"); |
| 1 | 81 // TODO: Perhaps re-enable this, but only let it live for a |
| 82 // few minutes? | |
| 83 //callback(xml); | |
| 84 //return; | |
| 85 } | |
| 86 | |
| 87 var request = new XMLHttpRequest(); | |
| 88 request.open("GET", url, true); | |
| 89 | |
| 90 var onRscFunc = function getXml_onReadyStateChange() { | |
| 91 if (request.readyState == 4) { | |
| 92 if (request.status == 200) { | |
| 93 if (request.responseText) { | |
| 94 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
|
95 "ambnews/feed-xml", |
| 1 | 96 request.responseText, |
| 97 0, | |
| 98 annSvc.EXPIRE_DAYS); | |
| 99 callback(request.responseText); | |
| 100 } else | |
| 101 callback(""); | |
| 102 } else | |
| 103 callback(""); | |
| 104 } | |
| 105 }; | |
| 106 | |
| 107 request.onreadystatechange = onRscFunc; | |
| 108 request.send(null); | |
| 109 }, | |
| 110 __getFeedProcessor: function AmbNews_getFeedProcessor() { | |
| 111 var Cc = Components.classes; | |
| 112 var Ci = Components.interfaces; | |
| 113 var fp = Cc["@mozilla.org/feed-processor;1"] | |
| 114 .createInstance(Ci.nsIFeedProcessor); | |
| 115 return fp; | |
| 116 }, | |
| 117 __getAnnSvc: function AmbNews_getAnnSvc() { | |
| 118 var Cc = Components.classes; | |
| 119 var annSvc = Cc["@mozilla.org/browser/annotation-service;1"] | |
| 120 .getService(Components.interfaces.nsIAnnotationService); | |
| 121 return annSvc; | |
| 122 }, | |
| 123 onLinkAdded: function AmbNews_onLinkAdded(aEvent) { | |
| 124 if (aEvent.target.rel == "alternate" && | |
| 125 (aEvent.target.type == "application/rss+xml" || | |
| 126 aEvent.target.type == "application/atom+xml")) { | |
| 127 var annSvc = AmbNews.__getAnnSvc(); | |
| 128 var page = AmbNews.url(aEvent.target.baseURI); | |
| 129 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
|
130 annSvc.setPageAnnotation(page, "ambnews/feed", |
| 1 | 131 feed.spec, 0, annSvc.EXPIRE_WEEKS); |
| 132 } | |
|
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
|
133 }, |
|
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
|
134 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
|
135 // 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
|
136 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
|
137 |
|
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 // 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
|
139 // 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
|
140 |
|
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 // 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
|
142 // 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
|
143 // 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 |
|
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 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
|
154 } |
|
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
|
155 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
|
156 gBrowser.addEventListener("NewTab", onOpenTab, false); |
| 1 | 157 } |
| 158 }; |
