415 lines
16 KiB
JavaScript
415 lines
16 KiB
JavaScript
var gsti;
|
|
var currentStories = new Array();
|
|
var playing = true;
|
|
|
|
//getElementById
|
|
function $ei(id, parentNode) {
|
|
parentNode = parentNode || document;
|
|
return parentNode.getElementById(id);
|
|
}
|
|
|
|
//getAttribute
|
|
function $a(parentNode, att) {
|
|
return parentNode.getAttribute(att);
|
|
}
|
|
|
|
//getElementByTagName
|
|
function $et(tagName, parentNode) {
|
|
parentNode = parentNode || document;
|
|
return parentNode.getElementsByTagName(tagName)[0];
|
|
}
|
|
|
|
//getElementByClassName
|
|
function $ec(className, parentNode) {
|
|
parentNode = parentNode || document;
|
|
elements = parentNode.getElementsByTagName("*");
|
|
for (i=0; i<elements.length; i++) {
|
|
if (elements[i].hasAttribute('class') && elements[i].getAttribute('class') == className) {
|
|
return elements[i];
|
|
}
|
|
}
|
|
|
|
}
|
|
//Xpath
|
|
function $xp(aNode, aExpr) {
|
|
var xpe = new XPathEvaluator();
|
|
var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
|
|
aNode.documentElement : aNode.ownerDocument.documentElement);
|
|
var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
|
|
var found = [];
|
|
var res;
|
|
while (res = result.iterateNext())
|
|
found.push(res);
|
|
return found;
|
|
}
|
|
String.prototype.escapeEntities = function() {
|
|
return this.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
}
|
|
|
|
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();
|
|
|
|
// QI the object to nsIDOMEventTarget to set event handlers on it:
|
|
|
|
request.QueryInterface(Components.interfaces.nsIDOMEventTarget);
|
|
request.addEventListener("load", handler, false);
|
|
request.addEventListener("error", handleError, false);
|
|
|
|
// 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);
|
|
}
|
|
|
|
for (i=0; i<topics.length; i++) {
|
|
topic = topics[i];
|
|
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'].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: []};
|
|
for (i=0; i<containers.length; i++) {
|
|
name = containers[i].name;
|
|
short_name = containers[i].short_name;
|
|
if (filteredContainers.name.indexOf(name) == -1) {
|
|
filteredContainers.name.push(name);
|
|
filteredContainers.short_name.push(short_name);
|
|
}
|
|
}
|
|
|
|
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));
|
|
|
|
menupopup = document.createElement('menupopup');
|
|
menupopup.setAttribute('id', filteredContainers.short_name[i] + 'Popup');
|
|
|
|
['All', 'Popular', 'Upcoming'].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);
|
|
}
|
|
);
|
|
|
|
menu.appendChild(menupopup);
|
|
$ei('containerPopup').appendChild(menu);
|
|
}
|
|
$ei('diggIndicator').style.display = 'none';
|
|
}
|
|
|
|
function populateStoryList(e) {
|
|
XHR = e.target;
|
|
//Application.console.log(XHR.responseText);
|
|
data = decodeJson(XHR.responseText);
|
|
stories = data.stories;
|
|
|
|
while ($ei('storyList').firstChild) {
|
|
$ei('storyList').removeChild($ei('storyList').firstChild);
|
|
}
|
|
|
|
var newStories = new Array();
|
|
Application.console.log(stories.length);
|
|
|
|
for (i=0; i<stories.length; i++) {
|
|
story = stories[i];
|
|
newStories.push(story.id);
|
|
//Application.console.log(i + " " + story.title + " " + story.id);
|
|
|
|
listitem = $ei('storyList').appendChild(document.createElement('richlistitem'));
|
|
listitem.id = "story_" + story.id;
|
|
|
|
htmllistitem = listitem.appendChild(
|
|
document.createElementNS("http://www.w3.org/1999/xhtml","html:div"));
|
|
htmllistitem.innerHTML = $ei('storyTitleFormat').innerHTML;
|
|
|
|
storyTitle = listitem.getElementsByAttribute('class', 'storyTitle')[0];
|
|
storyTitle.appendChild(document.createTextNode(story.title));
|
|
|
|
htmllistitem.innerHTML += $ei('storyDetailsFormat').innerHTML;
|
|
|
|
storyNew= listitem.getElementsByAttribute('class', 'storyNew')[0];
|
|
|
|
if (currentStories.indexOf(story.id) != -1) {
|
|
storyNew.style.textDecoration = "line-through";
|
|
storyNew.style.backgroundColor = "black";
|
|
storyNew.style.color = "white";
|
|
storyNew.title = "Old";
|
|
}
|
|
}
|
|
currentStories = newStories;
|
|
|
|
//Adaptive update interval code START
|
|
sum = 0;
|
|
weights = [0.4, 0.3, 0.2, 0.1]
|
|
for (i=0; i<4; i++) {
|
|
if (stories[0].promote_date != null)
|
|
diff = stories[i].promote_date - stories[i+1].promote_date;
|
|
else
|
|
diff = stories[i].submit_date - stories[i+1].submit_date;
|
|
|
|
sum += weights[i]*diff;
|
|
}
|
|
updateInterval = Math.round(sum*1000);
|
|
|
|
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
|
|
getService(Components.interfaces.nsIPrefBranch);
|
|
|
|
previousUpdateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
|
|
prefs.setIntPref("extensions.diggsidebar.updateinterval", updateInterval);
|
|
|
|
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
|
|
|
|
if (previousUpdateInterval == updateInterval)
|
|
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", (updateIntervalDecay + 1));
|
|
else
|
|
prefs.setIntPref("extensions.diggsidebar.updateintervaldecay", 0);
|
|
|
|
window.clearTimeout(gsti);
|
|
|
|
updateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
|
|
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
|
|
|
|
timeout = Math.round(updateInterval*(Math.pow(1.5, updateIntervalDecay)));
|
|
gsti = window.setTimeout(getStories, timeout);
|
|
//Adaptive update interval code END
|
|
|
|
$ei('diggIndicator').style.display = 'none';
|
|
|
|
//prefs.setIntPref("extensions.diggsidebar.progress", 100);
|
|
//$ei("diggIndicator").setAttribute('mode', 'determined');
|
|
//$ei("diggIndicator").setAttribute('value', 100);
|
|
//psi = window.setInterval(showProgress, Math.round(timeout/100));
|
|
//window.setTimeout("window.clearInterval(psi)", timeout);
|
|
}
|
|
|
|
//function showProgress() {
|
|
// var prefs = Components.classes["@mozilla.org/preferences-service;1"].
|
|
// getService(Components.interfaces.nsIPrefBranch);
|
|
// progress = prefs.getIntPref("extensions.diggsidebar.progress");
|
|
// prefs.setIntPref("extensions.diggsidebar.progress", (progress - 1));
|
|
//
|
|
// $ei("diggIndicator").setAttribute('value', (parseInt($ei("diggIndicator").getAttribute('value')) - 1));
|
|
//}
|
|
|
|
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].getElementsByAttribute('class', 'storyDetails')[0].style.display = 'none';
|
|
listitems[i].getElementsByAttribute('class', 'storyCEIcon')[0].src =
|
|
"chrome://diggsidebar/content/image/up.jpg";
|
|
}
|
|
|
|
storyListItem = $ei('story_'+ story.id)
|
|
storyDetails = storyListItem.getElementsByAttribute('class', 'storyDetails')[0];
|
|
storyDate = storyListItem.getElementsByAttribute('class', 'storyDate')[0];
|
|
storyStatus = storyListItem.getElementsByAttribute('class', 'storyStatus')[0];
|
|
storyContainer = storyListItem.getElementsByAttribute('class', 'storyContainer')[0];
|
|
storyTopic = storyListItem.getElementsByAttribute('class', 'storyTopic')[0];
|
|
storyUserName = storyListItem.getElementsByAttribute('class', 'storyUserName')[0];
|
|
storyDiggs = storyListItem.getElementsByAttribute('class', 'storyDiggs')[0];
|
|
storyComments = storyListItem.getElementsByAttribute('class', 'storyComments')[0];
|
|
storyDesc = storyListItem.getElementsByAttribute('class', 'storyDesc')[0];
|
|
storyLink = storyListItem.getElementsByAttribute('class', 'storyLink')[0];
|
|
storyHref = storyListItem.getElementsByAttribute('class', 'storyHref')[0];
|
|
|
|
storyCEIcon = storyListItem.getElementsByAttribute('class', 'storyCEIcon')[0];
|
|
storyRead = storyListItem.getElementsByAttribute('class', 'storyRead')[0];
|
|
|
|
storyDate.innerHTML = ((hr > 0) && (min > 0)) ?
|
|
(hr + " hr " + min + " mins ago") :
|
|
((hr == 0) && (min > 0)) ?
|
|
(min + " mins ago") :
|
|
((hr == 0) && (min == 0)) ?
|
|
"just now" : "";
|
|
storyStatus.innerHTML = story.status.escapeEntities();
|
|
storyContainer.innerHTML = story.container.name.escapeEntities();
|
|
storyTopic.innerHTML = story.topic.name.escapeEntities();
|
|
storyUserName.href = "http://digg.com/users/" + story.user.name;
|
|
storyUserName.innerHTML = story.user.name.escapeEntities();
|
|
storyDiggs.innerHTML = story.diggs;
|
|
storyComments.innerHTML = story.comments;
|
|
storyDesc.innerHTML = story.description.escapeEntities();
|
|
storyLink.href = story.link;
|
|
storyHref.href = story.href;
|
|
|
|
storyCEIcon.src = "chrome://diggsidebar/content/image/down.jpg";
|
|
storyRead.style.textDecoration = "line-through";
|
|
storyRead.style.backgroundColor = "black";
|
|
storyRead.style.color = "white";
|
|
storyRead.title = "Read";
|
|
storyNew.style.textDecoration = "line-through";
|
|
storyNew.style.backgroundColor = "black";
|
|
storyNew.style.color = "white";
|
|
storyNew.title = "Old";
|
|
|
|
storyDetails.style.display = 'block';
|
|
|
|
$ei('diggIndicator').style.display = 'none';
|
|
}
|
|
|
|
function getStories() {
|
|
var prefs = Components.classes["@mozilla.org/preferences-service;1"].
|
|
getService(Components.interfaces.nsIPrefBranch);
|
|
ep = prefs.getCharPref("extensions.diggsidebar.endpoint") || '';
|
|
$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 = Components.classes["@mozilla.org/preferences-service;1"].
|
|
getService(Components.interfaces.nsIPrefBranch);
|
|
prefs.setCharPref("extensions.diggsidebar.endpoint", 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 = Components.classes["@mozilla.org/preferences-service;1"].
|
|
getService(Components.interfaces.nsIPrefBranch);
|
|
updateInterval = prefs.getIntPref("extensions.diggsidebar.updateinterval")
|
|
updateIntervalDecay = prefs.getIntPref("extensions.diggsidebar.updateintervaldecay");
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
function decodeJson(string) {
|
|
var json = Components.classes["@mozilla.org/dom/json;1"]
|
|
.createInstance(Components.interfaces.nsIJSON);
|
|
return json.decode(string);
|
|
};
|
|
|
|
/*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
|
|
*/
|