Qon / FanfictionQomplete

// ==UserScript==
// @name          FanfictionQomplete
// @description   Loads all following chapters on fanfiction.net and strips off junk.
// @namespace     http://Q
// @include       https://www.fanfiction.net/s/*/*
// ==/UserScript==

function urlGetChap(url) {
    var re = /(^.*?fanfiction\.net\/s\/\d+\/)(\d+)(?:\/?.*$)/
    var arr = re.exec(url)
    return arr[2]
}

function urlSetChap(url, n) {
    var re = /(^.*?fanfiction\.net\/s\/\d+\/)(\d+)(?:\/?.*$)/
    var arr = re.exec(url)
    return arr[1] + n
}

function inc(url) {
    var re = /(^.*?fanfiction\.net\/s\/\d+\/)(\d+)(?:\/?.*$)/
    var arr = re.exec(url)
    return arr[1] + (parseInt(arr[2]) + 1)
}

function createChapFromFfpage(url, page) {
    var chapdiv = page.createElement('div')
    var storytext = page.getElementById('storytext')
    if (storytext) {
        chapdiv.setAttribute('class', 'chapter')
        var chapspan = page.createElement('span')
        chapspan.innerHTML = urlGetChap(url) + '. '
        var title = page.getElementsByTagName('title')[0]
        var chaptitle = page.createElement('a')
        chaptitle.setAttribute('href', url)
        chaptitle.innerHTML = title.innerHTML
        chapdiv.appendChild(chapspan)
        chapdiv.appendChild(chaptitle)
        chapdiv.appendChild(document.createElement('hr'))
        chapdiv.appendChild(storytext)
        return chapdiv
    } else return null
}
var body = document.getElementsByTagName('body')[0]
var head = document.getElementsByTagName('head')[0]
var title = document.getElementsByTagName('title')[0]

var profile_top = document.getElementById('profile_top')
var chap = createChapFromFfpage(document.location.href, document)

// else if(typeof ptbuttons[0].remove != "undefined") {
// if(typeof ptbuttons[0].removeNode != "undefined")    {
var ptbuttons = profile_top.getElementsByTagName('button')
if (ptbuttons.length) {
    try {
        ptbuttons[0].remove()
        for (; head.firstElementChild;) head.firstElementChild.remove();
        for (; body.firstElementChild;) body.firstElementChild.remove();
    } catch (e) {
        try {
            ptbuttons[0].removeNode(true)
            var elementChildren = head.children;
            for (; elementChildren.length;) {
                elementChildren[0].removeNode(true)
            }
            var elementChildren = body.children;
            for (; elementChildren.length;) {
                elementChildren[0].removeNode(true)
            }
        } catch (e) {}
    }
}
body.removeAttribute('style')



var style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.innerHTML = 'body{background-color:#3f5;color:#ccc;margin:0;padding:0;font-family:"Verdana";}div.wrap{max-width:1200px;margin-left:auto;margin-right:auto;}.chapter,#profile_top{background-color:#222;margin-top:1em;margin-bottom:1em;}img{float:left;}canvas{float:left;}a:link{color:#a05;}a:visited{color:#555;}a:hover{color:#fff;}a:active{color:#a05;}'
head.appendChild(style)
head.appendChild(title)

wrap = document.createElement('div')
wrap.setAttribute('class', 'wrap')

var div = document.createElement('div')
div.appendChild(profile_top)
wrap.appendChild(div)

wrap.appendChild(chap)
body.appendChild(wrap)

GLOBAL_URL_FOR_IE = ""

function loadQomplete() {
    var style = document.createElement('style')
    style.setAttribute('type', 'text/css')
    style.innerHTML = 'body{background-color:#604;}'
    head.appendChild(style)
    console.log('QOMPLETE')
}

function reqListener() {
    var xmlDoc = new DOMParser().parseFromString(this.responseText, "text/html")
    var url = this.responseURL
    if (url == undefined) url = GLOBAL_URL_FOR_IE
    var chap = createChapFromFfpage(url, xmlDoc)
    if (chap) {
        wrap.appendChild(chap)
        loadNext(url)
    } else loadQomplete()
}

function loadNext(url) {
    var oReq = new XMLHttpRequest();
    oReq.onload = reqListener;
    GLOBAL_URL_FOR_IE = inc(url)
    oReq.open("get", inc(url), true);
    oReq.send();
}

loadNext(document.location.href)