|
|
|
@ -1,307 +1,295 @@
|
|
|
|
|
var gsti;
|
|
|
|
|
var currentStories = new Array();
|
|
|
|
|
var playing = true;
|
|
|
|
|
var DiggSidebar = {
|
|
|
|
|
prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
|
|
|
|
|
categories: ['All', 'Popular', 'Upcoming', 'Hot', 'Top'],
|
|
|
|
|
timerId : null,
|
|
|
|
|
currentStories : new Array(),
|
|
|
|
|
playing : true,
|
|
|
|
|
|
|
|
|
|
//getElementById
|
|
|
|
|
function $ei(id, parentNode) {
|
|
|
|
|
parentNode = parentNode || document;
|
|
|
|
|
return parentNode.getElementById(id);
|
|
|
|
|
}
|
|
|
|
|
fetchData: function(url, handler) {
|
|
|
|
|
DiggSidebar.diggIndicator.style.display = '';
|
|
|
|
|
|
|
|
|
|
function fetchData(url, handler) {
|
|
|
|
|
//alert("inside fetchData");
|
|
|
|
|
$ei('diggIndicator').style.display = '';
|
|
|
|
|
$ei("diggIndicator").setAttribute('mode', 'undetermined');
|
|
|
|
|
var request = Components.
|
|
|
|
|
classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
|
|
|
|
createInstance();
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
request.onerror = DiggSidebar.errorHandler;
|
|
|
|
|
request.onload = handler;
|
|
|
|
|
request.open("GET", url, true);
|
|
|
|
|
request.send(null);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// QI the object to nsIDOMEventTarget to set event handlers on it:
|
|
|
|
|
errorHandler: function(e) {
|
|
|
|
|
window.alert("Error in accessing data from Digg");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
request.QueryInterface(Components.interfaces.nsIDOMEventTarget);
|
|
|
|
|
request.addEventListener("load", handler, false);
|
|
|
|
|
request.addEventListener("error", handleError, false);
|
|
|
|
|
populateMenu: function(e) {
|
|
|
|
|
var XHR = e.target;
|
|
|
|
|
var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
|
|
|
|
|
var topics = data.topics;
|
|
|
|
|
var containers = new Array();
|
|
|
|
|
|
|
|
|
|
// QI it to nsIXMLHttpRequest to open and send the request:
|
|
|
|
|
|
|
|
|
|
request.QueryInterface(Components.interfaces.nsIXMLHttpRequest);
|
|
|
|
|
request.open("GET", url, true);
|
|
|
|
|
request.send(null);
|
|
|
|
|
//$ei('diggIndicator').style.display = '';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleError(e) {
|
|
|
|
|
$ei('storyList').appendItem("!! Error accessing Digg !!");
|
|
|
|
|
//$ei('diggIndicator').style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function populateMenu(e) {
|
|
|
|
|
XHR = e.target;
|
|
|
|
|
data = decodeJson(XHR.responseText);
|
|
|
|
|
topics = data.topics;
|
|
|
|
|
containers = new Array();
|
|
|
|
|
|
|
|
|
|
while ($ei('topicPopup').firstChild) {
|
|
|
|
|
$ei('topicPopup').removeChild($ei('topicPopup').firstChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topics.forEach(function (topic) {
|
|
|
|
|
menu = document.createElement('menu');
|
|
|
|
|
menu.setAttribute('id', topic.short_name + 'Menu');
|
|
|
|
|
menu.setAttribute('label', topic.name);
|
|
|
|
|
menu.setAttribute('accesskey', topic.name.charAt(0));
|
|
|
|
|
|
|
|
|
|
menupopup = document.createElement('menupopup');
|
|
|
|
|
menupopup.setAttribute('id', topic.short_name + 'Popup');
|
|
|
|
|
|
|
|
|
|
['All', 'Popular', 'Upcoming', 'Hot', 'Top'].forEach(
|
|
|
|
|
function (label) {
|
|
|
|
|
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', "setEndPoint(this.value)");
|
|
|
|
|
menupopup.appendChild(menuitem);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
menu.appendChild(menupopup);
|
|
|
|
|
$ei('topicPopup').appendChild(menu);
|
|
|
|
|
|
|
|
|
|
containers.push(topic.container);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
while ($ei('containerPopup').firstChild) {
|
|
|
|
|
$ei('containerPopup').removeChild($ei('containerPopup').firstChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filteredContainers = {name: [], short_name: []};
|
|
|
|
|
containers.forEach(function (container) {
|
|
|
|
|
name = container.name;
|
|
|
|
|
short_name = container.short_name;
|
|
|
|
|
if (filteredContainers.name.indexOf(name) == -1) {
|
|
|
|
|
filteredContainers.name.push(name);
|
|
|
|
|
filteredContainers.short_name.push(short_name);
|
|
|
|
|
while (DiggSidebar.topicPopup.firstChild) {
|
|
|
|
|
DiggSidebar.topicPopup.removeChild(DiggSidebar.topicPopup.firstChild);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (i=0; i<filteredContainers.name.length; i++) {
|
|
|
|
|
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));
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
menupopup = document.createElement('menupopup');
|
|
|
|
|
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
|
|
|
|
|
var menupopup = document.createElement('menupopup');
|
|
|
|
|
menupopup.setAttribute('id', topic.short_name + 'Popup');
|
|
|
|
|
|
|
|
|
|
['All', 'Popular', 'Upcoming', 'Hot', 'Top'].forEach(
|
|
|
|
|
function (label) {
|
|
|
|
|
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', "setEndPoint(this.value)");
|
|
|
|
|
menupopup.appendChild(menuitem);
|
|
|
|
|
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.topicPopup.appendChild(menu);
|
|
|
|
|
|
|
|
|
|
containers.push(topic.container);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
while (DiggSidebar.containerPopup.firstChild) {
|
|
|
|
|
DiggSidebar.containerPopup.removeChild(DiggSidebar.containerPopup.firstChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
menu.appendChild(menupopup);
|
|
|
|
|
$ei('containerPopup').appendChild(menu);
|
|
|
|
|
}
|
|
|
|
|
$ei('diggIndicator').style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
function populateStoryList(e) {
|
|
|
|
|
XHR = e.target;
|
|
|
|
|
//Application.console.log(XHR.responseText);
|
|
|
|
|
data = decodeJson(XHR.responseText);
|
|
|
|
|
stories = data.stories;
|
|
|
|
|
var menupopup = document.createElement('menupopup');
|
|
|
|
|
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
|
|
|
|
|
|
|
|
|
|
while ($ei('storyList').firstChild) {
|
|
|
|
|
$ei('storyList').removeChild($ei('storyList').firstChild);
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var newStories = new Array();
|
|
|
|
|
//Application.console.log(stories.length);
|
|
|
|
|
menu.appendChild(menupopup);
|
|
|
|
|
DiggSidebar.containerPopup.appendChild(menu);
|
|
|
|
|
}
|
|
|
|
|
DiggSidebar.diggIndicator.style.display = 'none';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
stories.forEach(function (story) {
|
|
|
|
|
newStories.push(story.id);
|
|
|
|
|
//Application.console.log(i + " " + story.title + " " + story.id);
|
|
|
|
|
populateStoryList: function(e) {
|
|
|
|
|
var XHR = e.target;
|
|
|
|
|
//Application.console.log(XHR.responseText);
|
|
|
|
|
var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
|
|
|
|
|
var stories = data.stories;
|
|
|
|
|
|
|
|
|
|
listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
|
|
|
|
|
listitem.id = "story_" + story.id;
|
|
|
|
|
listitem.setAttribute('title', story.title);
|
|
|
|
|
while (DiggSidebar.storyList.firstChild) {
|
|
|
|
|
DiggSidebar.storyList.removeChild(DiggSidebar.storyList.firstChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentStories.indexOf(story.id) != -1) listitem.new = false;
|
|
|
|
|
});
|
|
|
|
|
currentStories = newStories;
|
|
|
|
|
var newStories = new Array();
|
|
|
|
|
//Application.console.log(stories.length);
|
|
|
|
|
|
|
|
|
|
//Adaptive update interval code START
|
|
|
|
|
sum = 0;
|
|
|
|
|
weights = [4, 3, 2, 1]
|
|
|
|
|
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 (i=0; i<Math.min(4, stories.length-1); i++) {
|
|
|
|
|
diff = stories[i][date] - stories[i+1][date];
|
|
|
|
|
sum += weights[i]*diff;
|
|
|
|
|
}
|
|
|
|
|
updateInterval = Math.round(
|
|
|
|
|
sum*1000/weights
|
|
|
|
|
.splice(0,Math.min(4, stories.length-1))
|
|
|
|
|
.reduce(function(a, b){return a + b;})
|
|
|
|
|
);
|
|
|
|
|
//Application.console.log(updateInterval);
|
|
|
|
|
stories.forEach(function (story) {
|
|
|
|
|
newStories.push(story.id);
|
|
|
|
|
//Application.console.log(i + " " + story.title + " " + story.id);
|
|
|
|
|
|
|
|
|
|
var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
|
|
|
|
|
var listitem = DiggSidebar.storyList.appendChild(document.createElement('richlistitem'));
|
|
|
|
|
listitem.id = "story_" + story.id;
|
|
|
|
|
listitem.setAttribute('title', story.title);
|
|
|
|
|
|
|
|
|
|
if (updateInterval > 0) {
|
|
|
|
|
previousUpdateInterval = prefs.get("updateinterval").value;
|
|
|
|
|
prefs.get("updateinterval").value = updateInterval;
|
|
|
|
|
if (DiggSidebar.currentStories.indexOf(story.id) != -1) listitem.new = false;
|
|
|
|
|
});
|
|
|
|
|
DiggSidebar.currentStories = newStories;
|
|
|
|
|
DiggSidebar.setUpdateInterval(stories);
|
|
|
|
|
DiggSidebar.diggIndicator.style.display = 'none';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateIntervalDecay = prefs.get("updateintervaldecay").value;
|
|
|
|
|
populateDescription: function(e) {
|
|
|
|
|
var XHR = e.target;
|
|
|
|
|
//Application.console.log(XHR.responseText);
|
|
|
|
|
var data = DiggSidebar.Utils.decodeJson(XHR.responseText);
|
|
|
|
|
var story = data.stories[0];
|
|
|
|
|
|
|
|
|
|
if (previousUpdateInterval == updateInterval)
|
|
|
|
|
prefs.get("updateintervaldecay").value = updateIntervalDecay + 1;
|
|
|
|
|
var now = new Date();
|
|
|
|
|
if (story.promote_date != null)
|
|
|
|
|
var then = new Date(story.promote_date*1000);
|
|
|
|
|
else
|
|
|
|
|
prefs.get("updateintervaldecay").value = 0;
|
|
|
|
|
}
|
|
|
|
|
window.clearTimeout(gsti);
|
|
|
|
|
var then = new Date(story.submit_date*1000);
|
|
|
|
|
var diff = Math.max(now - then, 0)
|
|
|
|
|
|
|
|
|
|
updateInterval = prefs.get("updateinterval").value;
|
|
|
|
|
updateIntervalDecay = prefs.get("updateintervaldecay").value;
|
|
|
|
|
var hr = Math.floor(diff/(1000*3600));
|
|
|
|
|
var min = Math.floor(diff/(1000*60)) - 60*hr;
|
|
|
|
|
|
|
|
|
|
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
|
|
|
gsti = window.setTimeout(getStories, timeout);
|
|
|
|
|
//Adaptive update interval code END
|
|
|
|
|
|
|
|
|
|
$ei('diggIndicator').style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function populateDescription(e) {
|
|
|
|
|
XHR = e.target;
|
|
|
|
|
//Application.console.log(XHR.responseText);
|
|
|
|
|
data = decodeJson(XHR.responseText);
|
|
|
|
|
story = data.stories[0];
|
|
|
|
|
|
|
|
|
|
now = new Date();
|
|
|
|
|
if (story.promote_date != null)
|
|
|
|
|
then = new Date(story.promote_date*1000);
|
|
|
|
|
else
|
|
|
|
|
then = new Date(story.submit_date*1000);
|
|
|
|
|
diff = Math.max(now - then, 0)
|
|
|
|
|
|
|
|
|
|
hr = Math.floor(diff/(1000*3600));
|
|
|
|
|
min = Math.floor(diff/(1000*60)) - 60*hr;
|
|
|
|
|
|
|
|
|
|
listitems = document.getElementsByTagName('richlistitem');
|
|
|
|
|
for (i=0; i<listitems.length; i++) {
|
|
|
|
|
listitems[i].hideDesc();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
li = $ei('story_'+ story.id)
|
|
|
|
|
|
|
|
|
|
storyDate = ((hr > 0) && (min > 0)) ?
|
|
|
|
|
(hr + " hr " + min + " mins ago") :
|
|
|
|
|
((hr == 0) && (min > 0)) ?
|
|
|
|
|
(min + " mins ago") :
|
|
|
|
|
((hr == 0) && (min == 0)) ?
|
|
|
|
|
"just now" : "";
|
|
|
|
|
li.setAttribute('date', storyDate);
|
|
|
|
|
li.setAttribute('status', story.status);
|
|
|
|
|
li.setAttribute('container', story.container.name);
|
|
|
|
|
li.setAttribute('topic', story.topic.name);
|
|
|
|
|
li.setAttribute('username', story.user.name);
|
|
|
|
|
li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
|
|
|
|
|
li.setAttribute('diggs', story.diggs);
|
|
|
|
|
li.setAttribute('comments', story.comments);
|
|
|
|
|
li.setAttribute('desc', story.description);
|
|
|
|
|
li.setAttribute('link', story.link);
|
|
|
|
|
li.setAttribute('href', story.href);
|
|
|
|
|
|
|
|
|
|
li.read = true;
|
|
|
|
|
li.new = false;
|
|
|
|
|
li.showDesc()
|
|
|
|
|
|
|
|
|
|
$ei('diggIndicator').style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getStories() {
|
|
|
|
|
var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
|
|
|
|
|
ep = prefs.get("endpoint").value || '';
|
|
|
|
|
$ei('diggEndPoint').value = "digg" + ep;
|
|
|
|
|
|
|
|
|
|
fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
|
|
|
|
|
"?count=30" + "&type=json" +
|
|
|
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
|
|
|
populateStoryList);
|
|
|
|
|
//gsti = window.setTimeout(getStories, prefs.getIntPref("extensions.diggsidebar.updateinterval"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDescription(storyId) {
|
|
|
|
|
if (storyId == null) return;
|
|
|
|
|
fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
|
|
|
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
|
|
|
populateDescription);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createMenu() {
|
|
|
|
|
fetchData("http://services.digg.com/topics" + "?type=json" +
|
|
|
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
|
|
|
populateMenu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openInTab(href) {
|
|
|
|
|
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
|
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
|
|
|
|
|
.rootTreeItem
|
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
|
.getInterface(Components.interfaces.nsIDOMWindow);
|
|
|
|
|
mainWindow.getBrowser().selectedTab = mainWindow.getBrowser().addTab(href);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setEndPoint(ep) {
|
|
|
|
|
var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
|
|
|
|
|
prefs.get("endpoint").value = ep;
|
|
|
|
|
window.clearTimeout(gsti);
|
|
|
|
|
//window.clearInterval(psi);
|
|
|
|
|
getStories();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function togglePlayPause() {
|
|
|
|
|
if (playing) {
|
|
|
|
|
window.clearTimeout(gsti);
|
|
|
|
|
//window.clearInterval(psi);
|
|
|
|
|
$ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Play.png";
|
|
|
|
|
$ei('diggPlayPause').setAttribute("tooltiptext", "Click to Start autoupdate");
|
|
|
|
|
playing = false;
|
|
|
|
|
} else {
|
|
|
|
|
var prefs = Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs;
|
|
|
|
|
updateInterval = prefs.get("extensions.diggsidebar.updateinterval").value;
|
|
|
|
|
updateIntervalDecay = prefs.get("extensions.diggsidebar.updateintervaldecay").value;
|
|
|
|
|
|
|
|
|
|
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
|
|
|
//newTimeout = Math.round(timeout*parseInt($ei("diggIndicator").getAttribute('value'))/100);
|
|
|
|
|
gsti = window.setTimeout(getStories, timeout);
|
|
|
|
|
//psi = window.setInterval(showProgress, Math.round(timeout/100));
|
|
|
|
|
//window.setTimeout("window.clearInterval(psi)", newTimeout);
|
|
|
|
|
$ei('diggPlayPause').image = "chrome://diggsidebar/content/image/Pause.png";
|
|
|
|
|
$ei('diggPlayPause').setAttribute("tooltiptext", "Click to Pause autoupdate");
|
|
|
|
|
playing = true;
|
|
|
|
|
var listitems = document.getElementsByTagName('richlistitem');
|
|
|
|
|
for (var i=0; i<listitems.length; i++) {
|
|
|
|
|
listitems[i].hideDesc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decodeJson(string) {
|
|
|
|
|
var json = Components.classes["@mozilla.org/dom/json;1"]
|
|
|
|
|
.createInstance(Components.interfaces.nsIJSON);
|
|
|
|
|
return json.decode(string);
|
|
|
|
|
var li = document.getElementById('story_'+ story.id)
|
|
|
|
|
|
|
|
|
|
var relativeTime = ((hr > 0) && (min > 0)) ?
|
|
|
|
|
(hr + " hr " + min + " mins ago") :
|
|
|
|
|
((hr == 0) && (min > 0)) ?
|
|
|
|
|
(min + " mins ago") :
|
|
|
|
|
((hr == 0) && (min == 0)) ?
|
|
|
|
|
"just now" : "";
|
|
|
|
|
li.setAttribute('date', relativeTime);
|
|
|
|
|
li.setAttribute('status', story.status);
|
|
|
|
|
li.setAttribute('container', story.container.name);
|
|
|
|
|
li.setAttribute('topic', story.topic.name);
|
|
|
|
|
li.setAttribute('username', story.user.name);
|
|
|
|
|
li.setAttribute('userlink', "http://digg.com/users/" + story.user.name);
|
|
|
|
|
li.setAttribute('diggs', story.diggs);
|
|
|
|
|
li.setAttribute('comments', story.comments);
|
|
|
|
|
li.setAttribute('desc', story.description);
|
|
|
|
|
li.setAttribute('link', story.link);
|
|
|
|
|
li.setAttribute('href', story.href);
|
|
|
|
|
|
|
|
|
|
li.read = true;
|
|
|
|
|
li.new = false;
|
|
|
|
|
li.showDesc()
|
|
|
|
|
|
|
|
|
|
DiggSidebar.diggIndicator.style.display = 'none';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getStories: function() {
|
|
|
|
|
var ep = DiggSidebar.prefs.get("endpoint").value || '';
|
|
|
|
|
DiggSidebar.diggEndPoint.value = "digg" + ep;
|
|
|
|
|
|
|
|
|
|
DiggSidebar.fetchData("http://services.digg.com/stories" + ep.replace(/\/all/g, '') +
|
|
|
|
|
"?count=30" + "&type=json" +
|
|
|
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
|
|
|
DiggSidebar.populateStoryList);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDescription: function(storyId) {
|
|
|
|
|
if (storyId == null) return;
|
|
|
|
|
DiggSidebar.fetchData("http://services.digg.com/story/" + storyId + "?type=json" +
|
|
|
|
|
"&appkey=" + encodeURIComponent("http://diggsidebar.googlepages.com"),
|
|
|
|
|
DiggSidebar.populateDescription);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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 updateInterval = Math.round(
|
|
|
|
|
sum*1000/weights
|
|
|
|
|
.splice(0,Math.min(4, stories.length-1))
|
|
|
|
|
.reduce(function(a, b){return a + b;})
|
|
|
|
|
);
|
|
|
|
|
//Application.console.log(updateInterval);
|
|
|
|
|
|
|
|
|
|
if (updateInterval > 0) {
|
|
|
|
|
var previousUpdateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
|
|
|
|
DiggSidebar.prefs.get("updateinterval").value = updateInterval;
|
|
|
|
|
|
|
|
|
|
var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
|
|
|
|
|
|
|
|
|
if (previousUpdateInterval == updateInterval)
|
|
|
|
|
DiggSidebar.prefs.get("updateintervaldecay").value = updateIntervalDecay + 1;
|
|
|
|
|
else
|
|
|
|
|
DiggSidebar.prefs.get("updateintervaldecay").value = 0;
|
|
|
|
|
}
|
|
|
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
|
|
|
|
|
|
|
|
var updateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
|
|
|
|
var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
|
|
|
|
|
|
|
|
|
var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
|
|
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
|
|
|
|
//Adaptive update interval code END
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setEndPoint: function(ep) {
|
|
|
|
|
DiggSidebar.prefs.get("endpoint").value = ep;
|
|
|
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
|
|
|
DiggSidebar.getStories();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
togglePlayPause: function() {
|
|
|
|
|
if (DiggSidebar.playing) {
|
|
|
|
|
window.clearTimeout(DiggSidebar.timerId);
|
|
|
|
|
DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Play.png";
|
|
|
|
|
DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Start autoupdate");
|
|
|
|
|
DiggSidebar.playing = false;
|
|
|
|
|
} else {
|
|
|
|
|
var updateInterval = DiggSidebar.prefs.get("updateinterval").value;
|
|
|
|
|
var updateIntervalDecay = DiggSidebar.prefs.get("updateintervaldecay").value;
|
|
|
|
|
|
|
|
|
|
var timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
|
|
|
DiggSidebar.timerId = window.setTimeout(DiggSidebar.getStories, timeout);
|
|
|
|
|
DiggSidebar.diggPlayPause.image = "chrome://diggsidebar/content/image/Pause.png";
|
|
|
|
|
DiggSidebar.diggPlayPause.setAttribute("tooltiptext", "Click to Pause autoupdate");
|
|
|
|
|
DiggSidebar.playing = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function(){
|
|
|
|
|
var $ = function(id) {return document.getElementById(id)};
|
|
|
|
|
DiggSidebar.diggIndicator = $('diggIndicator');
|
|
|
|
|
DiggSidebar.topicPopup = $('topicPopup');
|
|
|
|
|
DiggSidebar.containerPopup = $('containerPopup');
|
|
|
|
|
DiggSidebar.storyList = $('storyList');
|
|
|
|
|
DiggSidebar.diggEndPoint = $('diggEndPoint');
|
|
|
|
|
DiggSidebar.diggPlayPause = $('diggPlayPause');
|
|
|
|
|
DiggSidebar.createMenu();
|
|
|
|
|
DiggSidebar.getStories();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*TODO
|
|
|
|
|
preferences
|
|
|
|
|
toolbar button
|
|
|
|
|