Permalink
Browse files

Stories are now loaded into LocalStorage every X number of minutes so…

… that they can be loaded more quickly, improving the responsiveness of the UI
  • Loading branch information...
1 parent db528f8 commit 81c106544e1265d8df3964af9fb672a48dcb27ec @adamalbrecht committed Jun 15, 2010
Showing with 261 additions and 134 deletions.
  1. BIN ajax-loader.gif
  2. +16 −0 background.html
  3. +130 −0 core.js
  4. +28 −0 json2.js
  5. +1 −0 manifest.json
  6. +86 −134 popup.html
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="core.js" />
+<script src="json2.js" />
+<script>
+var requestInterval = 60000; //miliseconds
+function startRequest() {
+ UpdateFeed();
+ window.setTimeout(startRequest, requestInterval);
+}
+</script>
+</head>
+<body onload='startRequest()'>
+</body>
+</html>
View
130 core.js
@@ -0,0 +1,130 @@
+var feedUrl = 'http://news.ycombinator.com/rss';
+var maxFeedItems = 15;
+var req;
+var buildPopupAfterResponse = false;
+var OnFeedSuccess = null;
+var OnFeedFail = null;
+
+function UpdateFeed() {
+ req = new XMLHttpRequest();
+ req.onload = HandleRssResponse;
+ req.onerror = handleError;
+ req.open("GET", feedUrl, true);
+ req.send(null);
+}
+
+function HandleRssResponse() {
+ var doc = req.responseXML;
+ if (!doc) {
+ doc = parseXml(req.responseText);
+ }
+ if (!doc) {
+ handleFeedParsingFailed("Not a valid feed.");
+ return;
+ }
+ links = parseHNLinks(doc);
+ SaveLinksToLocalStorage(links);
+ if (buildPopupAfterResponse == true) {
+ buildPopup(links);
+ buildPopupAfterResponse = false;
+ }
+}
+
+function handleError() {
+ handleFeedParsingFailed('Failed to fetch RSS feed.');
+}
+
+function handleFeedParsingFailed(error) {
+ var feed = document.getElementById("feed");
+ feed.className = "error"
+ feed.innerText = "Error: " + error;
+}
+
+function parseXml(xml) {
+ var xmlDoc;
+ try {
+ xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
+ xmlDoc.async = false;
+ xmlDoc.loadXML(xml);
+ } catch (e) {
+ xmlDoc = (new DOMParser).parseFromString(xml, 'text/xml');
+ }
+
+ return xmlDoc;
+}
+
+function parseHNLinks(doc) {
+ var entries = doc.getElementsByTagName('entry');
+ if (entries.length == 0) {
+ entries = doc.getElementsByTagName('item');
+ }
+ var count = Math.min(entries.length, maxFeedItems);
+ var links = new Array();
+ for (var i=0; i< count; i++) {
+ item = entries.item(i);
+ var hnLink = new Object();
+ //Grab the title
+ var itemTitle = item.getElementsByTagName('title')[0];
+ if (itemTitle) {
+ hnLink.Title = itemTitle.textContent;
+ } else {
+ hnLink.Title = "Unknown Title";
+ }
+
+ //Grab the Link
+ var itemLink = item.getElementsByTagName('link')[0];
+ if (!itemLink) {
+ itemLink = item.getElementsByTagName('comments')[0];
+ }
+ if (itemLink) {
+ hnLink.Link = itemLink.textContent;
+ } else {
+ hnLink.Link = '';
+ }
+
+ //Grab the comments link
+ var commentsLink = item.getElementsByTagName('comments')[0];
+ if (commentsLink) {
+ hnLink.CommentsLink = commentsLink.textContent;
+ } else {
+ hnLink.CommentsLink = '';
+ }
+
+ links.push(hnLink);
+ }
+ return links;
+}
+
+function SaveLinksToLocalStorage(links) {
+ localStorage["HN.NumLinks"] = links.length;
+ for (var i=0; i<links.length; i++) {
+ localStorage["HN.Link" + i] = JSON.stringify(links[i]);
+ }
+}
+
+function RetrieveLinksFromLocalStorage() {
+ var numLinks = localStorage["HN.NumLinks"];
+ if (numLinks == null) {
+ return null;
+ }
+ else {
+ var links = new Array();
+ for (var i=0; i<numLinks; i++) {
+ links.push(JSON.parse(localStorage["HN.Link" + i]))
+ }
+ return links;
+ }
+}
+
+function openLink() {
+ openUrl(this.href, false);
+}
+
+// Show |url| in a new tab.
+function openUrl(url, take_focus) {
+ // Only allow http and https URLs.
+ if (url.indexOf("http:") != 0 && url.indexOf("https:") != 0) {
+ return;
+ }
+ chrome.tabs.create({url: url, selected: take_focus});
+}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -9,6 +9,7 @@
"default_icon": "icon18.gif",
"popup": "popup.html"
},
+ "background_page":"background.html",
"permissions": [
"tabs",
"http://news.ycombinator.com/*"
Oops, something went wrong.

0 comments on commit 81c1065

Please sign in to comment.