403 lines
15 KiB
JavaScript
403 lines
15 KiB
JavaScript
/*
|
|
Digg Sidebar - Shows Digg stories in the Firefox sidebar in real time.
|
|
Copyright (C) 2008 Abhinav Sarkar <abhinav dot sarkar at gmail dot com>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
var DiggSidebar = {
|
|
prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
|
|
storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh",
|
|
endpoint: {
|
|
container: null,
|
|
topic: null,
|
|
category: "all"
|
|
},
|
|
updateInterval: 10,
|
|
updateIntervalDecay: 0,
|
|
shownStoriesCount: 30,
|
|
categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'],
|
|
timerId: null,
|
|
storyIds: [],
|
|
playing: true,
|
|
stories: [],
|
|
|
|
fetchData: function(url, handler) {
|
|
DiggSidebar.UI.indicator.style.display = '';
|
|
|
|
var request = new XMLHttpRequest();
|
|
request.onerror = DiggSidebar.errorHandler;
|
|
request.onload = handler;
|
|
request.open("GET", url, true);
|
|
request.send(null);
|
|
},
|
|
|
|
errorHandler: function(e) {
|
|
window.alert("Error in accessing data from Digg");
|
|
},
|
|
|
|
populateMenu: function(e) {
|
|
var XHR = e.target;
|
|
var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
|
|
var topics = data.topics;
|
|
var containers = new Array();
|
|
|
|
DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.topicPopup);
|
|
|
|
topics.forEach(function (topic) {
|
|
var menu = document.createElement('menu');
|
|
menu.setAttribute('id', topic.short_name + 'Menu');
|
|
menu.setAttribute('label', topic.name);
|
|
menu.setAttribute('accesskey', topic.name.charAt(0));
|
|
|
|
var menupopup = document.createElement('menupopup');
|
|
menupopup.setAttribute('id', topic.short_name + 'Popup');
|
|
|
|
DiggSidebar.categories.forEach(
|
|
function (label) {
|
|
var menuitem = document.createElement('menuitem');
|
|
menuitem.setAttribute('label', label);
|
|
menuitem.setAttribute('value', '/topic/' + topic.short_name + '/' + label.toLowerCase());
|
|
menuitem.setAttribute('accesskey', label.charAt(0));
|
|
menuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)");
|
|
menupopup.appendChild(menuitem);
|
|
}
|
|
);
|
|
menu.appendChild(menupopup);
|
|
DiggSidebar.UI.topicPopup.appendChild(menu);
|
|
|
|
containers.push(topic.container);
|
|
});
|
|
|
|
DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.containerPopup);
|
|
|
|
var filteredContainers = {name: [], short_name: []};
|
|
containers.forEach(function (container) {
|
|
var name = container.name;
|
|
var short_name = container.short_name;
|
|
if (filteredContainers.name.indexOf(name) == -1) {
|
|
filteredContainers.name.push(name);
|
|
filteredContainers.short_name.push(short_name);
|
|
}
|
|
});
|
|
|
|
for (var i=0; i<filteredContainers.name.length; i++) {
|
|
var menu = document.createElement('menu');
|
|
menu.setAttribute('id', filteredContainers.short_name[i] + 'Menu');
|
|
menu.setAttribute('label', filteredContainers.name[i]);
|
|
menu.setAttribute('accesskey', filteredContainers.name[i].charAt(0));
|
|
|
|
var menupopup = document.createElement('menupopup');
|
|
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
|
|
|
|
DiggSidebar.categories.forEach(
|
|
function (label) {
|
|
var menuitem = document.createElement('menuitem');
|
|
menuitem.setAttribute('label', label);
|
|
menuitem.setAttribute('value', '/container/' +
|
|
filteredContainers.short_name[i] + '/' + label.toLowerCase());
|
|
menuitem.setAttribute('accesskey', label.charAt(0));
|
|
menuitem.setAttribute('oncommand', "DiggSidebar.setEndPoint(this.value)");
|
|
menupopup.appendChild(menuitem);
|
|
}
|
|
);
|
|
|
|
menu.appendChild(menupopup);
|
|
DiggSidebar.UI.containerPopup.appendChild(menu);
|
|
}
|
|
DiggSidebar.UI.indicator.style.display = 'none';
|
|
},
|
|
|
|
populateStoryList: function() {
|
|
DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox);
|
|
var newStoryIds = new Array();
|
|
|
|
var jp = new JPath(DiggSidebar.stories);
|
|
var filteredStories = jp.$(function(story){
|
|
return (story.$("category").json == DiggSidebar.endpoint.category) ;
|
|
}).json;
|
|
jp = new JPath(filteredStories);
|
|
if (DiggSidebar.endpoint.topic)
|
|
filteredStories = jp.$(function(story){
|
|
return (story.$("topic/short_name").json == DiggSidebar.endpoint.topic);
|
|
}).json;
|
|
if (DiggSidebar.endpoint.container)
|
|
filteredStories = jp.$(function(story){
|
|
return story.$("container/short_name").json == DiggSidebar.endpoint.container;
|
|
}).json;
|
|
|
|
filteredStories.forEach(function (story, index) {
|
|
if (index < DiggSidebar.shownStoriesCount) {
|
|
var now = new Date();
|
|
if (story.promote_date != null)
|
|
var then = new Date(story.promote_date*1000);
|
|
else
|
|
var then = new Date(story.submit_date*1000);
|
|
var diff = Math.max(now - then, 0)
|
|
|
|
var hr = Math.floor(diff/(1000*3600));
|
|
var min = Math.floor(diff/(1000*60)) - 60*hr;
|
|
|
|
var relativeTime = ((hr > 0) && (min > 0)) ?
|
|
(hr + " hr " + min + " mins ago") :
|
|
((hr == 0) && (min > 0)) ?
|
|
(min + " mins ago") :
|
|
((hr == 0) && (min == 0)) ?
|
|
"just now" : "";
|
|
|
|
var li = DiggSidebar.UI.storyListBox.appendChild(document.createElement('richlistitem'));
|
|
li.id = "story_" + story.id;
|
|
var attributes = {
|
|
title: story.title,
|
|
date: relativeTime,
|
|
status: story.status,
|
|
container: story.container.name,
|
|
topic: story.topic.name,
|
|
username: story.user.name,
|
|
userlink: "http://digg.com/users/" + story.user.name,
|
|
diggs: story.diggs,
|
|
comments: story.comments,
|
|
desc: story.description,
|
|
link: story.link,
|
|
href: story.href
|
|
}
|
|
for (attr in attributes)
|
|
li.setAttribute(attr, attributes[attr]);
|
|
|
|
if (DiggSidebar.storyIds.indexOf(story.id) != -1) li.new = false;
|
|
li.read = story.read;
|
|
}
|
|
if (DiggSidebar.storyIds.indexOf(story.id) == -1) newStoryIds.push(story.id);
|
|
});
|
|
DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds);
|
|
DiggSidebar.setUpdateInterval(DiggSidebar.stories.slice(0,5));
|
|
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
|
|
|
DiggSidebar.UI.indicator.style.display = 'none';
|
|
},
|
|
|
|
showDescription: function(storyId) {
|
|
var jp = new JPath(DiggSidebar.stories);
|
|
var story = jp.query('//[id == ' + storyId + ']')[0];
|
|
story.read = true;
|
|
|
|
var listitems = DiggSidebar.UI.storyListBox.children;
|
|
for (var i=0; i<listitems.length; i++)
|
|
listitems[i].hideDescription();
|
|
|
|
document.getElementById('story_'+ story.id).showDescription();
|
|
},
|
|
|
|
getStories: function() {
|
|
var ep = DiggSidebar.prefs.get("endpoint").value || '';
|
|
DiggSidebar.UI.endPointDesc.value = "digg" + ep;
|
|
|
|
DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
|
|
"?count=30" + "&type=json" +
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
function(e) {
|
|
var newStories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories.filter(
|
|
function(story) {
|
|
return !(DiggSidebar.storyIds.indexOf(story.id) != -1);
|
|
});
|
|
newStories.forEach(function(story) {
|
|
story.category = DiggSidebar.endpoint.category;
|
|
story.read = false;
|
|
});
|
|
DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
|
|
var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
observerService.notifyObservers(null, DiggSidebar.storyListRefreshEventTopic, null);
|
|
});
|
|
},
|
|
|
|
createMenu: function() {
|
|
DiggSidebar.fetchData("http://services.digg.com/topics" + "?type=json" +
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
DiggSidebar.populateMenu);
|
|
},
|
|
|
|
setUpdateInterval: function(stories) {
|
|
//Adaptive update interval code START
|
|
if (stories.length == 0) return;
|
|
var sum = 0;
|
|
var weights = [4, 3, 2, 1]
|
|
var date = (stories[0].promote_date != null) ? 'promote_date' : 'submit_date';
|
|
stories.sort(function (a, b) {
|
|
if (a[date] > b[date]) return -1;
|
|
else if (a[date] < b[date]) return 1;
|
|
return 0;
|
|
});
|
|
for (var i=0; i<Math.min(4, stories.length-1); i++) {
|
|
var diff = stories[i][date] - stories[i+1][date];
|
|
sum += weights[i]*diff;
|
|
}
|
|
var newUpdateInterval = Math.round(
|
|
sum*1000/weights
|
|
.splice(0,Math.min(4, stories.length-1))
|
|
.reduce(function(a, b){return a + b;})
|
|
);
|
|
|
|
if (newUpdateInterval > 0) {
|
|
var previousUpdateInterval = DiggSidebar.updateInterval;
|
|
DiggSidebar.updateInterval = newUpdateInterval;
|
|
|
|
if (previousUpdateInterval == newUpdateInterval)
|
|
DiggSidebar.updateIntervalDecay += 1;
|
|
else
|
|
DiggSidebar.updateIntervalDecay = 0;
|
|
}
|
|
//Adaptive update interval code END
|
|
},
|
|
|
|
setEndPoint: function(ep) {
|
|
DiggSidebar.prefs.get("endpoint").value = ep;
|
|
DiggSidebar.endpoint = DiggSidebar.getEndpointParts(ep);
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
DiggSidebar.getStories();
|
|
},
|
|
|
|
getEndpointParts: function(ep) {
|
|
var tmp = {
|
|
topic: null,
|
|
container: null,
|
|
category: "all"
|
|
};
|
|
var parts = ep.split("/");
|
|
if (parts.length == 2)
|
|
tmp.category = parts[1];
|
|
else if (parts.length == 4) {
|
|
tmp[parts[1]] = parts[2];
|
|
tmp.category = parts[3];
|
|
}
|
|
return tmp;
|
|
},
|
|
|
|
togglePlayPause: function() {
|
|
if (DiggSidebar.playing) {
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
DiggSidebar.UI.playPauseButton.image = "chrome://diggsidebar/content/image/Play.png";
|
|
DiggSidebar.UI.playPauseButton.setAttribute("tooltiptext", "Click to Start autoupdate");
|
|
DiggSidebar.playing = false;
|
|
} else {
|
|
var timeout = Math.round(DiggSidebar.updateInterval*(Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
|
DiggSidebar.UI.playPauseButton.image = "chrome://diggsidebar/content/image/Pause.png";
|
|
DiggSidebar.UI.playPauseButton.setAttribute("tooltiptext", "Click to Pause autoupdate");
|
|
DiggSidebar.playing = true;
|
|
}
|
|
},
|
|
|
|
initialize: function(){
|
|
DiggSidebar.endpoint = DiggSidebar.getEndpointParts(DiggSidebar.prefs.get("endpoint").value);
|
|
var $ = function(id) {return document.getElementById(id)};
|
|
|
|
DiggSidebar.UI = {};
|
|
DiggSidebar.UI.indicator = $('dsBusyIndicator');
|
|
DiggSidebar.UI.topicPopup = $('dsTopicPopup');
|
|
DiggSidebar.UI.containerPopup = $('dsContainerPopup');
|
|
DiggSidebar.UI.storyListBox = $('dsStoryListBox');
|
|
DiggSidebar.UI.endPointDesc = $('dsEndPointDesc');
|
|
DiggSidebar.UI.playPauseButton = $('dsPlayPauseButton');
|
|
|
|
DiggSidebar.storyListObserver = new DiggSidebar.StoryListObserver();
|
|
DiggSidebar.createMenu();
|
|
DiggSidebar.getStories();
|
|
},
|
|
|
|
destroy: function(){
|
|
DiggSidebar.storyListObserver.unregister();
|
|
}
|
|
};
|
|
|
|
DiggSidebar.Utils = {
|
|
url: function(spec) {
|
|
var ios = Components.classes["@mozilla.org/network/io-service;1"]
|
|
.getService(Components.interfaces.nsIIOService);
|
|
return ios.newURI(spec, null, null);
|
|
},
|
|
|
|
openUrlInTab: function(url) {
|
|
Application.activeWindow.open(DiggSidebar.Utils.url(url));
|
|
},
|
|
|
|
decodeJson: function(string) {
|
|
var json = Components.classes["@mozilla.org/dom/json;1"]
|
|
.createInstance(Components.interfaces.nsIJSON);
|
|
return json.decode(string);
|
|
},
|
|
|
|
removeAllChildren: function(node) {
|
|
while (node.firstChild)
|
|
node.removeChild(node.firstChild);
|
|
}
|
|
}
|
|
|
|
DiggSidebar.StoryListObserver = function() {
|
|
this.registered = false;
|
|
this.register();
|
|
}
|
|
|
|
DiggSidebar.StoryListObserver.prototype = {
|
|
observe: function(subject, topic, data) {
|
|
if (topic == window.DiggSidebar.storyListRefreshEventTopic) {
|
|
window.DiggSidebar.populateStoryList();
|
|
}
|
|
},
|
|
|
|
register: function() {
|
|
if (this.registered == false) {
|
|
var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
observerService.addObserver(this, DiggSidebar.storyListRefreshEventTopic, false);
|
|
this.registered = true;
|
|
}
|
|
},
|
|
|
|
unregister: function() {
|
|
if (this.registered == true) {
|
|
var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
observerService.removeObserver(this, DiggSidebar.storyListRefreshEventTopic);
|
|
this.registered = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*TODO
|
|
preferences
|
|
toolbar button
|
|
refresh button
|
|
*/
|
|
/*DONE
|
|
v0.2
|
|
added kdb navigation
|
|
Added progressmeter
|
|
added error notification
|
|
added accesskeys
|
|
added new story notification
|
|
|
|
v0.2.1
|
|
adaptive polling interval
|
|
promote_date
|
|
pause/play
|
|
|
|
v0.5
|
|
new ui
|
|
*/
|