Greasy Fork

Theme tweaker LN

This script provides some useful tools for ln translation website like a FullScreen Reader and some design style change for better readability. I provide the software as is, you can fully modify it to your likings :)

目前为 2017-08-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         Theme tweaker LN
// @namespace    https://openuserjs.org/scripts/akuma06/Theme_tweaker_LN
// @version      0.5.11
// @description  This script provides some useful tools for ln translation website like a FullScreen Reader and some design style change for better readability. I provide the software as is, you can fully modify it to your likings :) 
// @copyright    2016, akuma06
// @author       akuma06
// @match        http://krytykal.org/*
// @match        http://avertranslation.org/*
// @match        https://firebirdsnest.org/*
// @match        https://birdytranslations.com/*
// @match        http://tseirptranslations.com/*
// @match        http://infinitenoveltranslations.net/*
// @match        https://pumlated.wordpress.com/*
// @match        https://pizzasandcoke.wordpress.com/*
// @match        https://grimgarthetranslation.wordpress.com/*
// @match        https://oniichanyamete.wordpress.com/*
// @match        https://psicern.wordpress.com/*
// @match        https://knightsoflunadia.wordpress.com/*
// @match        http://www.yamitranslations.com/*
// @match        http://www.wuxiaworld.com/*
// @match        https://zirusmusings.com/*
// @match        http*://moonbunnycafe.com/*
// @match        https://isekailunatic.wordpress.com/*
// @match        https://addnewtab.wordpress.com/*
// @match        http://yukkuri-literature-service.blogspot.com/*
// @require      http://code.jquery.com/jquery-latest.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==
/* jshint asi:true */
var $ = window.jQuery
var regexp = {
    "krytykal\.org/.*": "krytykal_org",
    "avertranslation\.org/.*": "avertranslation_org",
    "firebirdsnest\.org/.*":"firebirdsnest_org",
    "tseirptranslations\.com/.*":"tseirptranslations_com",
    "birdytranslations\.com/.*":"birdytranslations_com",
    "infinitenoveltranslations\.net/.*":"infinitenoveltranslations_net",
    "moonbunnycafe\.com/.*":"moonbunnycafe_com",
    "zirusmusings\.com/.*": "zirusmusings_com",
    "www\.yamitranslations\.com/.*": "www_yamitranslations_com",
    "www\.wuxiaworld\.com/.*": "www_wuxiaworld_com",
    "pumlated\.wordpress\.com/.*": "pumlated_wordpress_com",
    "addnewtab\.wordpress\.com/.*": "addnewtab_wordpress_com",
    "pizzasandcoke\.wordpress\.com/.*": "pizzasandcoke_wordpress_com",
    "grimgarthetranslation\.wordpress\.com/.*": "grimgarthetranslation_wordpress_com",
    "oniichanyamete\.wordpress\.com/.*": "oniichanyamete_wordpress_com",
    "psicern\.wordpress\.com/.*": "psicern_wordpress_com",
    "knightsoflunadia\.wordpress\.com/.*": "knightsoflunadia_wordpress_com",
    "isekailunatic\.wordpress\.com/.*": "isekailunatic_wordpress_com",
    "yukkuri-literature-service\.blogspot\.com/.*": "yukkuri_literature_service_blogspot_com"
}

function Infobox(params) {
    this.clicked = []
    var callback = params.callback
    var appendTo = (params.append !== undefined) ? params.append : $("body")
    var buttons = (params.buttons !== undefined) ? params.buttons : { Ok: () => this.close() }
    var parentDiv = $("<div></div>").css({ position: "absolute", zIndex:10000, background:"none", display:"flex", textAlign:"center", margin:0, padding:0, top:"0", left:"0", right:"0", bottom:"0" })
    var infoDiv = $("<div></div>").css({ background:"rgba(171, 171, 171, 0.88)", display: "inline-block", color: "white", borderRadius: "1em", boxShadow:"0 0 19px 2px black", padding: "2em", margin:"auto" })
    this.close = function () {
        if (callback !== undefined) {
            callback(this)
        }
        $(parentDiv).remove()
    }
    var self = this
    $(infoDiv).html($("<p></p>").html(params.text))
    var divButtons = $("<div></div>").css({textAlign:"center"})
    for (var btn in buttons) {
        (function(name, button) { 
            if (button instanceof Function)
                $(divButtons).append($("<button></button>").css({ marginRight: "3px" }).text(name).click((e) => {
                    e.preventDefault()
                    self.clicked.push(name)
                    button()
                    self.close()
                }))
                })(btn, buttons[btn])
        }
    infoDiv.append(divButtons)
    parentDiv.html(infoDiv)
    appendTo.append(parentDiv)
    return parentDiv
}

function Tweaker(params) {
    this.getActive = () => {
        return $("div[data-reader='lnreader']")
    }
    this.links = {
        next: "",
        previous: ""
    }
    this.active = undefined
    this.mapF = (arr, text) => { return arr.map(function(ind, obj) { return ($(obj).text().match(text)) ? obj : undefined })[0] }
    this.mapFExact = (arr, text) => { return arr.map(function(ind, obj) { return ($(obj).text() == text) ? obj : undefined })[0] }
    var textTitle = (params !== undefined && params.title !== undefined) ? params.title : () => { return $(".entry-title")}
    var icon, iconClose
    if (params !== undefined) {
        if (params.icon != "noticon") {
            icon = (params.icon !== undefined && params.icon.open !== undefined) ? params.icon.open : undefined
            iconClose = (params.icon !== undefined && params.icon.close !== undefined) ? params.icon.close : undefined
        } else {
            icon = "noticon noticon-external"
            iconClose = "noticon noticon-close"
        }
    }
    var contentDiv = (params !== undefined && params.content !== undefined && params.content.find !== undefined) ? params.content : (self, link) => { return $(link).parent().parent().siblings(".entry-content")}
    var filter = (params !== undefined && params.content !== undefined && params.content.filter !== undefined) ? ((params.content.filter !== "") ? params.content.filter : (self, content) => { return content }) : (self, content) => { return $(content).find("> *:not(.sharedaddy)")}
    var nextLink = (params !== undefined && params.next !== undefined && params.next.find !== undefined) ? params.next.find : (self, link, text) => { return self.mapF($(contentDiv(self, link)).find("a"), text)}
    var prevLink = (params !== undefined && params.prev !== undefined && params.prev.find !== undefined) ? params.prev.find : (self, link, text) => { return self.mapF($(contentDiv(self, link)).find("a"), text)}
    var nextText = (params !== undefined && params.next !== undefined && params.next.text !== undefined) ? params.next.text : "Next"
    var prevText = (params !== undefined && params.prev !== undefined && params.prev.text !== undefined) ? params.prev.text : "Previous"
    var styles = (params !== undefined) ? params.styles : undefined
    this.navigation = (self, context) => {
        if (params !== undefined && params.navigation !== undefined) params.navigation(self, context)
        else this.navigationCallback(self, context)
            }
    var self = this
    this.Render = () => {
        if (GM_getValue("firstlaunch") !== true) {
            new Infobox({
                text: "Welcome to LN Reader!<br>You can read LN in fullscreen mode by clicking on the link next to the Chapter title.<br>Furthermore, you move to the next/previous page with your left/right arrow keys.<br><em>Have fun reading!</em>",
                callback: () => GM_setValue("firstlaunch", true)
            })
        }
        var tagIcon = (icon !== undefined) ? $("<i></i>").attr("class", icon).css("font-size", "1.5em") : "&plusmn;Reader"
        var linkOpen = $('<a href="" style="float:right;"></a>').html(tagIcon).click(function(e) {
            e.preventDefault()
            sessionStorage.setItem("readerMode", true)
            self.Popup($(this))
        })
        // console.log("Applying popup link...")
        $(textTitle(self)).append(linkOpen)
        if (styles !== undefined) {
            // console.log("Applying styles...")
            styles(textTitle(self), contentDiv(self, linkOpen))
        }
        this.navigation(self)
        var readerMode = sessionStorage.getItem("readerMode")
        if (readerMode=="true") this.Popup($($(textTitle(self)).find("a:last")[0]))
        // console.log("Rendering done!")
    }
    this.Popup = (context) => {
        // console.log("Opening popup...")
        var container = $("<div></div>").attr("tabindex", 0).attr("data-reader", "lnreader").css({ zIndex: "9999",
                                                                                                  background:"#EEE",
                                                                                                  width:"100%",
                                                                                                  height:"100%",
                                                                                                  position:"fixed",
                                                                                                  top:"0", left:"0",
                                                                                                  overflow:"auto",
                                                                                                  paddingTop:"20px",
                                                                                                  fontSize:"1.2em" })
        var contentBlock = $("<div></div>").attr("class", "post-content").css({ width:"60%", 
                                                                               minWidth:"700px", 
                                                                               maxWidth:"1200px",
                                                                               height:"auto",
                                                                               margin:"auto",
                                                                               padding:"2.5em",
                                                                               background:"white",
                                                                               color:"black",
                                                                               textAlign:"justify" })
        var closeIcon = (iconClose !== undefined) ? $("<i></i>").attr("class", iconClose).css("font-size", "2em") : "&times;"
        var linkClose = $("<a href=''></a>").html(closeIcon).css({position:"fixed", border: "none", left:"82%", top:"40px", fontSize:"3.5em"}).click(function(e){
            e.preventDefault()
            $(this).parent().parent().remove()
            $("body").css("overflow","")
            $("html").css("overflow","")
            sessionStorage.setItem("readerMode", false)
            self.active = undefined
        })

        $("body").append(container.html(
            contentBlock.html($(filter(self, contentDiv(self, context))).clone())
            .prepend(linkClose)
        )
                        ).css("overflow","hidden")
        $(container).focus().on("click", function(e) {
            $(this)[0].webkitRequestFullscreen()
        })
        this.active = $(container)[0]

        if (localStorage.getItem(window.location.href) !== null) {
            new Infobox({
                text:"We have saved where you left when reading this chapter, do you want to continue where you stopped?",
                buttons: {
                    Yes: () => {
                        var active = $("div[data-reader='lnreader']")[0]
                        active.scroll(0, localStorage.getItem(window.location.href))
                    },
                    No: () => localStorage.removeItem(window.location.href)
                },
                append: $(this.active)
            })
        }
        $(container)[0].webkitRequestFullscreen()
        $("html").css("overflow", "hidden")
        // console.log("Looking for next/prev links...")
        this.navigation(self, context)
        // console.log("Popup opened!")
    }
    this.navigationCallback = (self, context) => {
        if (context === undefined) {
            return
        }
        var nextHref = $(nextLink(self, context, nextText)).attr("href")
        if (nextHref) {
            self.links.next = nextHref
            // console.log("Next link found")
        }
        var previousHref = $(prevLink(self, context, prevText)).attr("href")
        if (previousHref) {
            self.links.previous = previousHref
            // console.log("Previous link found")
        }
        this.attachEvents(self)
    }

    this.attachEvents = (self) => {
        $(self.active).keyup(function (e) {
            if ((e.which == 39)&&(self.links.next !== "")) {
                e.preventDefault()
                self.next(self)
                // console.log("Moving to Next Page")
            } else if ((e.which == 37)&&(self.links.previous !== "")) {
                e.preventDefault()
                self.previous(self)
                // console.log("Moving to Previous Page")
            }
        })
        if (self.active !== undefined) {
            $(self.active).keyup(function (e) {
                if ((e.which == 40) && (self.links.next !== "")) {
                    self.hideup(e, true)
                } else if ((e.which == 38) && (self.links.previous !== "")) {
                    self.hidedown(e, true)
                }
            }).keydown(function (e) {
                if ((e.which == 40) && (self.links.next !== "")) {
                    self.hideup(e)
                } else if ((e.which == 38) && (self.links.previous !== "")) {
                    self.hidedown(e)
                }
            }).scroll((e) => {
                if (self.active.scrollHeight <= (self.active.scrollTop+this.active.offsetHeight) || self.active.scrollTop === 0) {
                    localStorage.removeItem(window.location.href)
                    return
                }
                localStorage.setItem(window.location.href, self.active.scrollTop)
            })
        }
    }
    this.next = (self) => {
        window.location.href = self.links.next
    }
    this.previous = (self) => {
        window.location.href = self.links.previous
    }
    this.hideup = function(e, reset) {
        if (this.active !== undefined) {
            var div = $(this.active).find("div")[0]
            if (reset) {
                div.style.position = ""
                div.style.top = 0
                return
            }
            if (this.active.scrollHeight <= (this.active.scrollTop+this.active.offsetHeight)) {
                if (e !== undefined) e.preventDefault()
                div.style.position = "relative"
                var pos = (div.style.top.replace("px", "") !== "") ? parseFloat(div.style.top.replace("px", "")) : -1
                pos = (pos >= 0) ? -1 : pos
                if (-pos > this.active.offsetHeight) {
                    this.next(this)
                    div.style.background = "none"
                    div.innerText = "Loading..."
                } else {
                    pos-= -0.2*pos
                    div.style.top = pos+"px"
                }
            }
        }
    }
    this.hidedown = function(e, reset) {
        if (this.active !== undefined) {
            var div = $(this.active).find("div")[0]
            if (reset) {
                div.style.position = ""
                div.style.top = 0
                return
            }
            if (this.active.scrollTop === 0) {
                if (e !== undefined) e.preventDefault()
                div.style.position = "relative"
                var pos = (div.style.top.replace("px", "") !== "") ? parseFloat(div.style.top.replace("px", "")) : 1
                pos = (pos <= 0) ? 1 : pos
                if (pos > this.active.offsetHeight) {
                    this.previous(this)
                    div.style.background = "none"
                    div.innerText = "Loading..."
                } else {
                    pos += 0.2*pos
                    div.style.top = pos+"px"
                }
            }
        }
    }
}

var tweakes = {
    krytykal_org :{ styles: () => {
        $(".main-navigation .nav-menu ul li > a").each(function () {
            this.style.setProperty("color", "black", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "black", "important")
            })
        })
        $(".main-navigation ul.children").css("background", "#2f3c4e")
        $(".main-navigation ul.children").css("border-left", "1px solid rgba(255,255,255,0.2)")
        $(".main-navigation ul.children li").css("border", "0")


        $(".main-navigation ul.children li a").each(function () {
            this.style.setProperty("color", "white", "important")
            this.style.setProperty("background", "none", "important")
            this.style.setProperty("font-family", "'Open Sans', 'Droid Arabic Kufi', Arial, sans-serif", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "white", "important")
            })
        })
        $("#page").css("color", "black")
        $("#page").css("background", "white")
    }
                  },
    avertranslation_org: { styles: () => {
        $("#menu-menu-1 li > a").each(function () {
            this.style.setProperty("color", "black", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "black", "important")
            })
        })
        $("#menu-menu-1 ul.sub-menu").css("background", "#2f3c4e")
        $("#menu-menu-1 ul.sub-menu").css("border-left", "1px solid rgba(255,255,255,0.2)")
        $("#menu-menu-1 ul.sub-menu li").css("border", "0")


        $("#menu-menu-1 ul.sub-menu li a").each(function () {
            this.style.setProperty("color", "white", "important")
            this.style.setProperty("background", "none", "important")
            this.style.setProperty("font-family", "'Open Sans', 'Droid Arabic Kufi', Arial, sans-serif", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "white", "important")
            })
        })
        $("#page").css("color", "black")
        $("#page").css("background", "white")
        $("#access").css("background", "white")

    } },
    firebirdsnest_org: { styles: () => {
        $(".hentry").css("color", "black")
        $(".hentry").css("background", "white")
        $("#menu li > a").each(function () {
            this.style.setProperty("color", "black", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "black", "important")
            })
        })
        $("#menu ul.sub-menu").css("background", "#2f3c4e")
        $("#menu ul.sub-menu").css("border-left", "1px solid rgba(255,255,255,0.2)")
        $("#menu ul.sub-menu li").css("border", "0")


        $("#menu ul.sub-menu li a").each(function () {
            this.style.setProperty("color", "white", "important")
            this.style.setProperty("background", "none", "important")
            this.style.setProperty("font-family", "'Open Sans', 'Droid Arabic Kufi', Arial, sans-serif", "important")
            $(this).hover(function() {
                this.style.setProperty("color", "#e74c3c", "important")
            }, function () {
                this.style.setProperty("color", "white", "important")
            })
        })
        $("#menu").css({ background: "white", float:"none", margin: "auto"}).append($("<div></div>").css("clear", "both"))
        $("#container").css("width", "100%")
        $("#main").css("width", "70%")
        $("#page-wrap").css({width: "90%", minWidth: "1000px"})
        $("#sidebar").css({width: "26%"})
        $(".widget").css({padding: 0, width:"100%"})
        $(".widget > *").css({marginLeft: "15px", marginBottom: "15px" })
        $(".widget-title").css({ position: "static", width:"100%", marginLeft:0,marginBottom:0})
    },
                        content: { find: () => { return $("#main .entry-content") }, filter: "" },
                        icon: { 
                            close: "noticon noticon-close",
                            open: "noticon noticon-external"
                        },
                        title: () => { return $("#main .entry-title") },
                       },
    tseirptranslations_com:{
        title: () =>{ return $(".content .post-title") },
        content: { find: (self, link) => { return $(link).parent().parent().siblings(".post-content") }, filter: (self, content) => { return $(content).find(">*:not(.sd-block)") } },
        icon: { open: "genericon genericon-external", close: "genericon genericon-close" }
    },
    pumlated_wordpress_com:{
        title: () =>{ return $("#content .entry-title") },
        icon: "noticon",
        prev: { link: () => { return $("a[rel='prev']") }},
        next: { link: () => { return $("a[rel='next']") }}
    },
    yukkuri_literature_service_blogspot_com: {
        title: () =>{ return $("#Blog1 .entry-title") },
    },
    pizzasandcoke_wordpress_com:{
        title: () =>{ return $("#main .entry-title") },
        icon: "noticon",
    },
    grimgarthetranslation_wordpress_com:{
        title: () =>{ return $(".page .page-title") },
        content: { find: (self, link) => { return $(link).parent().parent().siblings(".page-body")}, filter: (self, content) => { return $(content).find("> *:not(.sharedaddy)>*") } },
        icon: "noticon",
    },
    isekailunatic_wordpress_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: "noticon",
    },
    oniichanyamete_wordpress_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: "noticon",
    },
    infinitenoveltranslations_net:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: { open: "genericon genericon-external", close: "genericon genericon-close" },
        styles: () => {
            $("#xyz").prepend($("#xyz div").remove().html())
        }
    },
    birdytranslations_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        content: { find: (self, link) => { return $(link).parent().parent().siblings(".entry-wrapper") }, filter: (self, content) => { return $(content).find(".entry-content>*:not(.sharedaddy)") } },
        icon: { open: "genericon genericon-external", close: "genericon genericon-close" },
        next: { text: "[Next>"},
        prev: { text: "<Last]"}
    },
    moonbunnycafe_com:{
        title: () =>{ return $(".hentry .entry-title") },
        content: { find: (self, link) => { return $(link).parent().siblings(".entry-content") } },
        icon: { open: "dashicons dashicons-external", close: "dashicons dashicons-close" },
    },
    psicern_wordpress_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: "noticon",
        next: { text: "→"},
        prev: { text: "←"}
    },
    www_wuxiaworld_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: { open: "fa fa-external-link", close: "fa fa-close" },
        styles: (title, content) => {
            if (window.location.href.match("wmw-index")) {
                var parseChildren = (child) => {
                    if ($(child).children().length > 0) {
                        $(child).children().each(function() { parseChildren($(this)) })
                    }
                    $(child).html($(child).html().replace(/!/g, "."))
                }
                parseChildren($(content))
            }
        }
    },
    knightsoflunadia_wordpress_com:{
        title: () =>{ return $(".entry-header .entry-title") },
        icon: "noticon",
        next: { link: () => { return $(".nav-next") }},
        prev: { link: () => { return $(".nav-previous") }}
    },
    www_yamitranslations_com:{
        title: () =>{ return $(".hentry .entry-title") },
        content: { find: (self, link) => { return $(link).parent().siblings(".entry-content")}, filter: "" },
        next: { link: (self, link, text) => { return self.mapF($(".orPrevNext"), text) }},
        prev: { link: (self, link, text) => { return self.mapF($(".orPrevNext"), text) }},
        navigation: (self) => { 
            if (document.querySelectorAll(".orPrevNext a").length === 0) {
                // console.log("retry to find prev link")
                setTimeout(() => { self.navigation(self) }, 300)
                return
            }
            // console.log("found links!")
            self.navigationCallback(self, "")
        }
    },
    zirusmusings_com:{
        title: () =>{ return $(".page-title") },
        content: { find: (self, link) => { return $(link).parent().parent().parent().parent().find(".entry-content") } },
        icon: "noticon",
    },
    addnewtab_wordpress_com:{
        title: () =>{ return $("#content .entry-title") },
        icon: "noticon",
    }
}

$.each(regexp, function (key, val) {
    if (window.location.href.match(key)) {
        if (tweakes[val] !== undefined)
            new Tweaker(tweakes[val]).Render()
            else console.error("The element in tweakes map is not defined")
                }
})