UU看書手機版

幫助在使用手機閱讀UU看書時,能使用滑鼠手勢輕鬆地換頁

目前为 2021-09-15 提交的版本。查看 最新版本

// ==UserScript==
// @name                UU看書手機版
// @version             0.1.2
// @description         幫助在使用手機閱讀UU看書時,能使用滑鼠手勢輕鬆地換頁
// @author              John
// @match               *://*.uukanshu.com/read.aspx*
// @grant               none
// @license             MIT License
// @namespace           https://greasyfork.org/users/814278
// ==/UserScript==

var lastX,lastY,lastSign,afterGestures,gesturesWords,gesturesContent,gestures,signs;
const defaultFun={
    prePage:"prePage()",
    nextPage:"nextPage()"
};

var filterList = ['UU看書 www.uukanshu.com'
                 ,'UU看書www.uukanshu.com'
                  ,'UU看书 www.uukanshu.com'
                  ,'UU看书www.uukanshu.com'
                  ,'請記住本書首發域名:。手機版更新最快網址:'
                  ,'\\(未完待續。\\)'
                  ,'\\(未完待续。\\)'
                  ,'\\(\\)'
                  ,'\\[\\]'
                  ,'天才一秒記住本站地址:最快更新!無廣告!'
                  ,'章節缺失、錯誤舉報'
                  ,'章节缺失、錯誤举报'
                  ,'章节缺失、错误举报'
                  ,'天才壹秒記住,為您提供精彩小說閱讀。'
                  ,'手機用戶請瀏覽m閱讀,更優質的閱讀體驗。'
                  ,'手机用户请浏览m阅读,更优质的阅读体验。'
                 ];

var replaceList = {
                    ",":","
                    , ":":":"
                    , ";":';'
                    , "\\?":"?"
                    , "!":"!"
                    , "隻能":"只能"
                    , "隻要":"只要"
                    , "隻是":"只是"
                    , "隻可":"只可"
                    , "隻允":"只允"
                    , "并且":"並且"
                    };

function prePage(){
    var url = $("#read_pre").attr("href");
    if (url == undefined){
        url = $("#prev").attr("href");
    }
    if (url == undefined){
        return false;
    }
    window.location.href = url;
}

function nextPage(){
    var url = $("#read_next").attr("href");
    if (url == undefined){
        url = $("#next").attr("href");
    }
    if (url == undefined){
        return false;
    }
    window.location.href = url;
}

function initEventListener(start,move,end,tracer,clientX,clientY,startBool){
    var isMouse=start=="mousedown";
    function moveFun(e){
        tracer(eval(clientX),eval(clientY), isMouse);
        gesturesWords.innerHTML=signs;
        var gesturesWidth=signs.length*51+40;
        gesturesContent.style.width=gesturesWidth+"px";
        gesturesContent.style.marginLeft=-gesturesWidth/2+"px";
    };

    document.addEventListener(start, function(e) {
        if(!startBool || eval(startBool)){
            lastX=eval(clientX);
            lastY=eval(clientY);
            lastSign=signs="";
            document.addEventListener(move, moveFun, false);
        }
    }, false);

    function endFun(e) {
        document.removeEventListener(move, moveFun, false);
        setTimeout(function(){if(gesturesContent.parentNode)gesturesContent.parentNode.removeChild(gesturesContent);},500);
        if(signs){
            if(afterGestures)afterGestures();
            for(var g of gestures){
                var gSign=g.gesture;
                if(signs==gSign){
                    if(!isMouse) document.body.appendChild(gesturesContent);
                    var fun=defaultFun[g.fun];
                    if(fun===undefined || !fun){
                        eval(g.fun);
                    }else{
                        eval(fun);
                    }
                    e.stopPropagation();
                    e.preventDefault();
                    break;
                }
            }
            signs="";
        }
    };

    document.addEventListener(end, endFun, false);
    document.addEventListener("mouseup", endFun, false);
}

function fullToHalf(val) {
    var result = "";

    try{
        var value = val || "";
        if (value) {
            for (var i = 0; i <= value.length; i++) {
                if (value.charCodeAt(i) == 12288) {
                    result += " ";
                } else {
                    if (value.charCodeAt(i) > 65280 && value.charCodeAt(i) < 65375) {
                        result += String.fromCharCode(value.charCodeAt(i) - 65248);
                    } else {
                        result += String.fromCharCode(value.charCodeAt(i));
                    }
                }
            }
        }
    }catch(e){
        console.error(e);
    }

  return result;
}

function alterContent(){
    try{
        var obj = document.getElementById("bookContent");
        var val = fullToHalf(obj.innerText);
        for(const str of filterList){
            val = val.replace(new RegExp(str,"gi"),'');
        }

        setTimeout(function() {
            for (var key in replaceList) {
                val = val.replace(new RegExp(key, "gi"), replaceList[key]);
            }
            obj.innerText = val;
        },5);
    }catch(e){
        console.error(e);
    }
}

function addNexPagetLink(){
    try{
        var newEle = document.createElement("div");
        var oldEle = document.querySelector(".rp-switch");
        var nextLinkEle = document.getElementById("read_next")

        var leftEle = nextLinkEle.cloneNode(true);
        var rightEle = nextLinkEle.cloneNode(true);
        leftEle.style.cssText = 'left:0px;position:absolute;';
        rightEle.style.cssText = 'right:0px;position:absolute;';
        newEle.appendChild(leftEle);
        newEle.appendChild(rightEle);
        oldEle.parentNode.insertBefore(newEle ,oldEle);
    }catch(e){
        console.error(e);
    }
}


function alterEleLoction(){
    var toolbtm1Ele = document.getElementById("tool_btm1");
    var toolbtm2Ele = document.getElementById("tool_btm2");
    var fontBtbEle = document.getElementById("funfont");
    var nextBtnEle = document.getElementById("funnext");
    fontBtbEle.style = 'width:50px;position: absolute;right:10px;left:auto;padding-top: 12px;';
    nextBtnEle.style = 'width:50px;position: absolute;left:10px;';

    toolbtm1Ele.insertBefore(nextBtnEle, fontBtbEle);
    toolbtm1Ele.removeChild(fontBtbEle);
    toolbtm2Ele.appendChild(fontBtbEle);
}

(function() {
    'use strict';
    const minLength=256;
    const tg=0.5;
    gestures=[
          {gesture:"→", fun:"prePage"},
          {gesture:"←", fun:"nextPage"},
         ];

    function tracer(curX,curY,showSign) {
        let distanceX=curX-lastX,distanceY=curY-lastY;
        let distance=(distanceX*distanceX)+(distanceY*distanceY);
        if (distance>minLength) {
            lastX=curX;
            lastY=curY;
            let direction="";
            let slope=Math.abs(distanceY/distanceX);
            if(slope>tg){
                if(distanceY>0) {
                    direction="↓";
                }else{
                    direction="↑";
                }
            }else if(slope<=1/tg) {
                if(distanceX>0) {
                    direction="→";
                }else{
                    direction="←";
                }
            }
            if(lastSign!=direction) {
                signs+=direction;
                lastSign=direction;
                if(showSign){
                    document.body.appendChild(gesturesContent);
                }
            }
        }
    }

    gesturesContent=document.createElement("div");
    gesturesContent.id="gesturesContent";
    gesturesContent.style.cssText="width:300px;height:70px;position:fixed;left:50%;top:50%;margin-top:-25px;margin-left:-150px;z-index:999999999;background-color:#000;border:1px solid;border-radius:10px;opacity:0.65;filter:alpha(opacity=65);box-shadow:5px 5px 20px 0px #000;";
    gesturesContent.innerHTML='<div id="gesturesWords" style="position:absolute;left:20px;top:5px;font:bold 50px \'黑体\';color:#ffffff"></div>';
    gesturesWords = gesturesContent.querySelector("#gesturesWords");

    initEventListener("touchstart","touchmove","touchend",tracer,"e.changedTouches[0].clientX","e.changedTouches[0].clientY");
    initEventListener("mousedown","mousemove","contextmenu",tracer,"e.clientX","e.clientY","e.which === 3");

    alterContent();
    // alterEleLoction();
    addNexPagetLink();

    try{
        document.querySelector('.samebooks1').style.display = 'none';
        document.querySelector('.socialBtn').style.display = 'none';
    }catch(e){
        console.error(e);
    }
})();