Greasy Fork is available in English.
选中词语点击鼠标中键,就可以在页面内快速搜索小鸡词典
// ==UserScript== // @name 小鸡词典快查 // @namespace https://greasyfork.org/zh-CN/scripts/400962 // @version 0.1 // @description 选中词语点击鼠标中键,就可以在页面内快速搜索小鸡词典 // @author miguko // @match *://weibo.com/* // @match *://*.weibo.com/* // @match *://*.weibo.cn/* // @match *://tieba.baidu.com/* // @match *://*.acfun.cn/ // @match *://*.acfun.cn/* // @match *://*.bilibili.com/ // @match *://*.bilibili.com/* // @match *://*.douban.com/group/* // @match *://douban.com/group/* // @match *://adnmb2.com/* // @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js // @grant none // @license MIT License // ==/UserScript== /* 使用方式:在匹配的页面选中不明白的网络词语,并用鼠标中键点击那个词语,就可以调出小鸡词典(https://jikipedia.com/)查看这个词语的含义和来历。 如果同时安装了"能不能好好说话"脚本(https://lab.magiconch.com/nbnhhsh/nbnhhsh.user.js),还可以直接点击翻译出来的结果进行查询。 */ (function() { 'use strict'; // Your code here... const CHICK_DIC_URL="https://jikipedia.com/search?phrase="; chick_Appearance_Load(); window.addEventListener('mousedown', Chick_Check);//为页面增加鼠标点击的监听 window.addEventListener('mousedown', Chick_Nbnhhsh_Listener);//为页面增加鼠标点击的监听 function Chick_Get_Short_Text() { let text = getSelection().toString().trim(); text = text.replace(/[\r\n\:\@\#]/g, "");//去掉回车,换行符,@和#(微博话题用得上) if (!!text && text.length < 20) { return text; } else { //console.log("饲料的量小鸡顶不住啊(查找关键词过短或大于20个字符)"); return null; } } function Chick_Bye() { if ($("#chickCoop").length > 0) { $("#chickCoop").css("transition-duration", "0s"); $("#chickCoop").slideUp(500, function () { $("#chickCoop").remove(); }); } } function Chick_Check(event) {//通过检查是否是鼠标中键点击在文字选中区域决定是否召唤小鸡 let e = event || window.event;//兼容性 if (event.button == 1) {//选中了鼠标中键 if (Chick_Get_Short_Text()) { let mouseX = e.clientX;//鼠标点击位置的左距离 let mouseY = e.clientY;//鼠标点击位置的上距离 let rect = getSelection().getRangeAt(0).getBoundingClientRect();//获得选中文字的最小包围区 if (mouseX >= (rect.left - 5) && mouseX <= (rect.left + rect.width + 5) && mouseY >= (rect.top - 5) && mouseY <= (rect.top + rect.height + 5)) { e.preventDefault();//防止默认操作取消了文字的选择 chick_Load(Chick_Get_Short_Text());//孵化小鸡 } } } else { Chick_Bye();//如果不是在查询的话,就和小鸡说再见 } } function Chick_Nbnhhsh_Listener() { //能不能好好说话脚本主页:https://lab.magiconch.com/nbnhhsh if ($(".nbnhhsh-box").length > 0) {//如果有安装缩写转换的话,给每个匹配的解释增加点击查询功能 $(".nbnhhsh-tran-item").off('click').on("click", function () { chick_Load(this.innerHTML) });//先解绑点击时间防止多次点击导致的多次绑定 } } function chick__Locate() { let rect = getSelection().getRangeAt(0).getBoundingClientRect();//获得选中文字的最小包围区 let winHight=$(window).height();//获得窗口的高度 let scrollTop=$(document).scrollTop();//获取当前移动的高度 let pointBefore={x:0,y:0};//创建坐标点 if(winHight-rect.bottom>=500){//如果选中文字底部塞得下小鸡的话 pointBefore.y=rect.bottom+scrollTop; pointBefore.x=($(".nbnhhsh-box").length > 0)?rect.left+220:rect.left; }else if(rect.top>500){//如果上面挤的下小鸡的话 pointBefore.y=rect.top-510+scrollTop; pointBefore.x=rect.left; }else{//都没位置,只能凑合下了 pointBefore.y=scrollTop+winHight-550; pointBefore.x=($(".nbnhhsh-box").length > 0)?rect.left+220:rect.left; } return pointBefore; } function chick_Appearance_Load(){//加载小鸡的外表 let chickCssTemplate='<style>#chickCoop{display: block;\ width:300px;\ height:150px;\ background-color: antiquewhite;\ border:5px solid antiquewhite;\ border-radius: 5px;\ box-shadow: 5px 3px 5px grey;\ transition-duration: 0.5s;\ overflow:hidden;\ text-align: center;\ z-index: 99;\ position: absolute;\ }\ #chickStrawMat{\ border:0px;\ height:500px;\ display: none;\ width: 317px;\ overflow: auto;\ }</style>' let $chickFeather=$(chickCssTemplate); $("head").append($chickFeather); } function chick_Load(word) {//加载小鸡 //chickWord=word;//全局保存当前的关键词,用于之后切换萌百和百度时使用 if($("#chickLoading").length > 0){ return null; }else{ let chickTemplate = '<div id="chickCoop">\ <div id="chickLoading">\ <p>小鸡努力中...</p>\ <img src="https://jikipedia.com/images/loading/loading_chicken.gif"></img>\ </div>\ <iframe id="chickStrawMat" seamless></iframe>\ </div>';//鸡笼的模板 let $chickCoop = $(chickTemplate);//解析成jquery对象 let pointBefore=chick__Locate(); $chickCoop.css("top",pointBefore.y); $chickCoop.css("left",pointBefore.x); $("body").append($chickCoop);//添加到页面 let chickIframe = document.getElementById("chickStrawMat");//获取鸡笼的dom对象 chickIframe.src = CHICK_DIC_URL + word;//设置稻草垫子的内容 setTimeout(function () { $("#chickLoading").slideUp("fast"); $("#chickCoop").css("height", "500px"); $("#chickStrawMat").css("display", "block"); }, 1000);//因为加载很快但是想让大家看看小鸡所以强行加个延时 } } })();