|
|
|
@ -22,24 +22,26 @@ |
|
|
|
|
var DiggSidebar = { |
|
|
|
|
homepageURL: "http://code.google.com/p/digg-sidebar/", |
|
|
|
|
prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs, |
|
|
|
|
observerService: Components.classes["@mozilla.org/observer-service;1"] |
|
|
|
|
.getService(Components.interfaces.nsIObserverService), |
|
|
|
|
storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh", |
|
|
|
|
updateInterval: 1000, |
|
|
|
|
updateIntervalDecay: 0, |
|
|
|
|
shownStoriesCount: 30,//make configurable
|
|
|
|
|
cachedStoriesCount: 1000,//make configurable
|
|
|
|
|
categories: ["All", "Popular", "Upcoming", "Hot", "Top"], |
|
|
|
|
|
|
|
|
|
endpoint: { |
|
|
|
|
container: null, |
|
|
|
|
topic: null, |
|
|
|
|
category: "all" |
|
|
|
|
}, |
|
|
|
|
updateInterval: 1000, |
|
|
|
|
updateIntervalDecay: 0, |
|
|
|
|
lastUpdateAt: new Date().getTime(), |
|
|
|
|
shownStoriesCount: 30, |
|
|
|
|
categories: ["All", "Popular", "Upcoming", "Hot", "Top"], |
|
|
|
|
timerId: null, |
|
|
|
|
storyIds: [], |
|
|
|
|
refreshing: false, |
|
|
|
|
stories: [], |
|
|
|
|
expandedStory: null, |
|
|
|
|
observerService: Components.classes["@mozilla.org/observer-service;1"] |
|
|
|
|
.getService(Components.interfaces.nsIObserverService), |
|
|
|
|
|
|
|
|
|
$ce: function (tagName) { |
|
|
|
|
return document.createElement(tagName); |
|
|
|
@ -84,21 +86,19 @@ var DiggSidebar = { |
|
|
|
|
var cmenupopup = DiggSidebar.$ce("menupopup"); |
|
|
|
|
cmenupopup.setAttribute("id", container.short_name + "Popup"); |
|
|
|
|
|
|
|
|
|
DiggSidebar.categories.forEach( |
|
|
|
|
function (category) { |
|
|
|
|
var cmenuitem = DiggSidebar.$ce("menuitem"); |
|
|
|
|
with (cmenuitem) { |
|
|
|
|
setAttribute("label", category); |
|
|
|
|
setAttribute("value", |
|
|
|
|
"/container/" + container.short_name + |
|
|
|
|
"/" + category.toLowerCase()); |
|
|
|
|
setAttribute("accesskey", category.charAt(0)); |
|
|
|
|
setAttribute("oncommand", |
|
|
|
|
"DiggSidebar.setEndPoint(this.value)"); |
|
|
|
|
} |
|
|
|
|
cmenupopup.appendChild(cmenuitem); |
|
|
|
|
DiggSidebar.categories.forEach(function (category) { |
|
|
|
|
var cmenuitem = DiggSidebar.$ce("menuitem"); |
|
|
|
|
with (cmenuitem) { |
|
|
|
|
setAttribute("label", category); |
|
|
|
|
setAttribute("value", |
|
|
|
|
"/container/" + container.short_name + |
|
|
|
|
"/" + category.toLowerCase()); |
|
|
|
|
setAttribute("accesskey", category.charAt(0)); |
|
|
|
|
setAttribute("oncommand", |
|
|
|
|
"DiggSidebar.setEndPoint(this.value)"); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
cmenupopup.appendChild(cmenuitem); |
|
|
|
|
}); |
|
|
|
|
cmenupopup.appendChild(DiggSidebar.$ce("menuseparator")); |
|
|
|
|
var topics = container.topics; |
|
|
|
|
topics.forEach(function (topic) { |
|
|
|
@ -159,14 +159,14 @@ var DiggSidebar = { |
|
|
|
|
DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var now = new Date(); |
|
|
|
|
|
|
|
|
|
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 then = (story.promote_date != null) ? |
|
|
|
|
new Date(story.promote_date * 1000) : |
|
|
|
|
new Date(story.submit_date * 1000); |
|
|
|
|
|
|
|
|
|
var diff = Math.max(now - then, 0); |
|
|
|
|
|
|
|
|
|
var hr = Math.floor(diff / (1000 * 3600)); |
|
|
|
@ -209,7 +209,10 @@ var DiggSidebar = { |
|
|
|
|
newStoryIds.push(story.id); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds); |
|
|
|
|
DiggSidebar.storyIds = newStoryIds.concat(DiggSidebar.storyIds); |
|
|
|
|
if (DiggSidebar.storyIds.length > DiggSidebar.cachedStoriesCount) { |
|
|
|
|
DiggSidebar.storyIds.length = DiggSidebar.cachedStoriesCount; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
showDescription: function (storyId) { |
|
|
|
@ -232,22 +235,30 @@ var DiggSidebar = { |
|
|
|
|
var ep = DiggSidebar.prefs.get("endpoint").value || ""; |
|
|
|
|
|
|
|
|
|
DiggSidebar.indicateActivity(); |
|
|
|
|
DiggSidebar.fetchData("http://services.digg.com/stories" + |
|
|
|
|
ep.replace(/\/all/g, "") + "?count=30" + "&type=json" + |
|
|
|
|
DiggSidebar.fetchData( |
|
|
|
|
"http://services.digg.com/stories" + ep.replace(/\/all/g, "") + |
|
|
|
|
"?count=30" + "&type=json" + |
|
|
|
|
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL), |
|
|
|
|
|
|
|
|
|
function (e) { |
|
|
|
|
var stories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories; |
|
|
|
|
stories.forEach(function (story) { story.id = parseInt(story.id); }); |
|
|
|
|
|
|
|
|
|
var newStories = stories.filter(function (story) { |
|
|
|
|
return DiggSidebar.storyIds.indexOf(story.id) == -1; |
|
|
|
|
stories.forEach(function (story) { |
|
|
|
|
story.id = parseInt(story.id); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
var newStories = [ |
|
|
|
|
story for each (story in stories) |
|
|
|
|
if (DiggSidebar.storyIds.indexOf(story.id) == -1)]; |
|
|
|
|
|
|
|
|
|
newStories.forEach(function (story) { |
|
|
|
|
story.category = DiggSidebar.endpoint.category; |
|
|
|
|
story.read = false; |
|
|
|
|
}); |
|
|
|
|
DiggSidebar.stories = newStories.concat(DiggSidebar.stories); |
|
|
|
|
if (DiggSidebar.stories.length > DiggSidebar.cachedStoriesCount) { |
|
|
|
|
DiggSidebar.stories.length = DiggSidebar.cachedStoriesCount; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
DiggSidebar.observerService.notifyObservers( |
|
|
|
|
null, DiggSidebar.storyListRefreshEventTopic, null); |
|
|
|
|
|
|
|
|
@ -268,7 +279,8 @@ var DiggSidebar = { |
|
|
|
|
|
|
|
|
|
createMenu: function () { |
|
|
|
|
DiggSidebar.indicateActivity(); |
|
|
|
|
DiggSidebar.fetchData("http://services.digg.com/containers" + "?type=json" + |
|
|
|
|
DiggSidebar.fetchData( |
|
|
|
|
"http://services.digg.com/containers" + "?type=json" + |
|
|
|
|
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL), |
|
|
|
|
DiggSidebar.populateMenu, DiggSidebar.errorHandler); |
|
|
|
|
}, |
|
|
|
@ -295,12 +307,9 @@ var DiggSidebar = { |
|
|
|
|
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; |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
sum * 1000 / |
|
|
|
|
weights.slice(0, Math.min(4, stories.length - 1)) |
|
|
|
|
.reduce(function (a, b) a + b)); |
|
|
|
|
|
|
|
|
|
if (newUpdateInterval > 0) { |
|
|
|
|
var previousUpdateInterval = DiggSidebar.updateInterval; |
|
|
|
@ -316,8 +325,8 @@ var DiggSidebar = { |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
calculateTimeout: function () { |
|
|
|
|
return Math.round( |
|
|
|
|
DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay))); |
|
|
|
|
return Math.round(DiggSidebar.updateInterval * |
|
|
|
|
Math.pow(1.5, DiggSidebar.updateIntervalDecay)); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
setEndPoint: function (ep) { |
|
|
|
|