Skip to content

Instantly share code, notes, and snippets.

@kbauer
Last active October 8, 2016 19:37
Show Gist options
  • Save kbauer/8c285dd2e0c75eccd643fbc2cc145bca to your computer and use it in GitHub Desktop.
Save kbauer/8c285dd2e0c75eccd643fbc2cc145bca to your computer and use it in GitHub Desktop.
Bookmarklet for sorting the comic-list on http://www.comic-rocket.com/ (after logging in) by the number of unread pages. Confirmed to work in Chrome for Desktop and Chrome for iOS.
/* Comic Rocket New Pages Sorted.js */
var NW = 4; /* number width */
var MODE = 0; /* 1 for reordering the divs, 2 for creating a new format, 0 for dialog */
function g(x,y){return Array.from(x.getElementsByClassName(y))};
function ce(e,tag){var x=document.createElement(tag); if(e){e.appendChild(x)}; return x;}
function lpad(s,n){ s = ""+s;while(s.length<n){s=" "+s;} return s;}
function rpad(s,n){ s = ""+s;while(s.length<n){s=s+" ";} return s;}
var divs = g(document,"comics-item");
var data = divs.map(function(c){
var [current,total] = g(c,"progress-label")[0].textContent.split("/");
var unread = total - current;
var it = g(c,"comics-item-image")[0];
var name = it.textContent;
var href = it.href;
return { name, current, total, unread, href, div:c };
});
var datasorted = data.sort(function(a,b){ return b.unread - a.unread; }); /*most unread first*/
function sort_divs(){
/* sort the divs, keep the layout */
var parent = datasorted[0].div.parentElement;
parent.innerHTML = "";
for(c of datasorted){
parent.appendChild(c.div);
g(c.div, "progress-label")[0].innerText = "("+c.unread+" left) " + " " + c.current + "/" + c.total;
}
}
function overwrite_body(){
/* overwrite body entirely */
var b = ce(0,"body");
var p = ce(b,"pre");
for(c of datasorted){
var d = ce(p, "div");
var s = ce(d, "span");
var a = ce(d, "a");
a.href = c.href;
s.innerText = lpad(c.current,NW) + "/" + lpad(c.total,NW) + " " + rpad("("+c.unread+") ",NW+3);
a.innerText = c.name;
}
document.body = b;
}
switch(MODE){
case 0:
if(confirm("Rearrange (YES) or List (NO)?")) { action = sort_divs } else { action = overwrite_body };
break;
case 1:
action = sort_divs;
break;
case 2:
action = overwrite_body;
break;
}
action();
undefined; /* Needed for some reason... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment