commit 48ed2f0ebdb0b89ceca8db58dee8fd7d3a74f4ef Author: Niles Rogoff Date: Wed Jun 28 23:09:09 2017 -0700 Initial commit diff --git a/dangeru.user.js b/dangeru.user.js new file mode 100644 index 0000000..1e3a838 --- /dev/null +++ b/dangeru.user.js @@ -0,0 +1,101 @@ +// ==UserScript== +// @name danger/u/ catalog +// @namespace https://niles.xyz +// @include http://boards.dangeru.us/* +// @include https://boards.dangeru.us/* +// @version 1.0 +// @grant GM_getValue +// @grant GM_setValue +// @run-at document-end +// ==/UserScript== +var started = false; +var onload = function () { + + // Only start once + if (started) { + return; + } + started = true; + + var href = document.location.href; + href = href.substr(0, href.lastIndexOf("/")); + var board = href.substr(href.lastIndexOf("/") + 1); + var as = document.getElementsByTagName("a"); + for (var i = 0; i < as.length; i++) { + var a = as[i]; + if (a.href.indexOf("thread.php") < 0) { + continue; + } + + var idx = a.href.indexOf("thread.php"); + while (a.href[idx] != "=") idx++; + var id = a.href.substr(idx + 1); + var titlekey = board + ":" + id + ":title"; + var saved_title = GM_getValue(titlekey, id); + + if (a.innerHTML.trim().length === 0) { + a.innerHTML = "Thread " + saved_title + " deleted"; + continue; + } + GM_setValue(titlekey, a.innerHTML.trim()); + the_fn(board, a, id); + } + +}; + +var the_fn = (function the_fn(board, a, id) { + + var elem = document.createElement("span"); + a.appendChild(elem); + elem.innerHTML = "Loading..."; + + var xmlHttp = new XMLHttpRequest(); + xmlHttp.onreadystatechange = function() { + if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { + var jsontext = xmlHttp.responseText; + jsontext = jsontext.replace(/(?:\r\n|\r|\n)/g, " "); + var idx = jsontext.indexOf("https://boards.dangeru.us/static"); + while (jsontext[idx] != '}') idx++; + var b = jsontext.slice(0, idx) + '"' + jsontext.slice(idx); + var key = board + ":" + id; + var oldreplies = GM_getValue(key, 0); + try { + var json = JSON.parse(b); + var replies = json.replies.length; + if (oldreplies < replies) { + elem.innerHTML = "+" + (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 + the_other_fn(key, replies, a, elem); + } else { + elem.innerHTML = "" + replies + ""; + } + + } catch (e) { + elem.innerHTML = "Error."; + console.log(e); + } + } + }; + xmlHttp.open("GET", "https://boards.dangeru.us/api.php?type=thread&board=" + board + "&ln=250&thread=" + id, true); // true for asynchronous + xmlHttp.send(null); +}); + +var the_other_fn = (function(key, replies, a, elem) { + a.addEventListener("click", function() { + GM_setValue(key, replies); + elem.innerHTML = "" + replies + ""; + }); +}); + + +// In chrome, the userscript runs in a sandbox, and will never see these events +// Hence the run-at document-end +//document.addEventListener('DOMContentLoaded', onload); +//document.onload = onload; + +// One of these should work, and the started variable should prevent it from starting twice (I hope) +function GM_main() { + onload(); +} +onload(); + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..bb7c3ae --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +## Small userscript to browse danger/u/ more efficiently, based on lainmod + +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 will also show deleted posts, and if you had seen the post before it was deleted it will tell you the title. + +![](ss.png) diff --git a/ss.png b/ss.png new file mode 100644 index 0000000..82f6493 Binary files /dev/null and b/ss.png differ