Greasy Fork

FanfictionQomplete

Loads all following chapters on fanfiction.net and strips off bloat.

目前为 2015-06-02 提交的版本。查看 最新版本

// ==UserScript==
// @name          FanfictionQomplete
// @description   Loads all following chapters on fanfiction.net and strips off bloat.
// @namespace     https://greasyfork.org/en/users/11891-qon
// @author        Qon
// @include       https://www.fanfiction.net/s/*/*
// @noframes
// @grant         none
// @license       Simple Public License 2.0 (SimPL) https://tldrlegal.com/license/simple-public-license-2.0-%28simpl%29
// @version 0.0.1.20150602140134
// ==/UserScript==

(function() {
    if (Element.prototype.remove == undefined) {
        Element.prototype.remove = function() {
            this.parentElement.removeChild(this)
        }
    }

    function injectQompleteButton() {
        var lc = document.getElementsByClassName('lc')
        if (lc.length) {
            lc = lc[0]
            var btn = document.createElement('button')
            btn.setAttribute('onclick', 'document.runFFQomplete();')
            btn.setAttribute('class', 'btn') // class `btn` is a ff.net class giving the button the look of other buttons on the page.
            btn.setAttribute('style', 'margin-left:12px;margin-right:2px;')
            btn.setAttribute('title', 'Append all following chapters and remove unecessary bloat.')
            btn.innerHTML = 'Qomplete!'
            lc.appendChild(btn)
        }
    }
    injectQompleteButton()

    document.runFFQomplete = function() {
        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)

        var ptbuttons = profile_top.getElementsByTagName('button')
        if (ptbuttons.length) {
            ptbuttons[0].remove()
            for (; head.firstElementChild;) head.firstElementChild.remove();
            for (; body.firstElementChild;) body.firstElementChild.remove();
        }
        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)

        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 appendChapterFromURL(url) {
            var oReq = new XMLHttpRequest();
            oReq.onload = function() {
                var xmlDoc = new DOMParser().parseFromString(this.responseText, "text/html")
                var url = this.responseURL ? this.responseURL : this.responseURLfallback
                if (!url) {
                    console.log('Error: Response has no responseURL field!')
                }
                var chap = createChapFromFfpage(url, xmlDoc)
                if (chap) {
                    wrap.appendChild(chap)
                    appendChapterFromURL(inc(url))
                } else loadQomplete()
            }
            oReq.responseURLfallback = url
            oReq.open("get", url, true);
            oReq.send();
        }

        appendChapterFromURL(inc(document.location.href))
    }
    // document.runFFQomplete()
})()