发布价值信息
Wordpress、Software、VPN。。

Js + Html 自定义 网站鼠标右键功能

过年这几天闲着没事逛了几圈博客,发现好多网站更改了右键或者直接禁止了右键功能,索性直接扒了代码过来除除草,何先生博客因为工作原因,最近更新文章确实太少了。

更改网站右键功能有什么用呢?

  1. 可以简单定制和排列自己想要实现的右键功能
  2. 可以一定程度防止审查元素,当然只能防止一些小白,直接 F12 即可,如果是 Mac 电脑的 Safari 浏览器 Commend + Option + I 即可打开审查元素。

废话不多说,进入正题:

下面代码必须在 jQuery 之下运行,如果你的网站没有引入 jQuery,我们先引入:

<script type="text/javascript" src="//apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>

上代码:

(function(a) {
    a.extend({
        mouseMoveShow: function(b) {
            var d = 0,
                c = 0,
                h = 0,
                k = 0,
                e = 0,
                f = 0;
            a(window).mousemove(function(g) {
                d = a(window).width();
                c = a(window).height();
                h = g.clientX;
                k = g.clientY;
                e = g.pageX;
                f = g.pageY;
                h + a(b).width() >= d && (e = e - a(b).width() - 5);
                k + a(b).height() >= c && (f = f - a(b).height() - 5);
                a("html").on({
                    contextmenu: function(c) {
                        3 == c.which && a(b).css({
                            left: e,
                            top: f
                        }).show()
                    },
                    click: function() {
                        a(b).hide()
                    }
                })
            })
        },
        disabledContextMenu: function() {
            window.oncontextmenu = function() {
                return !1
            }
        }
    })
})(jQuery);

function getSelect() {
    "" == (window.getSelection ? window.getSelection() : document.selection.createRange().text) ? alert("啊噢...你没还没选择文字呢!") : document.execCommand("Copy")
}
function baiduSearch() {
    var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
    "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("https://www.baidu.com/s?wd=" + a)
}
function googleSearch() {
    var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
    "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("https://www.google.com/search?q=" + a)
}
function localSearch() {
    var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
    "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("search.php?q=" + a)
}
$(function() {
    for (var a = navigator.userAgent, b = "Android;iPhone;SymbianOS;Windows Phone;iPad;iPod".split(";"), d = !0, c = 0; c < b.length; c++) if (0 < a.indexOf(b[c])) {
        d = !1;
        break
    }
    d && ($.mouseMoveShow(".usercm"), $.disabledContextMenu())
});

这段 Js 代码无需做更改,当然如果你是大牛,你懂代码想加入其它功能,是可以的。

下面是 Html 代码:

<div class="usercm">
    <ul>
     <li><a href="javascript:void(0);" onclick="getSelect();">
        <i class="fa fa-clipboard fa-fw"></i><span>复制文字</span></a></li>
        <li><a href="javascript:window.location.reload();"><i class="fa fa-refresh fa-fw"></i><span>刷新页面</span></a></li>
        <li><a href="javascript:history.go(1);"><i class="fa fa-arrow-right fa-fw"></i><span>前进一页</span></a></li>
        <li><a href="javascript:history.go(-1);"><i class="fa fa-arrow-left fa-fw"></i><span>后退一页</span></a></li>
        <li><a target="_blank" href="javascript:void(0);" onclick="baiduSearch();"><i class="fa fa-paw fa-fw"></i><span>百度搜索</span></a></li>
       <li><a target="_blank" href="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BD%95%E5%85%88%E7%94%9F&oq=%25E4%25BD%2595%25E5%2585%2588%25E7%2594%259F&rsv_pq=828bf96c00048de2&rsv_t=384aNvt4%2FBe4gTkflvzhIKRiR5GEFyey2Xz9lsZ59Lrbr3dAMrBHaOGNfX0&rqlang=cn&rsv_enter=0"><i class="fa fa-search"></i><span>百度优化</span></a></li>
       <li><a target="_blank" href="https://author.baidu.com/home/1613749953561222"><i class="fa fa-paw" aria-hidden="true"></i><span>关注熊掌号</span></a></li>
    </ul>
</div>

你需要修改指定上面代码中链接地址为你自己的,比如熊掌号:我的熊掌号 ID 为 1613749953561222,你需要修改这串数字为你的熊掌号 ID。

最后是 Css 代码:

a {text-decoration: none;}
    div.usercm{background-repeat:no-repeat;background-position:center center;background-size:cover;background-color:#fff;font-size:13px!important;width:130px;-moz-box-shadow:1px 1px 3px rgba
(0,0,0,.3);box-shadow:1px 1px 3px rgba(0,0,0,.3);position:absolute;display:none;z-index:10000;opacity:0.9;}
    div.usercm ul{list-style-type:none;list-style-position:outside;margin:0px;padding:0px;display:block}
    div.usercm ul li{margin:0px;padding:0px;line-height:35px;border-bottom:1px solid #ccc;}
    div.usercm ul li a{color:#666;padding:0 15px;display:block}
    div.usercm ul li a:hover{color:#fff;background:rgba(170,222,18,0.88)}
    div.usercm ul li a i{margin-right:10px}
    a.disabled{color:#c8c8c8!important;cursor:not-allowed}
    a.disabled:hover{background-color:rgba(255,11,11,0)!important}
    div.usercm{background:#fff !important;}

接下来我们把它合并,并应用到网站上,何先生使用的是 Wordprss 程序,我们打开主题文件夹,找到 Footer.php,把下面代码粘贴到 Html 标签 </body> 之上就完成了:

<style type="text/css">
    a {text-decoration: none;}
    div.usercm{background-repeat:no-repeat;background-position:center center;background-size:cover;background-color:#fff;font-size:13px!important;width:130px;-moz-box-shadow:1px 1px 3px rgba
(0,0,0,.3);box-shadow:1px 1px 3px rgba(0,0,0,.3);position:absolute;display:none;z-index:10000;opacity:0.9;}
    div.usercm ul{list-style-type:none;list-style-position:outside;margin:0px;padding:0px;display:block}
    div.usercm ul li{margin:0px;padding:0px;line-height:35px;border-bottom:1px solid #ccc;}
    div.usercm ul li a{color:#666;padding:0 15px;display:block}
    div.usercm ul li a:hover{color:#fff;background:rgba(170,222,18,0.88)}
    div.usercm ul li a i{margin-right:10px}
    a.disabled{color:#c8c8c8!important;cursor:not-allowed}
    a.disabled:hover{background-color:rgba(255,11,11,0)!important}
    div.usercm{background:#fff !important;}
    </style>
    <div class="usercm">
    <ul>

     <li><a href="javascript:void(0);" onclick="getSelect();">
        <i class="fa fa-clipboard fa-fw"></i><span>复制文字</span></a></li>
        <li><a href="javascript:window.location.reload();"><i class="fa fa-refresh fa-fw"></i><span>刷新页面</span></a></li>
        <li><a href="javascript:history.go(1);"><i class="fa fa-arrow-right fa-fw"></i><span>前进一页</span></a></li>
        <li><a href="javascript:history.go(-1);"><i class="fa fa-arrow-left fa-fw"></i><span>后退一页</span></a></li>
        <li><a target="_blank" href="javascript:void(0);" onclick="baiduSearch();"><i class="fa fa-paw fa-fw"></i><span>百度搜索</span></a></li>
       <li><a target="_blank" href="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%BD%95%E5%85%88%E7%94%9F&oq=%25E4%25BD%2595%25E5%2585%2588%25E7%2594%259F&rsv_pq=828bf96c00048de2&rsv_t=384aNvt4%2FBe4gTkflvzhIKRiR5GEFyey2Xz9lsZ59Lrbr3dAMrBHaOGNfX0&rqlang=cn&rsv_enter=0"><i class="fa fa-search"></i><span>百度优化</span></a></li>
       <li><a target="_blank" href="https://author.baidu.com/home/1613749953561222"><i class="fa fa-paw" aria-hidden="true"></i><span>关注熊掌号</span></a></li>
    </ul>
    </div>   
     
    <script type="text/javascript">
    (function(a) {
        a.extend({
            mouseMoveShow: function(b) {
                var d = 0,
                    c = 0,
                    h = 0,
                    k = 0,
                    e = 0,
                    f = 0;
                a(window).mousemove(function(g) {
                    d = a(window).width();
                    c = a(window).height();
                    h = g.clientX;
                    k = g.clientY;
                    e = g.pageX;
                    f = g.pageY;
                    h + a(b).width() >= d && (e = e - a(b).width() - 5);
                    k + a(b).height() >= c && (f = f - a(b).height() - 5);
                    a("html").on({
                        contextmenu: function(c) {
                            3 == c.which && a(b).css({
                                left: e,
                                top: f
                            }).show()
                        },
                        click: function() {
                            a(b).hide()
                        }
                    })
                })
            },
            disabledContextMenu: function() {
                window.oncontextmenu = function() {
                    return !1
                }
            }
        })
    })(jQuery);
     
    function getSelect() {
        "" == (window.getSelection ? window.getSelection() : document.selection.createRange().text) ? alert("啊噢...你没还没选择文字呢!") : document.execCommand("Copy")
    }
    function baiduSearch() {
        var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
        "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("https://www.baidu.com/s?wd=" + a)
    }
    function googleSearch() {
        var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
        "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("https://www.google.com/search?q=" + a)
    }
    function localSearch() {
        var a = window.getSelection ? window.getSelection() : document.selection.createRange().text;
        "" == a ? alert("啊噢...你没还没选择文字呢!") : window.open("search.php?q=" + a)
    }
    $(function() {
        for (var a = navigator.userAgent, b = "Android;iPhone;SymbianOS;Windows Phone;iPad;iPod".split(";"), d = !0, c = 0; c < b.length; c++) if (0 < a.indexOf(b[c])) {
            d = !1;
            break
        }
        d && ($.mouseMoveShow(".usercm"), $.disabledContextMenu())
    });
    </script>

我们来看看实际效果:

展开阅读全文
赞(2) 打赏本站

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
打赏作者
未经允许不得转载:何先生 » Js + Html 自定义 网站鼠标右键功能
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

何先生的博客 专注分享 更优质

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

何先生自用推荐:Vultr vps

限时:注册即可赠送 $25美元

服务器每月仅需:$5/月

注册