FanfictionQomplete

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

目前為 2015-06-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          FanfictionQomplete
// @description   Loads all following chapters on fanfiction.net and strips off bloat.
// @namespace     https://www.fanfiction.net
// @include       https://www.fanfiction.net/s/*/*
// @version 0.0.1.20150602085450
// ==/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')
            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()
})()