百度贴吧图片点击放大

点击图片放大,再次点击还原,滚轮上下移动图片,可以各种组合搭配

目前为 2016-07-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         百度贴吧图片点击放大
// @namespace    https://github.com/lliwhx/Userscripts
// @version      1.4.25.20160713
// @author       lliwhx
// @description  点击图片放大,再次点击还原,滚轮上下移动图片,可以各种组合搭配
// @homepage     http://weibo.com/lliwhx
// @supportURL   https://github.com/lliwhx/Userscripts/issues
// @include      http://tieba.baidu.com/p/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// @grant        GM_openInTab
// @run-at       document-start
// @noframes     true
// @compatible   chrome
// @copyright    Copyright (c) 2016 lliwhx
// @license      The MIT License (MIT)
// ==/UserScript==

/*
 * The MIT License (MIT)
 * Copyright (c) 2016 lliwhx
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

//自定义设置已经移动到Monkey的选项菜单,随时设置随时应用

GM_registerMenuCommand("自定义设置", function(){
    document.getElementById("yawf-user-guide").style.display = "block";
},"s");

(function(){
    "use strict";

    //变量声明
    var Fon=[],
        imgEvent = {},
        documentHeight,
        documentWidth,
        timer = null,
        clicks = 0,
        thread_id = location.pathname.substring(3),
        forum_name = document.querySelector("[fname]").getAttribute("fname"),
        target = null,
        imgMouse = {x:0,y:0,left:0,top:0,button:true},
        _data = [];
    //自定义设置
    var checkform = function(e){
        var f = e.target;
        Fon = [f.click.value,f.esc.value,f.scroll.value,f.restore.value];
        GM_setValue("setup",Fon.join());
        _data[0].style.display = "none";
    };
    var createSetup = function(){
        var div = document.createElement("div");
        div.id = "yawf-user-guide";
        div.className = "W_layer yawf-Layer";
        div.style.display = "none";
        div.innerHTML = '<div class="content" style="width:400px!important"><div class="W_layer_title">自定义设置 (在Monkey选项菜单可开启)</div><div><form id="yawf-user-form" onsubmit="return false"><div class="layer_point"><div>鼠标放大操作<br><label>单击 <input type="radio" checked name="click" value="0"></label><label>双击 <input type="radio" name="click" value="1"></label></div><br><div>关闭放大图片<br><label>单击 <input type="radio" checked name="esc" value="0"></label><label>双击 <input type="radio" name="esc" value="1"></label></div><br><div>滚轮默认上下移动图片,搭配组合键完成图片放大缩小<br><label>关闭 <input type="radio" checked name="scroll" value="0"></label><label>CTRL <input type="radio" name="scroll" value="1"></label><label>ALT <input type="radio" name="scroll" value="2"></label><label>SHIFT <input type="radio" name="scroll" value="3"></label></div><br><div>滚轮默认上下移动图片,搭配组合键完成图片放大缩小<br><label>关闭 <input type="radio" checked name="restore" value="0"></label><label>CTRL <input type="radio" name="restore" value="1"></label><label>ALT <input type="radio" name="restore" value="2"></label><label>SHIFT <input type="radio" name="restore" alue="3"></label></div></div><div class="W_layer_btn S_bg1"><input type="submit" value="确定" class="W_btn_a btn_34px"></div></form></div></div>';
        _data[2] = document.body;
        _data[2].appendChild(div);
        _data[0] = document.getElementById("yawf-user-guide");
        _data[1] = document.getElementById("yawf-user-form");
        _data[1].addEventListener("submit",function(e){checkform(e);});
        if(GM_getValue("setup")){
            var form = _data[1];
            Fon = GM_getValue("setup").split(",");
            form.click[Fon[0]].click();
            form.esc[Fon[1]].click();
            form.scroll[Fon[2]].click();
            form.restore[Fon[3]].click();
        }
    };
    GM_addStyle(".W_layer,.W_layer legend,.W_layer .W_input:focus,.W_layer .S_txt1,.W_layer .W_btn_b,.W_layer .SW_fun .S_func1{color: #333; text-decoration: none;}.W_layer{position: fixed; z-index: 9999;top:50%;left:50%;transform:translate(-50%,-50%);}.W_layer .content{position: relative; background: #fff; border-radius: 3px; border-top: 2px solid #fa7f40;}.W_layer .W_layer_title{border-bottom: 1px solid #f2f2f5; line-height: 38px; font-size: 14px; font-weight: 700; padding: 0 0 0 16px; vertical-align: middle;}.layer_point{padding: 20px 40px; text-align: center; *position: relative;}.layer_point .point dd p.S_txt1{text-align: left;}.W_layer .S_bg1,.W_layer blockquote,.W_layer .SW_fun_bg:hover,.W_layer .SW_fun_bg_active{background-color: #f2f2f5;}.W_layer .W_layer_btn{position: relative; min-width: 200px; *min-width: auto; border-radius: 3px; text-align: center; padding: 10px 0; *padding: 10px 0 10px 10px;}.W_layer .W_layer_btn a{margin: 0 5px;}.W_layer .W_btn_a{color: #fff;}.btn_34px{display: inline-block; height: 34px; line-height: 35px; padding: 0 15px; font-size: 14px;}.W_btn_a{background: #ff8140; border: 1px solid #f77c3d; color: #fff; box-shadow: 0px 1px 2px rgba(0,0,0,0.25);}.W_btn_a,.W_btn_b,.W_btn_c,.W_btn_d{padding: 0 10px 0 10px; white-space: nowrap; display: inline-block; border-radius: 2px; height: 24px; line-height: 25px; *line-height: 24px; text-decoration: none; font-size: 12px; min-width: 40px; text-align: center;}");
    var Fon_fun = {
        click:function(){
            if(Fon[0]==="0")
                return "click";
            return "dblclick";
        },
        esc:function(){
            if(Fon[1]==="0")
                return "click";
            return "dblclick";
        },
        scroll:function(e){
            if(Fon[2]==="0")
                return e.ctrlKey===true&&e.altKey===true&&e.shiftKey===true;
            if(Fon[2]==="1")
                return e.ctrlKey;
            if(Fon[2]==="2")
                return e.altKey;
            if(Fon[2]==="3")
                return e.shiftKey;
        },
        restore:function(e){
            if(Fon[3]==="0")
                return false;
            if(Fon[3]==="1")
                return e.ctrlKey;
            if(Fon[3]==="2")
                return e.altKey;
            if(Fon[3]==="3")
                return e.shiftKey;
        }
    };
    //图片样式
    var randomColor = function(){var colorArr = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];var colorVal = "#";for (var i=0;i<6;i++) {colorVal+=colorArr[Math.floor(Math.random()*16)];}return colorVal;};
    GM_addStyle(" .enlargeImg {position:fixed;z-index:999;box-shadow:0 10px 45px #233;border:7px solid white;top:50%;left:50%;transform:translate(-50%,-50%);} .enlargeImg:hover {border-color:" + randomColor() + ";}");
    randomColor=null;
    //主事件
    imgEvent.init = function(element){
        documentHeight = document.documentElement.clientHeight;
        documentWidth = document.documentElement.clientWidth;
        element.addEventListener("click",function(e){
            var target = e.target;
            if(target.className=="BDE_Image"&&e.button === 0){
                var regex=target.getAttribute("src").match(/([a-z0-9]{15,})\.[a-zA-Z]{3,}$/);
                $(target).off("click");
                if(!regex)
                    return false;
                if(Fon_fun.restore(e)){
                    imgEvent.original(regex[1]);
                    return false;
                }
                if(Fon_fun.click()==="click"){
                    imgEvent.view(regex[0]);
                }else{
                    clicks++;
                    if(clicks===2){
                        clearTimeout(timer);
                        clicks=0;
                        imgEvent.view(regex[0]);
                        return false;
                    }
                    timer = setTimeout(function(){
                        clicks=0;
                    },200);
                }
                e.stopPropagation();
            }
        },true);};
    //原事件
    imgEvent.original = function(uid){
        GM_openInTab("http://tieba.baidu.com/photo/p?kw=" + forum_name + "&flux=1&tid=" + thread_id + "&pic_id=" + uid + "&pn=1&fp=2&see_lz=");
    };
    //点击事件
    imgEvent.view = function(uid){
        var img = document.createElement("img");
        img.src="http://imgsrc.baidu.com/forum/pic/item/" + uid;
        img.className = "enlargeImg";
        img.onmousedown = imgEvent.down; //给img添加鼠标down事件
        img.onmousewheel = imgEvent.wheel; //给img添加滚轮事件
        _data[2].appendChild(img);
    };
    //img鼠标down事件
    imgEvent.down = function(e){
        if (e.button !== 0)
            return false;
        target = e.target;
        imgMouse.x = e.pageX;
        imgMouse.y = e.pageY;
        imgMouse.left = target.offsetLeft;
        imgMouse.top = target.offsetTop;
        imgMouse.button = true;
        document.addEventListener("mousemove",imgEvent.move);
        document.addEventListener("mouseup",imgEvent.up);
        e.preventDefault();
    };
    //持续按下
    imgEvent.move = function(e){
        imgMouse.button = false;
        target.style.cssText = "top:" + (e.pageY - imgMouse.y + imgMouse.top) + "px; left:" + (e.pageX - imgMouse.x + imgMouse.left) + "px";
        var Top = target.offsetTop,
            Left = target.offsetLeft,
            height = target.height,
            width = target.width;
        if(Top>height / 2 + documentHeight){ //下边界
            target.style.top = height / 2 + documentHeight + "px";
            return false;
        }
        if(Top<-height / 2){ //上边界
            target.style.top = -height / 2 + "px";
            return false;
        }
        if(Left>width / 2 + documentWidth){ //右边界
            target.style.left = width / 2 + documentWidth + "px";
            return false;
        }
        if(Left<-width / 2){ //左边界
            target.style.left = -width / 2 + "px";
            return false;
        }
    };
    //img鼠标up事件
    imgEvent.up = function(e){
        document.removeEventListener("mousemove",imgEvent.move);
        document.removeEventListener("mouseup",imgEvent.up);
        if (imgMouse.button)
            if(Fon_fun.esc()=="click"){
                _data[2].removeChild(target);
            }else{
                clicks++;
                if(clicks===2){
                    clearTimeout(timer);
                    clicks=0;
                    _data[2].removeChild(target);
                    return false;
                }
                timer = setTimeout(function(){
                    clicks=0;
                },200);
            }
    };
    //img滚轮事件
    imgEvent.wheel = function(e) {
        var target = e.target,
            Height = target.naturalHeight/2,
            height = target.height,
            Top;
        if(Fon_fun.scroll(e)){
            height = height - e.wheelDelta/2;
            target.height = height < Height ? Height : height;
            return false;
        }
        if(height<documentHeight)
            return false;
        target.style.top = target.offsetTop + e.wheelDelta/2 + "px";
        Top = target.offsetTop;
        if(Top>height / 2 + 7){ //上边界
            target.style.top = height / 2 + 7 + "px";
            return false;
        }
        if(Top<documentHeight - height / 2 - 7){ //下边界
            target.style.top = documentHeight - height / 2 - 7 + "px";
            return false;
        }
        e.preventDefault();
    };

    var checkElement = window.setInterval(function(){
        var element = document.getElementById("j_p_postlist");
        if(element){
            createSetup();
            if(!GM_getValue("setup")){
                _data[0].style.display = "block";
            }
            imgEvent.init(element);
            clearInterval(checkElement);
        }},100);

})();