Efficient AO3

Making AO3 an efficient reference

As of 2018-02-14. See the latest version.

// ==UserScript==
// @name         Efficient AO3
// @namespace    https://everlarkarchivetms.tumblr.com/
// @version      0.82
// @description  Making AO3 an efficient reference
// @author       EverlarkArchive
// @match        SEE INFO ON WHAT TO PUT HERE
// @grant        GM_setValue
// @grant        GM_getValue
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// ==/UserScript==


/*  Known bugs/Limitations/To Do List:
-BUG: Last Chapter link color will not change for fics set to temporary 1 star because of pairing
-BUG: Expanding/Collapsing summary while in 1,2,4,5 star will revert back to default color. (have to reload page to set color appropriately)
-LIMIT: Cannot have multiple preferred pairings. Cannot use two versions of this script for the same AO3 works link. Think in terms of OTP
-TODO: Allow for To Read to coexist with other settings
-TODO: Either add code or create another script for other sections of AO3   */

/*  Misc:
-By copying and pasting this script, you acknowledge the author is not to be held responsible if your computer craps
itself, sprouts legs and runs away, or any other problem you may have with it (in other words, you’ve read the disclaimer).
-For the privacy/security-minded, this script does not access any site outside of AO3 other than jquery from Google.
-please let me know about any additional bugs. I'll try to get to them as soon as possible (as free time becomes available)
-you can slso request features but I may or may not decide to add. If I do, again, as free time becomes available   */


const ReadLinksCount = 20;  // Number of links in displayed Read List. Maximum is 20. 0 to turn off.
const ShipList = "Katniss/Peeta";  /* Ship to be prioritized.  "" = nothing proiritized. Single name for each char in rel preferred. (EX: Katniss/Peeta)
      Non-priority ships listed first will be grayd out as 1 star but not added to 1 star list.
      To override individually, set fic using button. To return to temp status, unselect and reload   */
const Read = "#ecffec";  // backcolor for to-read items. default: #ecffec
const Star1 = "lightgray";  // font color for 1 star items default: lightgray
const Star2 = "darkgray";  // font color for 2 star items default: darkgray
const Star3 = "#4d4d4d";  // font color for 3 star items default: #4d4d4d
const Star4 = "black";  // font color for 4 & 5 star items default: black
const Own_Color = "#ededed";  // backcolor for author's own fics. Can be overridden with another setting. default: #ededed
const Date_Format = "1";  // use number associated (0) Day/Month/Year format or (1) Month/Day/Year format. Be careful


const None = "transparent"; // DO NOT CHG -- reset color default: transparent
var hideList=[],toReadList=[],star1List=[],star2List=[],star3List=[],star4List=[],star5List=[];
var toReadLinks=[];

var myFunctions = window.myFunctions = {};
myFunctions.addCss=function () {
    //toReadList=["12918618"];
    if(GM_getValue('hide','')!=""){hideList=GM_getValue('hide');}
    if(GM_getValue('toread','')!=""){toReadList=GM_getValue('toread');}
    if(GM_getValue('star1','')!=""){star1List=GM_getValue('star1');}
    if(GM_getValue('star2','')!=""){star2List=GM_getValue('star2');}
    if(GM_getValue('star3','')!=""){star3List=GM_getValue('star3');}
    if(GM_getValue('star4','')!=""){star4List=GM_getValue('star4');}
    if(GM_getValue('star5','')!=""){star5List=GM_getValue('star5');}
    var head = document.getElementsByTagName('body')[0];
    var newCss = document.createElement('style');
    newCss.type = "text/css";//
    newCss.innerHTML = "";
    newCss.innerHTML = "em {visibility:hidden; font-style:initial; font-size:100%; font-weight:initial; width:auto; background-color:#555; color:#fff; border-radius:6px;";
    newCss.innerHTML+=   "padding:8px; position:absolute; z-index:1; bottom:-5px; margin-left:10px; white-space:nowrap; }";
    newCss.innerHTML+= "*:hover em{visibility: visible;} *:not(:hover) em{visibility: hidden;} ";
    newCss.innerHTML+= ".totheLeft{display:inline-block; margin-right:2%} .totheRight{float:right;}";
    newCss.innerHTML+= ".required-tags{position:static !important;} li{position: static !important}";
    newCss.innerHTML+= ".dd1{position: relative; display: inline-block; margin:0px 30px}";
    newCss.innerHTML+= ".dd1 a:hover {background-color: #f1f1f1}";
    newCss.innerHTML+= ".dd1btn{background-color: transparent; color: black; font-size: 16px;border: none;cursor: pointer;}";
    newCss.innerHTML+= ".dd1btn:hover {background-color: #ecffec;}";
    newCss.innerHTML+= ".dd1-content {display: none; position: absolute; background-color: #f9f9f9; width: 100px; height:190px; overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1;}";
    newCss.innerHTML+= ".dd1-content a {color: black; text-decoration: none; display: block; border:none; margin-left:10px; margin-top:5px;}";
    newCss.innerHTML+= ".show {display:block;}";
    newCss.innerHTML+= "a[href^='/works/']:link, a[href^='/users/']:link{  font: 16px Arial,sans-serif; color:"+Star3+" }";
    newCss.innerHTML+= "a[href^='/works/']:visited, a[href^='/users/']:visited{color:"+Star3+" }";
    newCss.innerHTML+= "a[href$='comments'], a[href$='bookmarks'],#chapter,.readlink{ font-size:inherit !important;}";
    newCss.innerHTML+= ".toReadLinks{font: 14px Arial,sans-serif;}";
    newCss.innerHTML+= "a.i{border: none; font-size:inherit !important; text-decoration:none; font-style:italic; cursor:text; position: relative; display: inline-block;}";
    newCss.innerHTML+= "a.i:hover{color:inherit;}";
    newCss.innerHTML+= "td,tr {padding:0px}";
    newCss.innerHTML+= "ul.required-tags{width:60px !important;column-gap:0px !important;}";
    newCss.innerHTML+= "ul.required-tags li{width:20px;height:20px;}";
    newCss.innerHTML+= "ul,table,tr,td{background-color:inherit;";
    newCss.innerHTML+= "table{padding:20px}";
    newCss.innerHTML+= "tbody{width:100px !important}";
    newCss.innerHTML+= ".truncateIt{margin-bottom:3px; width: 90%; height:22pt; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}";//width: 50px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    head.appendChild(newCss);
    myFunctions.BeginWorking();
};
myFunctions.BeginWorking=function (){
    try{
        var works,sq,titleauthor,fandom,datetime,rel,summ,wccks;
        works = document.getElementsByClassName("work blurb group");
        var i,a;
        for(i=works.length-1;i>=0;i--){
            var work = works[i];
            var id = work.id.replace("work_","");
            var sqs = work.getElementsByClassName("required-tags")[0].getElementsByTagName("li");
            sq = "<ul class='required-tags'><table><tbody><tr>";
            sq+= "<td>"+sqs[0].outerHTML+"</td><td>"+sqs[2].outerHTML+"</td></tr><tr>";
            sq+= "<td>"+sqs[1].outerHTML+"</td><td>"+sqs[3].outerHTML+"</td>";
            sq+= "</tr></tbody></table></ul>";
            var title,author,cssClass=" class='worktext' ";
            title = work.getElementsByTagName("h4")[0].getElementsByTagName("a")[0].outerHTML;title = title.substr(0,title.indexOf(">"))+cssClass+title.substr(title.indexOf(">"));
            author = work.getElementsByTagName("h4")[0].getElementsByTagName("a")[1].outerHTML;author = author.substr(0,author.indexOf(">"))+cssClass+author.substr(author.indexOf(">"));
            var fandoms = work.getElementsByClassName("fandoms heading")[0].getElementsByTagName("a"); //list of a
            fandom=[];for(a=0;a<fandoms.length;a++){ fandom.push(fandoms[a].innerHTML); }
            datetime= myFunctions.convertDateTime(work.getElementsByClassName("datetime")[0].innerHTML);
            var rels = work.getElementsByClassName("relationships");//list of a w/relationships
            rel=[];for(a=0;a<rels.length;a++){ rel.push(rels[a].getElementsByTagName("a")[0].innerHTML); }// }
            var summs = work.getElementsByClassName("userstuff summary");
            if(summs.length!=0){
                summ=summs[0].innerHTML.replace(/<p>&nbsp;<\/p>/g,"").replace(/<b>/g,"").replace(/<\/b>/g,"");//summ="<div class='worktext truncateIt'>"+summ+"</div>";
                summ="<div class='worktext' id='summ"+id+"' style='color:"+Star3+";display: inline-block; margin-bottom:3px; width: 90%; height:22pt; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;'>" +summ+"</div>";
            }else{summ="";}
            wccks = work.getElementsByTagName("dl")[0].getElementsByTagName("dd");// 0(words), 1(chapt), 2(comnts), 3(kudos), 4(bookmarks)
            wcck=[];for(a=1;a<wccks.length;a++){
                if(wccks[a].outerHTML.includes("words")){wcck[0]=wccks[a].innerHTML;}
                else if (wccks[a].outerHTML.includes("chapters")){wcck[1]=wccks[a].innerHTML;}
                else if (wccks[a].outerHTML.includes("kudos")){var kudo=wccks[a].getElementsByTagName("a")[0]; kudo.className="worktext"; wcck[3]=kudo.outerHTML; }
                else if (wccks[a].outerHTML.includes("comments")){ var comm=wccks[a].getElementsByTagName("a")[0];
                    comm.href= "http://archiveofourown.org/works/"+id+"?show_comments=true&view_full_work=true#comments"; comm.className= "worktext"; wcck[2]=comm.outerHTML;
                }
                else if (wccks[a].outerHTML.includes("bookmarks")){var bkmk=wccks[a].getElementsByTagName("a")[0]; bkmk.className= "worktext"; wcck[4]=bkmk.outerHTML;}
            }
            myFunctions.removeElements(work);
            var str = "";
            str+="<div style='float:left;display:inline !important;margin:0px 10px 3px 0px;'>"+sq+"</div>";
            str+="<div style='display:inline !important;margin-left:0px;'>";
              str+=title+" <span class='worktext'>-</span> "+author;
              str+=" <a href=\"#/\" id=\"down"+id+"\" style=\"font-size:12pt;border:0\">\u2304</a><a href=\"#/\" id=\"up"+id+"\" style=\"border:0\">^</a>";
              str+="<span class='totheRight worktext'style='width:275px'><div id='dattime' style='font-size:75%;vertical-align:top;float:right'>Upd: "+datetime+"</div></span><br style='margin-bottom:5px'>";
              str+=summ;
            str+="</div>";
            str+="<div style='width:100%'>";
            str+="<a href='#/' class='i worktext'>Fandoms <em>"+fandom.join("<br>")+"</em></a><span class='totheLeft worktext'></span>";//  title='"+fandom.join(", ")+"'
            str+="<a href='#/' class='i worktext'>Relationships <em>"+rel.join("<br>")+"</em></a><span class='totheLeft worktext'></span>";//
            str+="<span class='totheLeft worktext'><i>Words:</i> "+wcck[0]+"</span>";
            str+="<span class='totheLeft worktext'><i>Chapters:</i> "+"<span id='chapters'>"+wcck[1]+"</span>"+"</span>";
            if (wcck[2]!=null){str+="<span class='totheLeft worktext'><i>Comments:</i> "+wcck[2]+"</span>";}
            if (wcck[3]!=null){str+="<span class='totheLeft worktext'><i>Kudos:</i> "+wcck[3]+"</span>";}
            if (wcck[4]!=null){str+="<span class='totheLeft worktext'><i>Bookmarks:</i> "+wcck[4]+"</span>";}
            str+="</div>";
            work.innerHTML = str;
            insertUpClick(id); insertDownClick(id);
            summsUp();/**/
            var newRight = work.getElementsByClassName("totheRight")[0];
            var dropBtn = myFunctions.addDropDown(id,"margin-right:30px;border:0px");
            newRight.insertBefore(dropBtn,newRight.childNodes[0]);
            if(hideList.indexOf(id)>-1||toReadList.indexOf(id)>-1||star1List.indexOf(id)>-1||star2List.indexOf(id)>-1||star3List.indexOf(id)>-1||star4List.indexOf(id)>-1||star5List.indexOf(id)>-1){
                if(hideList.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='0']")[0]);}
                else if(toReadList.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='1']")[0]);}//}
                else if(star1List.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='2']")[0]);}//}
                else if(star2List.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='3']")[0]);}//}
                else if(star3List.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='4']")[0]);}//}
                else if(star4List.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='5']")[0]);}//}
                else if(star5List.indexOf(id)>-1){myFunctions.selectIt(dropBtn.querySelectorAll("[id='6']")[0]);}//}
            }
            else{
                if(work.className.indexOf("own")>-1){work.style.backgroundColor=Own_Color;}
                else{
                    var searchRel;
                    if (Number(ShipList.length)>0){
                        searchRel=ShipList.toLowerCase().split("/");
                        if (rel[0].toLowerCase().indexOf(searchRel[0])==-1||rel[0].toLowerCase().indexOf(searchRel[1])==-1){
                            myFunctions.toFormat1_4(work,Star1);
                        }
                    }
                }
            }
        }/**/
        addLastChapters(); injectReadLinks(); addReadLinks(); injectClearCbo();
    }
    catch(err){}
};
myFunctions.convertDateTime=function(datetime){
    var dt=new Date(Date.parse(datetime));
    var m=myFunctions.padZeros((dt.getMonth()+1).toString(),2);
    var d=myFunctions.padZeros((dt.getDate()).toString(),2);
    var y=dt.getFullYear();
    if (Date_Format==0){return d+"/"+m+"/"+y;} else {return m+"/"+d+"/"+y;}
};
myFunctions.padZeros=function(str,sp){
    if (str.length == sp){return str;}
    while(str.length!=sp){str="0"+str;}
    return str;
};
function insertUpClick(id){ $(document).on('click','#up'+id,function(){summUp(id);return false;}); }
function insertDownClick(id){ $(document).on('click','#down'+id,function(){summDown(id);return false;}); }
function summUp(id){
    var summ = document.getElementById("summ"+id);
    summ.style.cssText="display: inline-block; margin-bottom:3px; width: 90%; height:22pt; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;";
    $('a[id="down'+id+'"]').show();$('a[id="up'+id+'"]').hide();
}
function summDown(id){
    var summ = document.getElementById("summ"+id);
    summ.removeAttribute("style");
    $('a[id="up'+id+'"]').show();$('a[id="down'+id+'"]').hide();
}
function summsUp(){ $('a[id^="up"]').each(function(){ $(this).hide(); }); $('a[id^="down"]').each(function(){ $(this).show(); }); }
function summsDown(){ $('a[id^="up"]').each(function(){ $(this).show(); }); $('a[id^="down"]').each(function(){ $(this).hide(); }); }

myFunctions.removeElements=function (blurb){var i; for(i=blurb.children.length-1;i>=0;i--){blurb.children[i].outerHTML = "";}};
myFunctions.addDropDown=function (id,style=""){
    var di="_"+id;
    var drop = document.createElement("div"); drop.className = "dd1"; drop.style.cssFloat="left";
    var btn = document.createElement("input"); btn.type="button"; btn.className="dd1btn"; btn.value="Select";
    btn.addEventListener('click', function() { myFunctions.dropItDown(di); }, false);drop.appendChild(btn);
    var opts = document.createElement("div"); opts.className="dd1-content";opts.id=di;
    var array = ["Hide","to Read","\u2605","\u2605\u2605","\u2605\u2605\u2605","\u2605\u2605\u2605\u2605","\u2605\u2605\u2605\u2605\u2605","Unselect"];//;
    for(i=0;i<array.length;i++){
        var opt = document.createElement("a");
        opt.href="#/"; opt.id=i; opt.onclick=function(){myFunctions.selectIt(this);return false;};opt.innerHTML = array[i];
        opts.appendChild(opt);
    }
    drop.appendChild(opts);
    return(drop);
};

myFunctions.dropItDown=function (id) {
    var btns = document.querySelectorAll("[id^='_']");
    var i,oi=0;for(i=0;i<btns.length;i++){ if (btns[i].classList.contains("show")){oi=btns[i].id;} }
    if(oi==0||oi==id){document.getElementById(id).classList.toggle('show');}else{document.getElementById(oi).classList.remove("show");document.getElementById(id).classList.add('show');}
};
myFunctions.selectIt=function (opt){
    var opts = opt.parentNode;
    var btn = opts.parentNode.getElementsByTagName("input")[0];
    var work = opts.parentNode.parentNode.parentNode.parentNode;
    var workID = work.id.replace("work_","");
    switch(opt.innerHTML){
        case "Hide":
            myFunctions.toDefault(work,"hide");
            toFormatHide(work);
            if (hideList.indexOf(workID)==-1){hideList.push(workID);GM_setValue('hide',hideList);}
            break;
        case "to Read":
            myFunctions.toDefault(work,"toread");
            work.style.backgroundColor=Read;
            if (toReadList.indexOf(workID)==-1){toReadList.push(workID);GM_setValue('toread',toReadList);addReadLink(workID);}
            break;
        case "\u2605":
            myFunctions.toDefault(work,"star1");
            myFunctions.toFormat1_4(work,Star1);
            if (star1List.indexOf(workID)==-1){star1List.push(workID);GM_setValue('star1',star1List);}
            break;
        case "\u2605\u2605":
            myFunctions.toDefault(work,"star2");
            myFunctions.toFormat1_4(work,Star2);
            if (star2List.indexOf(workID)==-1){star2List.push(workID);GM_setValue('star2',star2List);}
            break;
        case "\u2605\u2605\u2605":
            myFunctions.toDefault(work,"star3");
            myFunctions.toFormat1_4(work,Star3);
            if (star3List.indexOf(workID)==-1){star3List.push(workID);GM_setValue('star3',star3List);}
            break;
        case "\u2605\u2605\u2605\u2605":
            myFunctions.toDefault(work,"star4");
            myFunctions.toFormat1_4(work,Star4);
            if (star4List.indexOf(workID)==-1){star4List.push(workID);GM_setValue('star4',star4List);}
            break;
        case "\u2605\u2605\u2605\u2605\u2605":
            myFunctions.toDefault(work,"star5");
            myFunctions.toFormat1_4(work,Star4);
            var title = work.querySelectorAll("[href^='/works/']")[0];
            title.innerHTML="<span style='text-shadow: 4px -4px 4px red,  2px -2px 3px #e6d71a,6px -6px 9px #0e0504'>"+myFunctions.toFormat5(title.innerHTML)+"</span>";
            if (star5List.indexOf(workID)==-1){star5List.push(workID);GM_setValue('star5',star5List);}
            break;
        case "Unselect":
            myFunctions.toDefault(work,"");
            if (work.className.indexOf("own")>-1){work.style.backgroundColor=Own_Color;}
            //make sure not hidden
            break;
    }
    if(opt.innerHTML=="Unselect"){btn.value = "Select";}else{btn.value = opt.innerHTML;}
    opts.classList.remove("show");
    var cbo = document.getElementById("cboClear"); if (cbo!=null){ cbo.value="Lists"; var ul=document.getElementByID("lstClear"); ul.innerHTML=""; }

};
myFunctions.toDefault=function (work,item){
    var workID = work.id.replace("work_","");var x;
    if (item!="hide"){x = hideList.indexOf(workID); if(x!=-1){hideList.splice(x,1);GM_setValue('hide',hideList);}}
    if (item!="toread"){x = toReadList.indexOf(workID); if(x!=-1){toReadList.splice(x,1);GM_setValue('toread',toReadList);removeReadLink(workID);}}//addReadLinks();
    if (item!="star1"){x = star1List.indexOf(workID); if(x!=-1){star1List.splice(x,1); GM_setValue('star1',star1List); }}
    if (item!="star2"){x = star2List.indexOf(workID); if(x!=-1){star2List.splice(x,1);GM_setValue('star2',star2List);}}
    if (item!="star3"){x = star3List.indexOf(workID); if(x!=-1){star3List.splice(x,1);GM_setValue('star3',star3List);}}
    if (item!="star4"){x = star4List.indexOf(workID); if(x!=-1){star4List.splice(x,1);GM_setValue('star4',star4List);}}
    if (item!="star5"){x = star5List.indexOf(workID); if(x!=-1){star5List.splice(x,1);GM_setValue('star5',star5List);}}
    var text;
    var title=work.querySelectorAll("[href^='/works/']")[0];
    if (title.innerHTML.indexOf("<span")!=-1){
        title.innerHTML=title.innerHTML; title.innerHTML=title.innerHTML.replace(title.innerHTML.substr(0,title.innerHTML.indexOf(">")+1),"");
        while(title.innerHTML.indexOf("<")>-1){
            var a,b; a = title.innerHTML.indexOf("<"); b=title.innerHTML.indexOf(">");
            title.innerHTML=title.innerHTML.replace(title.innerHTML.substr(a,(b+1)-a),"");
        }
    }
    if (work.style.backgroundColor==myFunctions.hex2RGB(Read)){removeReadLink(workID,false);}work.style.backgroundColor=None;
    var chgd = work.getElementsByClassName("worktext");
    var i;for(i=0;i<chgd.length;i++){chgd[i].style.color=Star3;}
    toUnhide(work);

};
myFunctions.hex2RGB=function (Hex){
    Hex = Hex.trim(); var r,g,b;
    if (Hex.indexOf("#")>-1){Hex=Hex.replace("#","");}
    if (Hex.length==3){ Hex=Hex[0]+Hex[0]+Hex[1]+Hex[1]+Hex[2]+Hex[2]; }
    r=parseInt(Hex.substr(0,2),16); g=parseInt(Hex.substr(2,2),16); b=parseInt(Hex.substr(4),16);
    return "rgb("+r+", "+g+", "+b+")";
};
function toUnhide(work){
    if ($(work).children().first().is(':hidden')){
        $(work).children().each(function(){//divs
            if ($(this).html().includes("dd1")){ // title, author, up, down, button, date
                $(this).children().each(function(){
                    if ($(this).html().includes("dd1")) {$(this).children(":not(.dd1)").show();}
                    else {$(this).show();}
                });/**/
            }
            else{$(this).show();}
        });
        summUp($(work).attr('id').replace("work_",""));
    }
}
function toFormatHide(work){
    if ($(work).children().first().is(':hidden')){}else{
        $(work).children().each(function(){//div
            if ($(this).html().includes("dd1")){
                $(this).children().each(function(){//span totheRight
                    if ($(this).html().includes("dd1")){$(this).children(":not(.dd1)").hide();} else {$(this).hide();}
                });/**/
            }
            else{$(this).hide();}
        });
    }
}
myFunctions.toFormat1_4=function (work,color){var chgd = work.getElementsByClassName("worktext"); var i; for(i=0;i<chgd.length;i++){chgd[i].style.color=color;}};
myFunctions.toFormat5=function (title){
    var lst=[],f, o =Math.ceil((title.length - 1) / 3); f=Math.floor((Math.random()*o)+1);
    var ff;for(ff=1;ff<title.length;ff++){
        var x=Math.floor(Math.random()*title.length);
        if (lst.length==0||lst.indexOf(x)==-1){lst.push(x);}
        if (lst.length==f){break;}
    }
    lst=lst.sort(sortNumber);
    for(ff=lst.length-1;ff>=0;ff--){
        title=title.substr(0,lst[ff]+1)+"</i>"+title.substr(lst[ff]+1);
        title=title.substr(0,lst[ff])+"<i style='text-shadow: 6px -8px 4px red,  4px -4px 3px #e6d71a,8px -6px 9px #0e0504'>"+title.substr(lst[ff]);
    }
    return title;
};function sortNumber(a,b) { return a - b; }

function addLastChapters(){
    $('.work.blurb.group').each(function () {
        var path = $(this);
        var id = $(this).attr('id').replace("work_","");
        var ch=$(this).find('#chapters').first().html().split("/");
        if (ch[0]=="1"){return;}
        var och = $(this).find('#chapters');
        $.get('/works/' + id + '/navigate', function (page) {
            var first = $(page).find('ol li').first().find('span').html().replace("(","").replace(")","");
            var newdate;
            var dt=new Date(Date.parse(first));//alert(id+"  first:"+first+"  dt:"+dt+"  "+dt.getMonth()+"/"+dt.getDate()+"/"+dt.getFullYear());
            var m=myFunctions.padZeros((dt.getMonth()+1).toString(),2);
            var d=myFunctions.padZeros((dt.getDate()+1).toString(),2);
            var y=dt.getFullYear();
            if (Date_Format==0){newdate= d+"/"+m+"/"+y;} else {newdate=  m+"/"+d+"/"+y;}
            $(path).find('#dattime').first().prepend('<span style="padding-right:20px">Pub: '+newdate+'<br></span>');
            var last = $(page).find('ol li').last().find('a').attr('href');
            $(och).html('<a href="' + last + '" id="chapter" class="worktext" title="Go to last chapter">'+ch[0]+'</a>/'+ch[1]);
        });
    });
}
function injectReadLinks(){
    if (ReadLinksCount==0){return;}
    var el = $('.narrow-hidden.filters').children().eq(1);
    $(el).before('<center><b>To Read</b></center>').after('<p class="toReadLinks"></p>');
}
function addReadLinks(){
    if (ReadLinksCount==0){return;}
    var a,z; if (toReadList.length<ReadLinksCount){z=toReadList.length;}else{if(ReadLinksCount>20){z=20;}else{z=ReadLinksCount;}}
    var newel = $('.toReadLinks'); if(!($(newel).is(':empty'))){$(newel).empty();}//$('.toReadLinks).find('ul').eq(0)
    $(newel).append('<ul style=""></ul>'); newel=$(newel).find('ul').first();
    for(i=0;i<z;i++){ $(newel).append('<li id="readlink'+toReadList[i]+'" style="list-style-type:none;margin:0;padding:0;padding-bottom:2px"></li>'); }
    $(newel).children().each(function(){
        var id = $(this).attr("id").replace("readlink","");
        var child = $(this);
        jQuery.get('/works/' + id + '/navigate', function (page) {
            var ficTitle = $(page).find('.works-navigate.region').find('a').eq(0).text();
            var fID = $(page).find('.works-navigate.region').find('a[href^="/works/"]').attr('href').replace("/works/","");
            var ficAuthor = $(page).find('.works-navigate.region').find('a').eq(1).text();
            var num = Number(ficTitle.length+ficAuthor.length);
            if (Number(num) > Number(40)){
                var o;
                if (Number(ficAuthor.length)>Number(20)){
                    if(Number(ficTitle.length)>Number(20)){ficAuthor=ficAuthor.substr(0,17)+"...";}else{ o = 20-Number(ficTitle.length);o+=19;ficAuthor=ficAuthor.substr(0,o)+"..."; }
                }
                o = 20-Number(ficAuthor.length);
                if (Number(ficTitle.length)>Number(25)){o+=19;ficTitle=ficTitle.substr(0,o)+"...";}
            }
            var style="margin-left:10px; padding: 0px 5px; width: 100px; height: 100px; color: black; background: lightgray; -moz-border-radius: 70px; ";
            style+="-webkit-border-radius: 70px; border-radius: 70px;";
            var remove = " <a href=\"#/\" id=\"remove"+fID+"\" class=\"readlink\" style=\""+style+"\" title=\"Remove from \"To Read\" list\">x</a>\n";
            $(child).html('<a href="/works/' + fID + '" class="readlink">'+ficTitle+'</a> - '+ficAuthor+remove);
            $(document).on('click','#remove'+fID,function(){removeReadLink(fID);return false;});
        });/**/
    });
}
function addReadLink(id){
    if ($('#readlink'+id).length >0){return;}
    var newl = $('.toReadLinks').find('ul').first().append('<li id="readlink'+id+'" style="list-style-type:none;margin:0;padding:0;padding-bottom:2px"></li>');
    newl = $('#readlink'+id);
    jQuery.get('/works/' + id + '/navigate', function (page) {
        var ficTitle = $(page).find('.works-navigate.region').find('a').eq(0).text();
        var ficAuthor = $(page).find('.works-navigate.region').find('a').eq(1).text();
        var num = Number(ficTitle.length+ficAuthor.length);
        if (Number(num) > Number(40)){
            var o;
            if (Number(ficAuthor.length)>Number(20)){
                if(Number(ficTitle.length)>Number(20)){ficAuthor=ficAuthor.substr(0,17)+"...";}else{ o = 20-Number(ficTitle.length);o+=19;ficAuthor=ficAuthor.substr(0,o)+"..."; }
            }
            o = 20-Number(ficAuthor.length);
            if (Number(ficTitle.length)>Number(25)){o+=19;ficTitle=ficTitle.substr(0,o)+"...";}
        }
        var style="margin-left:10px; padding: 0px 5px; width: 100px; height: 100px; color: black; background: lightgray; -moz-border-radius: 70px; ";
        style+="-webkit-border-radius: 70px; border-radius: 70px;";
        var remove = " <a href=\"#/\" id=\"remove"+id+"\" class=\"readlink\" style=\""+style+"\" title=\"Remove from \"To Read\" list\">x</a>\n";
        $(newl).html('<a href="/works/' + id + '" class="readlink">'+ficTitle+'</a> - '+ficAuthor+remove);
        $(document).on('click','#remove'+id,function(){removeReadLink(id);return false;});
    });/**/

}
function removeReadLink(id,removeEverything=true){
    //if fic on page, unselect
    if (removeEverything){
        if ($('#work_'+id).exists()){myFunctions.selectIt($('#_'+id).children().eq(7)[0]);}
        else{var x = toReadList.indexOf(id); if(x!=-1){toReadList.splice(x,1);GM_setValue('toread',toReadList);} }
    }
    $('#readlink'+id).remove();
}

function injectClearCbo(){
    var el = $('.narrow-hidden.filters').children().eq(4).after('<div style="margin-left:10px;"><select id="cboClear" style="width:25%;margin-bottom:10px;"></select><UL id="lstClear"></UL></div>');
    $('#cboClear').append('<option selected disabled hidden>Lists</option>');
    $('#cboClear').append('<option value="hide">Hide</option>');
    $('#cboClear').append('<option value="star1">1 Star</option>');
    $('#cboClear').append('<option value="star2">2 Stars</option>');
    $('#cboClear').append('<option value="star3">3 Stars</option>');
    $('#cboClear').append('<option value="star4">4 Stars</option>');
    $('#cboClear').append('<option value="star5">5 Stars</option>');
    $(document).on('change','#cboClear',function(){listSelected();});
}
function listSelected(){
    var ullist = $('#lstClear').empty();
    var opt = $('#cboClear').val();
    var cboList;
    switch(opt){
        case "hide": cboList = hideList; break;
        case "star1": cboList = star1List; break;
        case "star2": cboList = star2List; break;
        case "star3": cboList = star3List; break;
        case "star4": cboList = star4List; break;
        case "star5": cboList = star5List; break;
    }
    var i; for(i=0;i<cboList.length;i++){ $('#lstClear').append('<li id="lst'+cboList[i]+'" style="list-style-type:none;margin:0px;margin-bottom:2px"></li>');}
    loadList();
}
function loadList(){
    $('#lstClear').css({'background-color"': '"yellow"', '"font-size"': '"200%"'});
    var font = $('.toReadLinks').find('ul').eq(0).children().eq(0).css('font-size')+" "+$('.toReadLinks').find('ul').eq(0).children().eq(0).css('font-family');
    $('#lstClear').children().each(function(){
        var id = $(this).attr('id').replace("lst","");
        var child = $(this);
        jQuery.get('/works/' + id + '/navigate', function (page) {
            var ficTitle = $(page).find('.works-navigate.region').find('a').eq(0).text();
            var fID = $(page).find('.works-navigate.region').find('a[href^="/works/"]').attr('href').replace("/works/","");
            var ficAuthor = $(page).find('.works-navigate.region').find('a').eq(1).text();
            var num = Number(ficTitle.length+ficAuthor.length);
            if (Number(num) > Number(40)){
                var o;
                if (Number(ficAuthor.length)>Number(20)){
                    if(Number(ficTitle.length)>Number(20)){ficAuthor=ficAuthor.substr(0,17)+"...";}else{ o = 20-Number(ficTitle.length);o+=19;ficAuthor=ficAuthor.substr(0,o)+"..."; }
                }
                o = 20-Number(ficAuthor.length);
                if (Number(ficTitle.length)>Number(25)){o+=19;ficTitle=ficTitle.substr(0,o)+"...";}
            }
            var style="all:initial; padding:0px 5px; height: 50px ; width: 50px; color: black; background: lightgray; font:"+font+";cursor: pointer;";
            style+="-moz-border-radius: 50px; -webkit-border-radius: 50px; border-radius: 50%;";
            var remove = " <a href=\"#/\" id=\"removelst"+fID+"\" class=\"readlink\" style=\""+style+"\" title=\"Remove from list\">x</a>";//
            $(child).html('<a href="/works/' + fID + '" class="readlink">'+ficTitle+'</a> - '+ficAuthor+remove);
            $(document).on('click','#removelst'+fID,function(){removeListItem(fID);return false;});
        });//$(child).html();
    });
}
function removeListItem(id){
    var opt = $('#cboClear').val();
    var cboList,optID;

    if ($('#work_'+id).exists()){ myFunctions.selectIt($('#_'+id).children().last()[0]);}
    else{
        var x;
        switch(opt){
            case "hide": x = hideList.indexOf(id); if(x!=-1){hideList.splice(x,1);GM_setValue(opt,hideList);} break;
            case "toread": x = toReadList.indexOf(id); if(x!=-1){toReadList.splice(x,1);GM_setValue(opt,toReadList);} break;
            case "star1": x = star1List.indexOf(id); if(x!=-1){star1List.splice(x,1); GM_setValue(opt,star1List);}  break;
            case "star2": x = star2List.indexOf(id); if(x!=-1){star2List.splice(x,1);GM_setValue(opt,star2List);} break;
            case "star3": x = star3List.indexOf(id); if(x!=-1){star3List.splice(x,1);GM_setValue(opt,star3List);} break;
            case "star4": x = star4List.indexOf(id); if(x!=-1){star4List.splice(x,1);GM_setValue(opt,star4List);} break;
            case "star5": x = star5List.indexOf(id); if(x!=-1){star5List.splice(x,1);GM_setValue(opt,star5List);} break;
        }
    }
    $('#lst'+id).remove();
}
function getHTML(id){ alert($('#work_'+id).html()); }
$.fn.exists=function(){return this.length !== 0;};
function summCutOff(id){ var cutoff = ".cutoff"+id; $('.cutoff'+id).hide(); }
// Call save dialog to download settings
myFunctions.download=function (data, filename, type) {
    var file = new Blob([data], {type: type});
    if (window.navigator.msSaveOrOpenBlob) // IE10+
        window.navigator.msSaveOrOpenBlob(file, filename);
    else { // Others
        var a = document.createElement("a"),
                url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function() {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
    }
};
// Debug:Find out what the CSS is set to
myFunctions.accessCSS=function (element){
    var css;
    css = getComputedStyle(element);
    var i,str;
    for(i=0;i<css.length;i++){
        //console.log(css[i]+": "+css.getPropertyValue(css[i])+"\n");
        str+=css[i]+": "+css.getPropertyValue(css[i])+"\n";
    }
alert(element.id+" "+str);
};
myFunctions.addCss();