百度贴吧图片点击放大

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

目前为 2016-06-27 提交的版本。查看 最新版本

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

/*
 * 本脚本基于" 百度贴吧图片放大 "重构:
 * 作者:          o丨Reborn
 * 博客:          http://blog.sbw.so/
 * Email:         [email protected]
 * 原脚本地址:   https://greasyfork.org/zh-CN/scripts/2371-%E7%99%BE%E5%BA%A6%E8%B4%B4%E5%90%A7%E5%9B%BE%E7%89%87%E6%94%BE%E5%A4%A7
 */

/*
 * 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.
 */


var Fon = {
    //鼠标放大操作 —— 0:单击 ,1:双击

    "click" : 0 ,

    //键盘组合键,搭配上面的鼠标操作使用 —— 0:关闭 ,1:ctrl ,2:alt ,3:shift

    "key" : 0 ,

    //关闭放大图片 —— 0:单击 ,1:双击

    "esc" : 0 ,

    //滚轮默认上下移动图片,搭配组合键完成图片放大缩小 —— 0:关闭 ,1:ctrl ,2:alt ,3:shift

    "scroll": 0 ,

    //贴吧图片原事件,鼠标左键搭配组合键才能使用 —— 0:关闭 ,1:ctrl ,2:alt ,3:shift

    "restore": 0 ,

};

(function(){
    "use strict";

    //变量声明
    var Save,
        target,
        close,
        oldX,
        oldY,
        oldLeft,
        oldTop,
        num=0,
        setTime;

    //操作事件
    var Fon_fun = {
        click:function(){
            if(Fon.click===0)
                return "click";
            return "dblclick";
        },
        key:function(e){
            if(Fon.key===0)
                return e.ctrlKey!=1&&e.altKey!=1&&e.shiftKey!=1;
            if(Fon.key===1)
                return e.ctrlKey;
            if(Fon.key===2)
                return e.altKey;
            if(Fon.key===3)
                return e.shiftKey;
        },
        esc:function(){
            if(Fon.esc===0)
                return "click";
            return "dblclick";
        },
        scroll:function(e){
            if(Fon.scroll===0)
                return e.ctrlKey===true&&e.altKey===true&&e.shiftKey===true;
            if(Fon.scroll===1)
                return e.ctrlKey;
            if(Fon.scroll===2)
                return e.altKey;
            if(Fon.scroll===3)
                return e.shiftKey;
        },
        restore:function(e){
            if(Fon.restore===0)
                return false;
            if(Fon.restore===1)
                return e.ctrlKey;
            if(Fon.restore===2)
                return e.altKey;
            if(Fon.restore===3)
                return e.shiftKey;
        }
    };

    //添加图片样式
    GM_addStyle(
        " .crackImg {"+
        "   position:fixed; "+
        "   z-index:99999; "+
        "   box-shadow:0 10px 45px #233; "+
        "   border:7px solid white; "+
        "   top:50%; "+
        "   left:50%; "+
        "   transform:translate(-50%,-50%); "+
        " } "+
        " .crackImg:hover { "+
        "   border-color:#44a0ff;"+
        " } "
    );

    //img滚轮事件
    function pictureScroll(e) {
        target = e.target;
        if(Fon_fun.scroll(e)){
            var width = target.width - e.wheelDelta/2;
            target.width = width < 150 ? 150 : width;
            return false;
        }
        var oldY = parseInt($(target).css("top"));
        $(target).css({
            "top":oldY + e.wheelDelta/2 + "px",
        });
        if(target.offsetTop>target.height / 2 + 7){ //上边界
            $(target).css({
                "top":target.height / 2 + 7 + "px",
            });
            return false;
        }
        if(target.offsetTop<document.documentElement.clientHeight - target.height / 2 - 7){ //下边界
            $(target).css({
                "top":document.documentElement.clientHeight - target.height / 2 - 7 + "px",
            });
            return false;
        }
        return false;
    }

    //img鼠标down事件
    function picMouseDown(e){
        if (e.button !== 0)
            return false;
        target = e.target;
        oldX = e.pageX;
        oldY = e.pageY;
        oldLeft = parseInt($(target).css("left"));
        oldTop = parseInt($(target).css("top"));
        close = true;
        $(document).mousemove(picMouseMove);
        return false;
    }

    //持续按下状态
    function picMouseMove(e){
        close = false;
        $(target).css({
            "top":e.pageY - oldY + oldTop + "px",
            "left":e.pageX - oldX + oldLeft + "px"
        });
        if(target.offsetTop>target.height / 2 + document.documentElement.clientHeight){ //下边界
            $(target).css({
                "top":target.height / 2 + document.documentElement.clientHeight + "px",
            });
            return false;
        }
        if(target.offsetTop<-target.height / 2){ //上边界
            $(target).css({
                "top":-target.height / 2 + "px",
            });
            return false;
        }
        if(target.offsetLeft>target.width / 2 + document.documentElement.clientWidth){ //右边界
            $(target).css({
                "left":target.width / 2 + document.documentElement.clientWidth + "px",
            });
            return false;
        }
        if(target.offsetLeft<-target.width / 2){ //左边界
            $(target).css({
                "left":-target.width / 2 + "px",
            });
            return false;
        }
    }

    //img鼠标up事件
    function picMouseUp(){
        if (close)
            if(Fon_fun.esc()=="click")
                document.body.removeChild(target);
            else{
                num+=1;
                setTime = setTimeout(function(){num=0;},300);
                if(num>=2){
                    clearTimeout(setTime);
                    document.body.removeChild(target);
                    num=0;
                }
            }
        $(document).off("mousemove",picMouseMove);
    }


    //鼠标事件
    function pictureView(){
        var reg=/\/[a-z0-9]{20,}\.[a-zA-Z]{3,}/;
        var match=target.getAttribute("src").match(reg); //获取图片id
        if(!match)
            return false;
        var img = document.createElement("img");
        img.src="http://imgsrc.baidu.com/forum/pic/item" + match[0];
        img.className = "crackImg";
        document.body.appendChild(img);
        img.onmousedown = picMouseDown; //给img添加鼠标down事件
        img.onmouseup = picMouseUp; //给img添加鼠标up事件
        img.onmousewheel = pictureScroll; //给img添加滚轮事件
    }

    //保存原事件
    function SaveMouseDown(e){
        target = e.target;
        if(target.className=="BDE_Image"&&!Save){
            Save = $(".BDE_Image").data("events");
            if(Save){
                Save = Save.click[0].handler;
                window.removeEventListener("mousedown", SaveMouseDown,true);
            }
        }
    }
    window.addEventListener("mousedown",SaveMouseDown,true);

    //注册点击事件
    window.addEventListener("mouseup",function(e){
        target = e.target;
        if(target.className=="BDE_Image"&&e.button === 0){
            $(target).off("click");
            if(Fon_fun.restore(e)){
                $(target).one("click",Save);
                return false;
            }
            if (Fon_fun.key(e))
                if(Fon_fun.click()=="click")
                    pictureView();
                else{
                    num+=1;
                    setTime = setTimeout(function(){num=0;},300);
                    if(num>=2){
                        clearTimeout(setTime);
                        pictureView();
                        num=0;
                    }
                }
        }
    });

})();