老司机传说开发版

准备撸一个新的老司机传说,现在基本什么功能都没有,后面慢慢加,尽量不弃坑,欢迎提供建议。

As of 2020-07-24. See the latest version.

// ==UserScript==
// @name        老司机传说开发版
// @namespace   http://www.ocrosoft.com/
// @version     0.102
// @description 准备撸一个新的老司机传说,现在基本什么功能都没有,后面慢慢加,尽量不弃坑,欢迎提供建议。
// @author      ocrosoft
// @match       *://blog.reimu.net/*
// @match       *://pan.baidu.com/share/init?*
// @match       *://yun.baidu.com/share/init?*
// @match       *://www.liuli.se/*
// @match       *://www.liuli.pl/*
// @icon        https://www.liuli.pl/favicon.ico
// @grant       unsafeWindow
// ==/UserScript==

let prefix = '[老司机传说] ';
function INFO(msg) {
    console.info(prefix + msg);
}
function WARN(msg) {
    console.warn(prefix + msg);
}
function ERROR(msg) {
    console.error(prefix + msg);
}
function ELEM(element) {
    console.log(element);
}

// jQuery
let $ = jQuery;
let jquery_cdns = [
    'http://code.jquery.com/jquery-2.1.4.min.js',
    'http://libs.baidu.com/jquery/2.1.4/jquery.min.js',
    'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js'
];
function CheckJQuery() {
    function InsertJQuery(url) {
        let script = document.createElement('script');
        script.src = url;
        document.head.appendChild(script);
        return script;
    }
    function ConverProtocolIfNeeded(url) {
        let is_https = location.href.indexOf('https://') != -1;
        let url_is_https = url.indexOf('https://') != -1;

        if (is_https && !url_is_https) {
            return url.replace('http://', 'https://');
        } else if (!is_https && url_is_https) {
            return url.replace('https://', 'http://');
        }
    }
    function WaitAndCheckJQuery(cdn_index, resolve) {
        if (cdn_index >= jquery_cdns.length) {
            ERROR('无法加载 JQuery,正在退出。');
            resolve(false);
            return;
        }
        let url = ConverProtocolIfNeeded(jquery_cdns[cdn_index]);
        INFO('尝试第 ' + (cdn_index + 1) + ' 个 JQuery CDN:' + url + '。');
        let script = InsertJQuery(url);
        setTimeout(function() {
            if ($ == null) {
                WARN('无法访问。');
                script.remove();
                WaitAndCheckJQuery(cdn_index + 1, resolve);
            } else {
                INFO('已加载 JQuery。');
                resolve(true);
            }
        }, 100);
    }
    return new Promise(function(resolve) {
        if ($ == null) {
            INFO('未发现 JQuery,尝试加载。');
            WaitAndCheckJQuery(0, resolve);
        } else {
            INFO('已加载 JQuery。');
            resolve(true);
        }
    });
}

// 窗口大小发生改变的调用方法
let resize_functions = {};

let sites = [{
    name: '琉璃神社',
    pattern: 'https:\/\/www\.liuli\.(se|pl)\/wp\/\\d+\.html',
    process: function() {
        // 处理磁力链
        function ProcessLink() {
            // 增加一个跳转下载链接的气泡
            function AddBubble(hash_match) {
                // 元素
                let comment_bubble_wrapper = $('#wpd-bubble-wrapper');
                let link_bubble_wrapper = comment_bubble_wrapper.clone();
                link_bubble_wrapper.find('#wpd-bubble-all-comments-count').text(hash_match.length);
                let link_bubble = link_bubble_wrapper.find('#wpd-bubble-count');
                let svg = $('<svg height="512pt" viewBox="0 -61 512 512" width="512pt" xmlns="http://www.w3.org/2000/svg"><path d="m377 0h-121l-30 37.5 30 37.5h121c33.085938 0 60 26.914062 60 60s-26.914062 60-60 60h-121l-30 37.5 30 37.5h121c74.4375 0 135-60.5625 135-135s-60.5625-135-135-135zm0 0" fill="#18e0ff"></path><path d="m256 120-30 37.5 30 37.5c33.085938 0 60 26.914062 60 60s-26.914062 60-60 60l-30 37.5 30 37.5c74.4375 0 135-60.5625 135-135s-60.5625-135-135-135zm0 0" fill="#13bdf7"></path><path d="m135 195h121v-75h-121c-74.4375 0-135 60.5625-135 135s60.5625 135 135 135h121v-75h-121c-33.085938 0-60-26.914062-60-60s26.914062-60 60-60zm0 0" fill="#18e0ff"></path><path d="m256 75v-75c-74.4375 0-135 60.5625-135 135s60.5625 135 135 135v-75c-33.085938 0-60-26.914062-60-60s26.914062-60 60-60zm0 0" fill="#13bdf7"></path></svg>');
                link_bubble.children('svg').remove();
                link_bubble.append(svg);
                link_bubble_wrapper.css({
                    'left': comment_bubble_wrapper.css('left'),
                    'bottom': parseInt(comment_bubble_wrapper.css('bottom')) + 100 + 'px',
                });
                resize_functions.liuli_resize_bubble_position = function() {
                    link_bubble_wrapper.css('left', comment_bubble_wrapper.css('left'));
                };
                // 链接
                let bubble_message = link_bubble_wrapper.find('#wpd-bubble-add-message');
                bubble_message.empty().css({'display': 'block', 'text-align': 'left', 'width': 'max-content', 'height': 'auto'});
                $.each(hash_match, function(index, hash) {
                    if (index > 0) {
                        bubble_message.append('<br>');
                    }
                    let link_jump = $('<a style="color:#fff;">定位到链接</a>');
                    let link_download = $('<a href="magnet:?xt=urn:btih:' + hash + '" style="color:#fff; margin-left: 10px;">' + hash.toUpperCase() + '</a>');
                    link_jump.click(function() {
                        $('body, html').animate({scrollTop: $('#od_link_' + hash).offset().top}, 500);
                    });
                    let span = $('<span></span>');
                    span.append(link_jump).append(link_download);
                    bubble_message.append(span);
                });
                // 事件
                link_bubble_wrapper.find('#wpd-bubble-count').click(function(event) {
                    let wrapper = $(this).parent();
                    wrapper.addClass('wpd-bubble-hover');
                });
                link_bubble_wrapper.find('#wpd-bubble').find('svg').css('-webkit-transform', 'rotate(45deg)');
                // 关闭只点 SVG,点弹出的链接就不会关闭了
                link_bubble_wrapper.find('#wpd-bubble').find('svg').click(function(event) {
                    let wrapper = $(this).parent().parent();
                    wrapper.removeClass('wpd-bubble-hover');
                });
                comment_bubble_wrapper.after(link_bubble_wrapper);
                // 找到了链接,主动打开一下
                if (hash_match.length > 0) {
                     link_bubble_wrapper.find('#wpd-bubble-count').click();
                }
            }

            let e_content = $('.entry-content');
            let content = e_content.text();
            let hash_match = content.match(/[a-zA-Z0-9]{40}/g);
            ELEM(hash_match);

            // HASH 替换成老司机链接
            $.each(hash_match, function(index, hash) {
                let replace_text = '<br><a id="od_link_' + hash + '" href="magnet:?xt=urn:btih:' + hash + '">老司机链接</a>(' + hash + ')<br>';
                e_content.get(0).innerHTML = e_content.get(0).innerHTML.replace(hash, replace_text);
            });

            AddBubble(hash_match);
        }

        ProcessLink();
    },
}, {
    name: '灵梦御所',
    pattern: 'https?:\/\/blog.reimu.net\/*',
    process: function() {
        //
    },
}];

CheckJQuery().then(function(success) {
    if (!success) {
        return;
    }

    let url = location.href;
    $.each(sites, function(index, site) {
        if (!url.match(site.pattern)) {
            return;
        }
        INFO(site.name);
        site.process();

        return false;
    });

    $(window).resize(function() {
        $.each(resize_functions, function(name, func) {
            func();
        });
    });
});

//执行函数
/*function a(){
    'use strict';
    //灵梦御所
    if(host=="blog.reimu.net"){
        var titleTime;
        if(boxON[1])addBox();
        //开启老司机模式
        if(ifshow()){
            var pre = document.getElementsByTagName('pre');
            for(var rmi=0;rmi<pre.length;rmi++){
                if (pre[rmi]) pre[rmi].style.display = 'block';
                var textrei=pre[rmi].innerHTML;
                if(textrei){
                    var cilirei=textrei.match(/magnet:?[^\n"]+/);
                    pre[rmi].innerHTML=textrei.replace(cilirei,'<a href="'+cilirei+'">'+cilirei+'</a>');
                }
            }
        }
        //添加直接跳转到下载链接的按钮
        var oD_box=document.createElement("div");
        oD_box.style.zindex="999999";
        oD_box.id="goToDownload";
        oD_box.style="position:fixed;top:60px;right:0px;width:40px;transition: all 0.2s ease;white-space:nowrap;z-index:99998";
        var oD_button=document.createElement("button");
        oD_button.id="goToDownload_button";
        oD_button.type="button";
        oD_button.innerHTML="前往<br/>下载";
        oD_button.style="padding: 4px 0px; position: absolute; top: -1px; right: 0px; width: 40px; height: auto;";
        oD_button.onclick=function (){
            if(document.getElementsByTagName('pre').length>1)alert("下载链接可能不止一个,将跳转到第一个!");
            document.getElementsByTagName('pre')[0].scrollIntoView();
        };
        oD_box.appendChild(oD_button);
        document.body.appendChild(oD_box);
    }
    //调整跳转链接
    else if(host.indexOf('hacg')!=-1||host.indexOf('llss')!=-1||host.indexOf('liuli')!=-1||host.indexOf('tianshit')!=-1){//琉璃神社\天使二次元
        if((host.indexOf('hacg')!=-1 || host.indexOf('llss')!=-1 || host.indexOf('liuli')!=-1)&&boxON[0])addBox();
        if(host.indexOf('tianshit')!=-1&&boxON[4])addBox();
        var aaa=$(".entry-title a");
        for(var j=0;j<aaa.length;j++){
            var ax=aaa[j];
            var dir=ax.href.indexOf(':');
            ax.href="https"+ax.href.substring(dir);
        }
        //调整神秘代码
        var toogle = document.getElementsByClassName('toggle-box')[0];
        if (toogle) toogle.style.display = 'block';
        var oldDriver = document.getElementsByClassName('entry-content')[0];
        if(host.indexOf('tianshit')!=-1)oldDriver=document.getElementsByClassName('article-content')[0];
        var childDriver = oldDriver.childNodes;
        for (var i = childDriver.length - 1; i >= 0; i--){
            var takeMe = childDriver[i].textContent.match(/(\w{40})|(([A-Za-z0-9]{2,39})( ?)[\u4e00-\u9fa5 ]{2,}( ?)+(\w{2,37})\b)/g);
            if (takeMe){
                for (j = 0; j < takeMe.length; ++j){
                    var has = takeMe[j].toString().replace(/(\s|[\u4e00-\u9fa5])+/g, '').trim();
                    if (has.length >= 40){
                        var fuel = "<a href='magnet:?xt=urn:btih:" + has + "'>老司机链接</a>"+"("+has+")";
                        if(childDriver[i].innerHTML)
                            childDriver[i].innerHTML = childDriver[i].innerHTML.toString().replace(takeMe[j], fuel);
                        else if(childDriver[i].parentElement)
                            if(childDriver[i].parentElement.innerHTML)
                                childDriver[i].parentElement.innerHTML=childDriver[i].parentElement.innerHTML.replace(takeMe[j], fuel);
                    }
                }
            }
            else{
                takeMe=childDriver[i].textContent.match(/(\w{32})|(([A-Za-z0-9]{2,31})( ?)[\u4e00-\u9fa5 ]{2,}( ?)+(\w{2,29})\b)/g);
                if(takeMe){
                    for (j = 0; j < takeMe.length; ++j){
                        var has2 = takeMe[j].toString().replace(/(\s|[\u4e00-\u9fa5])+/g, '').trim();
                        if (has2.length >= 32){
                            var fuel2 = "<a href='magnet:?xt=urn:btih:" + has2 + "'>老司机链接</a>"+"("+has2+")";
                            childDriver[i].innerHTML = childDriver[i].innerHTML.toString().replace(takeMe[j], fuel2);
                        }
                    }
                }
            }
        }
        //小八链接
        var buDang=document.getElementsByClassName('comment-content');
        for (i in buDang){
            if(buDang[i].innerHTML){
                buDang[i].innerHTML=buDang[i].innerHTML.replace(/b?\/?s?\/?\b(\w{8})\b/g,"<a href='http://pan.baidu.com/s/$1'>百度网盘</a>");
            }
        }
    }
    //百度云填写验证码
    else{
        var sCode = hash.slice(1).trim();
        if(sCode==='')return;
        setTimeout (function (){
            codeBox = $('.pickpw').find('input');
            btnOk = $('.pickpw').find('.g-button');
            if(codeBox)
            {
                codeBox.val(sCode);
                btnOk.click();
            } else {
                var codeBox = $(conf.code),btnOk = $(conf.btn);
                codeBox.val(sCode);
                if (conf.preSubmit)
                    if (conf.preSubmit (codeBox, btnOk))
                        return ;
                btnOk.click();
            }
        }, 10);
    }
})();
var hostName = location.host.match(/\w+\.\w+$/)[0].toLowerCase();
var conf = site[hostName];
var HostArr = [];
for(var i in site) HostArr.push(i);
var HostExp = new RegExp(HostArr.join("|"),'i');
if(site.JumpUrl[host]){
    site.JumpUrl[host].each(function(){
        $(this).attr({'href':$(this).text(),'target':'blank'});
    });
}
//跳转(点击链接、前进后退)时开启老司机模式
function lsjmodeON(){
    if(urls.indexOf("archives")!=-1){
        var pret = document.getElementsByTagName('pre')[0];
        if(pret)pret.style.display='inline';//文章页跳转文章页特殊处理
    }
    if(urls==location.href){
        var pret2 = document.getElementsByTagName('pre')[0];
        if(pret2)pret2.style.display='block';//文章页跳转文章页特殊处理
        return;
    }
    $("#attention").remove();
    urls=location.href;
    var re=urls.indexOf("archives");
    if(re!=-1){
        var pre = document.getElementsByTagName('pre')[0];
        var trytime=0;
        var itval=setInterval(function (){
            var pre = document.getElementsByTagName('pre')[0];
            trytime+=1;
            if(trytime==40)clearInterval(itval);
            if(pre){
                if(pre.style.display!='inline'){
                    if(ifshow()){
                        var prex = document.getElementsByTagName('pre');
                        for(var rmi=0;rmi<prex.length;rmi++)
                            if (prex[rmi]) prex[rmi].style.display = 'block';
                        //pre.style.display='block';
                        clearInterval(itval);
                    }
                    else clearInterval(itval);
                }
            }
        },500);
    }
}
//监控点击事件
$('body').on('click', 'a', function (){
    if(host=="blog.reimu.net")lsjmodeON();
    //整合提取码和链接
    if(aiON===0)return;
    var target=this;
    if(this.hash) return;
    if(HostExp.test(this.href)&&!/(?:eyun|tieba)\.baidu\.com/i.test(this.href)){
        if(target.nextSibling&&site['codeRule'].test(target.nextSibling.textContent.trim())){
            if(!/#/i.test(target.href)) target.href+='#'+extCode(target.nextSibling);}
        else{
            var cajhc=target.nextSibling.data;
            if(!cajhc)cajhc=target.nextSibling.text;
            if(cajhc.indexOf(":")!=-1){
                target.href+='#'+cajhc.substring(cajhc.indexOf(":")+1,cajhc.indexOf(":")+3);
            }
            else target.href+='#8酱';
        }
    }
});
//提取码
function extCode(obj){
    text=obj.textContent.trim();
    var rule=new RegExp('(?:提取|访问)[码碼]?\s*[:: ]?\\s*([a-z\\d]{4})','i');
    return rule.test(text)?text.match(rule)[1]:text.match(site['codeRule'])[1];
}
//老司机快捷键,并移除重口味提示
jQuery(document).keypress(function(e){
    if(odON===0)return;//快捷键无效
    if(e.ctrlKey && e.which == 13 || e.which == 10) {
        var pre = document.getElementsByTagName('pre');
        for(var rmi=0;rmi<pre.length;rmi++)
            if (pre[rmi]) pre[rmi].style.display = 'block';
        $("#attention").remove();
    }
});
//支持前进后退
jQuery(document).ready(function ($) {
    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
            var hashLocation = location.hash;
            var hashSplit = hashLocation.split("#!/");
            var hashName = hashSplit[1];
            if (hashName !== '') {
                var hash = window.location.hash;
                if (hash === '') lsjmodeON();
            }
        });
    }
});*/