Add caching support for closed threads

This commit is contained in:
Niles Rogoff 2017-06-30 00:24:35 -07:00
parent 48ed2f0ebd
commit 39c41053cb
No known key found for this signature in database
GPG Key ID: B78B908F23430F80
2 changed files with 53 additions and 18 deletions

View File

@ -34,20 +34,49 @@ var onload = function () {
var saved_title = GM_getValue(titlekey, id); var saved_title = GM_getValue(titlekey, id);
if (a.innerHTML.trim().length === 0) { if (a.innerHTML.trim().length === 0) {
a.innerHTML = "<span style='color: grey;'>Thread " + saved_title + " deleted</span>"; a.innerHTML = grey("Thread " + saved_title + " deleted");
continue; continue;
} }
GM_setValue(titlekey, a.innerHTML.trim()); GM_setValue(titlekey, a.innerHTML.trim());
the_fn(board, a, id);
var elem = document.createElement("span");
a.appendChild(elem);
elem.innerHTML = "Loading...";
var key = board + ":" + id;
var oldreplies = GM_getValue(key, 0);
var closed = false;
if (a.style.color.trim().length > 0) {
// post is closed.
closed = true;
}
var closedkey = key + ":closed_replies";
if (!closed) {
xhr_and_key(board, a, id, key, oldreplies, closed, elem, closedkey);
} else {
var closedreplies = GM_getValue(closedkey, "no");
if (closedreplies === "no") {
xhr_and_key(board, a, id, key, oldreplies, closed, elem, closedkey);
} else {
comparison_and_update_elem(key, closedreplies, a, elem, closed, oldreplies);
//elem.innerHTML = elem.innerHTML + " (cached)"; // debug
}
}
} }
}; };
var the_fn = (function the_fn(board, a, id) { var grey = function grey(text) {
return color("grey", text);
};
var red = function red(text) {
return color("red", text);
};
var color = function color(c, text) {
return "<span style='color: " + c + ";'>" + text + "</span>";
};
var elem = document.createElement("span"); var xhr_and_key = (function xhr_and_key(board, a, id, key, oldreplies, closed, elem, closedkey) {
a.appendChild(elem);
elem.innerHTML = "Loading...";
var xmlHttp = new XMLHttpRequest(); var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { xmlHttp.onreadystatechange = function() {
@ -57,19 +86,13 @@ var the_fn = (function the_fn(board, a, id) {
var idx = jsontext.indexOf("https://boards.dangeru.us/static"); var idx = jsontext.indexOf("https://boards.dangeru.us/static");
while (jsontext[idx] != '}') idx++; while (jsontext[idx] != '}') idx++;
var b = jsontext.slice(0, idx) + '"' + jsontext.slice(idx); var b = jsontext.slice(0, idx) + '"' + jsontext.slice(idx);
var key = board + ":" + id;
var oldreplies = GM_getValue(key, 0);
try { try {
var json = JSON.parse(b); var json = JSON.parse(b);
var replies = json.replies.length; var replies = json.replies.length;
if (oldreplies < replies) { comparison_and_update_elem(key, replies, a, elem, closed, oldreplies);
elem.innerHTML = "<span style='color:red;'>+" + (replies - oldreplies) + "</span>"; if (closed) {
// we have to wrap this in a closure because otherwise it clicking any post would only update the last post processed in this loop GM_setValue(closedkey, replies);
the_other_fn(key, replies, a, elem);
} else {
elem.innerHTML = "<span style='color: grey;'>" + replies + "</span>";
} }
} catch (e) { } catch (e) {
elem.innerHTML = "Error."; elem.innerHTML = "Error.";
console.log(e); console.log(e);
@ -80,12 +103,22 @@ var the_fn = (function the_fn(board, a, id) {
xmlHttp.send(null); xmlHttp.send(null);
}); });
var the_other_fn = (function(key, replies, a, elem) { var comparison_and_update_elem = function(key, replies, a, elem, closed, oldreplies) {
if (oldreplies < replies) {
elem.innerHTML = red("+" + (replies - oldreplies));
// we have to wrap this in a closure because otherwise it clicking any post would only update the last post processed in this loop
set_onclick_listener(key, replies, a, elem, closed);
} else {
elem.innerHTML = grey(replies);
}
};
var set_onclick_listener = function set_onclick_listener(key, replies, a, elem, closed) {
a.addEventListener("click", function() { a.addEventListener("click", function() {
GM_setValue(key, replies); GM_setValue(key, replies);
elem.innerHTML = "<span style='color: grey;'>" + replies + "</span>"; elem.innerHTML = grey(replies);
}); });
}); };
// In chrome, the userscript runs in a sandbox, and will never see these events // In chrome, the userscript runs in a sandbox, and will never see these events

View File

@ -2,6 +2,8 @@
It keeps track of how many replies a post has, and shows the number of new replies in red when there are new ones. It marks a post as fully read when you click on it in the list. It keeps track of how many replies a post has, and shows the number of new replies in red when there are new ones. It marks a post as fully read when you click on it in the list.
It caches the number of replies on closed posts to save bandwidth
It will also show deleted posts, and if you had seen the post before it was deleted it will tell you the title. It will also show deleted posts, and if you had seen the post before it was deleted it will tell you the title.
![](ss.png) ![](ss.png)