JavaScript 代码
新建一个名为 qgg-user-rcm.js 的文件,将以下代码丢到该文件中去,以便后面引用。
/** 鼠标右键菜单功能 */ (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 printMe() { var global_Html = ""; global_Html = document.body.innerHTML; document.body.innerHTML = document.querySelector('.content').innerHTML; window.print(); window.setTimeout(function() { document.body.innerHTML = global_Html; }, 1500); } function getSelect() { var a = window.getSelection ? window.getSelection() : document.selection.createRange().text; "" == a ? 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() { 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(".user-rcm"), $.disabledContextMenu()) });
注意:如果你不需要判断用户身份以确认是否启用,也可以将代码直接复制到网站 JS 文件中。
您已支付
CSS 代码
CSS 代码配置没什么好说的,直接将下面代码丢到网站 CSS 文件中去即可。
/** 鼠标右键功能 */ a {text-decoration: none;} div.user-rcm{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.user-rcm ul{list-style-type:none;list-style-position:outside;margin:0px;padding:0px;display:block} div.user-rcm ul li{margin:0px;padding:0px;line-height:35px;border-bottom:1px solid #ccc;} div.user-rcm ul li a{color:#666;padding:0 15px;display:block} div.user-rcm ul li a:hover{color:#fff;background:rgba(36, 160, 240, 0.6);} div.user-rcm 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.user-rcm{background:#fff !important;}
CSS 样式大家可自行调整。。。
前端显示代码
新建一个名为 modify-user-right-click-menu.php 的文件,并将以下代码复制到该文件中以方便后面调用。
/** * @name 用户右键菜单修改 */ ?> <div class="user-rcm"><ul><li><a href="javascript:window.location.reload();" rel="external nofollow" ><i class="fa fa-refresh fa-fw">i><span>刷新页面span>a>li> <li><a href="javascript:history.go(1);" rel="external nofollow" ><i class="fa fa-arrow-right fa-fw">i><span>前进一页span>a>li> <li><a href="javascript:history.go(-1);" rel="external nofollow" ><i class="fa fa-arrow-left fa-fw">i><span>后退一页span>a>li> <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="getSelect();"><i class="fa fa-clipboard fa-fw">i><span>复制文字span>a>li> <li><a target="_self" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="printMe();"><i class="fa fa-print fa-fw">i><span>打印页面span>a>li> <li><a target="_blank" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="googleSearch();"><i class="fa fa-chrome fa-fw">i><span>谷歌搜索span>a>li> <li><a target="_blank" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="baiduSearch();"><i class="fa fa-paw fa-fw">i><span>百度搜索span>a>li> <li><a target="_blank" href="" rel="external nofollow" ><i class="fa fa-rss-square fa-fw">i><span>订阅本站span>a>li> ul> div>
注意:与上面 JavaScript 代码一样,如果你不需要判断用户身份以确认是否启用,也可以将代码直接复制到网站前端文件中。
WordPress 判断用户身份
如果你不需要下面的功能,基本上复制上面的代码到网站对应的文件中去即可以使用该功能。就像开篇提到的,为了该功能不影响自己后期对网站的调试,特加了个用户身份判断。按上方要求配置好 qgg-user-rcm.js 文件及 modify-user-right-click-menu.php 文件后,通过以下代码调用这两个文件即可。
if( !current_user_can( 'manage_options' ) ){ require_once get_stylesheet_directory() . '/modify/modify-user-right-click-menu.php'; ?>"text/javascript" src=class="hljs-string">"/js/qgg-user-rcm.js">script> } class="hljs-meta">?>
注意将代码中的路径替换为你真实存放文件的路径。
另外对于 DUX 及其他后台使用 Option FrameWork 框架的用户,如果你想在后台设置一个方便的开工启用或停用该功能,将以下代码丢到主题 options.php 文件中去:
class="ql-syntax" spellcheck="false">class="hljs-comment">// 右键菜单优化 $options[] = class="hljs-keyword">array( class="hljs-string">'name' => __(class="hljs-string">'全站右键菜单优化', class="hljs-string">'QGG'), class="hljs-string">'id' => class="hljs-string">'qgg_right_click_menu_open', class="hljs-string">'std' => class="hljs-keyword">true, class="hljs-string">'desc' => __(class="hljs-string">'开启', class="hljs-string">'QGG'), class="hljs-string">'type' => class="hljs-string">'checkbox');
然后,调用代码改为:
class="ql-syntax" spellcheck="false">class="hljs-keyword">if( !current_user_can( class="hljs-string">'manage_options' ) && QGG_options(class="hljs-string">'qgg_right_click_menu_open') ){ require_once get_stylesheet_directory() . ="hljs-string">'/diy/modify/modify-user-right-click-menu.php'; class="hljs-meta">?>class</span>=<span class="hljs-string">"hljs-string"</span>><span class="hljs-string">"text/javascript"</span></span> src=<span <span class="hljs-keyword">class</span>=<span class="hljs-string">"hljs-string"</span>><span class="hljs-string">"/diy/js/qgg-user-rcm.js"</span></span>>script> } <span <span class="hljs-keyword">class</span>=<span class="hljs-string">"hljs-meta"</span>>?></span> </pre> </pre>