Greasy Fork

Greasy Fork is available in English.

wytk-tieba

无影坦克

当前为 2021-02-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         wytk-tieba
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  无影坦克
// @author       cjq
// @match        https://tieba.baidu.com/p/*
// @match        http://tieba.baidu.com/photo/p*
// @match        http://tiebapic.baidu.com/forum/pic/item/*
// @grant        GM_setValue
// @grant        GM_getValue
// @require      http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js
// ==/UserScript==

(function () {
    jQuery.noConflict();
    //引入原脚本
    function utf8Encode(string) {
        var utftext = "";
        for (var n = 0; n<string.length; n++) {
            var c = string.charCodeAt(n);
            if (c<128) {
                utftext += String.fromCharCode(c);
            } else if ((c>127) && (c<2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    }

    function utf8Decode(inputStr) {
        var outputStr = "";
        var code1, code2, code3, code4;
        for(var i = 0; i < inputStr.length; i++) {
            code1 = inputStr.charCodeAt(i);
            if(code1 < 128) {
                outputStr += String.fromCharCode(code1);
            }else if(code1 < 224) {
                code2 = inputStr.charCodeAt(++i);
                outputStr += String.fromCharCode(((code1 & 31) << 6) | (code2 & 63));
            }else if(code1 < 240) {
                code2 = inputStr.charCodeAt(++i);
                code3 = inputStr.charCodeAt(++i);
                outputStr += String.fromCharCode(((code1 & 15) << 12) | ((code2 & 63) << 6) | (code3 & 63));
            }else {
                code2 = inputStr.charCodeAt(++i);
                code3 = inputStr.charCodeAt(++i);
                code4 = inputStr.charCodeAt(++i);
                outputStr += String.fromCharCode(((code1 & 7) << 18) | ((code2 & 63) << 12) |((code3 & 63) << 6) | (code2 & 63));
            }
        }
        return outputStr;
    }

    let IMG1=new Image();
    let IMGINFO=[];
    let IMG2=new Image();
    let MODE=4;
    let SRC1="";
    let SRC2="";
    let CURR_URL="";
    var DOMAIN=document.URL;

    function a2(){
        try{
            let f=sol();
            if(SRC2){URL.revokeObjectURL(SRC2)}
            SRC2=URL.createObjectURL(f[0]);
            jQuery("#" + String(id - 1)).attr("src",SRC2)
            jQuery("#" + String(id - 1)).before(createTip("图片读取成功!"))
        }catch(e){
            tryOpenOriginPicWhenDecodeError();
            jQuery("#" + String(id - 1)).before(createTip("解析读取失败,试试放大。如果已经放大,那就是无法解析。"))
        }
    }

    function sol(){
        let cv=document.createElement("canvas");
        let cvd=cv.getContext("2d");
        cv.width=IMG2.width;
        cv.height=IMG2.height;
        cvd.drawImage(IMG2,0,0);
        let imgdata=cvd.getImageData(0,0,IMG2.width,IMG2.height);
        let klist=de(imgdata.data[2]%8,imgdata);
        let file=new File([klist[1].buffer],utf8Decode(klist[0][1]),{type:klist[0][2]})
        return [file,utf8Decode(klist[0][1])]
    }

    function de(mode,imgdata){
        let aa=Math.ceil(3*mode/8);
        let n=imgdata.width*imgdata.height;
        let j=0;
        let k="";
        let i=1;
        let mlist=[1,2,4,8,16,32,64,128];
        let word="";
        let blist//=new Uint8Array();
        let blength=0;
        while(i<n && (word.length==0 || word.slice(-1).charCodeAt(0)>0)){
            k=k+(imgdata.data[4*i  ]+256).toString(2).slice(-mode);
            k=k+(imgdata.data[4*i+1]+256).toString(2).slice(-mode);
            k=k+(imgdata.data[4*i+2]+256).toString(2).slice(-mode);
            i++
            for(let ii=0;ii<aa;ii++){
                if(k.length>=8 && (word.length==0 || word.slice(-1).charCodeAt(0)>0)){
                    word=word+String.fromCharCode(parseInt(k.slice(0,8),2));
                    k=k.slice(8);
                }
            }
        }
        //word分隔符:","
        blength=parseInt(word.split(String.fromCharCode(1))[0]);
        if(!(blength>-1)){
            throw "error"
        }
        if(!(word.split(String.fromCharCode(1)).length>2)){
            throw "error"
        }
        blist=new Uint8Array(blength);
        if(k.length>=8 && j<blength){
            blist[j]=parseInt(k.slice(0,8),2);
            k=k.slice(8);
            j++
        }
        while(i<n && j<blength){
            k=k+(imgdata.data[4*i  ]+256).toString(2).slice(-mode);
            k=k+(imgdata.data[4*i+1]+256).toString(2).slice(-mode);
            k=k+(imgdata.data[4*i+2]+256).toString(2).slice(-mode);
            i++
            for(let ii=0;ii<aa;ii++){
                if(k.length>=8 && j<blength){
                    blist[j]=parseInt(k.slice(0,8),2);
                    k=k.slice(8);
                    j++
                }
            }
        }
        return [word.split(String.fromCharCode(0))[0].split(String.fromCharCode(1)),blist]
    }

    //url转data
    function getImageFileFromUrl(url, imageName,callback) {

        // imageName一定要带上后缀
        var blob = null;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.setRequestHeader('Accept', 'image/jpeg');
        xhr.responseType = "blob";
        xhr.onload = () => {
            if (xhr.status == 200) {
                blob = xhr.response;
                let imgFile = new File([blob], imageName, {type: 'image/jpeg'});
                console.log(imgFile)
                callback.call(this,imgFile);
            }else{jQuery("#" + String(id)).before(createTip("网络出错,无法读取。"))}};
        xhr.send();

    }

    //获取协议类型
    function getProtocolStr(url){
        var reg=/http:/;
        if(reg.test(url)){
            return "http";
        }else{
            return "https"
        }
    }

    //得到链接中图片的名称
    function getUrlLastPathNameStr(url){
        var index = url.lastIndexOf("\/");
        var last= url.substring(index + 1, url.length);
        return last;
    }

    //如果解析失败可能是因为图片需要放大解析,但是由于跨域问题,所以要在新页面中解析,此时hover事件再次绑定成功,即可解析
    function tryOpenOriginPicWhenDecodeError(){
        if(DOMAIN.startsWith("https://tieba.baidu.com")){
            window.open('http://tiebapic.baidu.com/forum/pic/item/'+getUrlLastPathNameStr(CURR_URL));
        }
    }

    //创建提示标签
    function createTip(str){
        return "<p style=\"font-size:15px;color:orange\">"+str+"</p><br/>";
    }

    var id = 0;

    if(!GM_getValue('firstTime')){
        GM_setValue('firstTime',true);
        GM_setValue('pluginOnOffFlag',true);
    }
    console.log(GM_getValue('pluginOnOffFlag'));
    var userConfig={
        pluginOnOffFlag:GM_getValue('pluginOnOffFlag')
    }

    window.onload=function(){
        //下面这两行不知道是干什么的,屏蔽掉了
        //let w="<!DOCTYPE "+"html>"+document.documentElement.outerHTML;
        //document.getElementById("bc").href=URL.createObjectURL(new Blob([w],{type:"text/html"}))

        var timerId=setInterval(function(){

            //考虑到有些用户会不自觉放大页面
            //检查是不是有原图,有则跳转到新标签页显示,因为在原图网页我绑定不了hover事件
            var picURL=jQuery('.image_original_original').attr('src');
            if(picURL!=undefined && picURL!=''){
                window.open(picURL);
                clearInterval(timerId);
            }
        },1000);

        jQuery("body").append("\
<div id='plugin_float' \
style='left: 20px;\
bottom: 20px;\
background: #9beee2;\
color:#3e32d2;\
overflow: hidden;\
z-index: 9999;\
position: fixed;\
padding:5px;\
text-align:center;\
font-size:20px;\
'></div>");

        jQuery("#plugin_float").append('无影坦克𝐈𝐈:<br>')
            .append('<input type="checkbox" id="plugin_enable_state"> 插件使能 <br>');

        if(userConfig.pluginOnOffFlag){
            jQuery('#plugin_enable_state').attr('checked','checked');
        }

    }

    //冒泡事件,防止动态加载
    jQuery("body").on("hover","img",function(event){

        //只响应鼠标移动到图片上,忽略移开
        if(event.type=="mouseenter"){

            if(!userConfig.pluginOnOffFlag)
                return;

            //忽略已经处理过的图片
            if(jQuery(this).attr("id")==(id-1))
                return;

            jQuery(this).attr("id",String(id));
            jQuery("#" + String(id)).before(createTip("正在加载。。。若长时间无反应请手动点击图片!"));
            id = id + 1;

            var url=jQuery(this).attr("src");
            CURR_URL=url;

            //请求资源的协议看domain不看资源的src,否则会产生mixed错误
            getImageFileFromUrl(getProtocolStr(DOMAIN) + url.substring(4,url.length),'testFile.jpg',function(file){
                console.log(file)
                var r = new FileReader()
                r.readAsDataURL(file)
                r.onloadend = function(oFRevent){
                    var osrc = oFRevent.target.result;
                    IMG2.src=osrc;
                    IMG2.onload=function(){
                        a2()
                    }
                }
            });
        }
    });

    jQuery("body").on("click",function(event){
        //console.log(event.target);
        if(event.target.id=="plugin_enable_state"){
            userConfig.pluginOnOffFlag=jQuery('#plugin_enable_state').is(':checked');
            GM_setValue('pluginOnOffFlag',userConfig.pluginOnOffFlag);
            console.log("插件使能状态改变:"+userConfig.pluginOnOffFlag);
        }
    });

})();