From 96da12e18ed75dc2cab2f47fede8e895d3e35abc Mon Sep 17 00:00:00 2001 From: example Date: Thu, 28 Mar 2019 18:09:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=AA=97=E5=8F=A3=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E8=AE=A1=E7=AE=97=E8=A7=84=E5=88=99=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E5=A5=BD=E7=9A=84=E5=85=BC=E5=AE=B9=E5=90=84=E7=A7=8D=E7=BD=91?= =?UTF-8?q?=E9=A1=B5=E5=B8=83=E5=B1=80=EF=BC=9Bhost=20=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E8=A6=86=E7=9B=96=E6=AD=A5=E9=AA=A4=E8=B0=83?= =?UTF-8?q?=E6=95=B4=EF=BC=9B=E5=9B=BE=E6=A0=87=E9=80=89=E4=B8=AD=20JS=20?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=99=A8=20bug=20=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- translate/translate-dictionary.js | 37 ++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/translate/translate-dictionary.js b/translate/translate-dictionary.js index 2e27d59..77f16ae 100644 --- a/translate/translate-dictionary.js +++ b/translate/translate-dictionary.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 划词翻译:多词典查询 // @namespace http://tampermonkey.net/ -// @version 3.6 +// @version 3.7 // @description 划词翻译调用“有道词典(有道翻译)、金山词霸、Bing 词典(必应词典)、剑桥高阶、沪江小D、谷歌翻译” // @author https://github.com/barrer // @match http://*/* @@ -26,6 +26,7 @@ var style = document.createElement('style'); style.textContent = ` /*组件样式*/ + :host{all:unset!important} :host{all:initial!important} *{word-wrap:break-word!important;word-break:break-word!important} a{color:#36f;text-decoration:none;cursor:pointer} @@ -628,19 +629,39 @@ ); var scrollTop = Math.max(parseInt(document.documentElement.scrollTop), parseInt(document.body.scrollTop)); var scrollLeft = Math.max(parseInt(document.documentElement.scrollLeft), parseInt(document.body.scrollLeft)); + var clientHeight = [parseInt(document.documentElement.clientHeight), parseInt(document.body.clientHeight)].filter(function (x) { + return x <= parseInt(window.innerHeight); + }).sort(function (a, b) { + return a > b ? -1 : (a == b ? 0 : 1); + })[0]; // 找出最大值且小于等于 window 的高度 + var clientWidth = [parseInt(document.documentElement.clientWidth), parseInt(document.body.clientWidth)].filter(function (x) { + return x <= parseInt(window.innerWidth); + }).sort(function (a, b) { + return a > b ? -1 : (a == b ? 0 : 1); + })[0]; // 找出最大值且小于等于 window 的宽度 + var iconNewTop = -1; if (parseInt(icon.style.top) < scrollTop) { log('Y adjust top'); - icon.style.top = scrollTop + 'px'; - } else if (parseInt(icon.style.top) + panelHeight > scrollTop + document.documentElement.clientHeight) { + iconNewTop = scrollTop; + } else if (parseInt(icon.style.top) + panelHeight > scrollTop + clientHeight) { log('Y adjust bottom'); - icon.style.top = parseInt(scrollTop + document.documentElement.clientHeight - panelHeight) + 'px'; + iconNewTop = parseInt(scrollTop + clientHeight - panelHeight); } + if (iconNewTop != -1 && Math.abs(iconNewTop - parseInt(icon.style.top)) <= panelHeight) { + log('Y set iconNewTop', iconNewTop); + icon.style.top = iconNewTop + 'px'; + } + var iconNewLeft = -1; if (parseInt(icon.style.left) < scrollLeft) { log('X adjust left'); - icon.style.left = scrollLeft + 'px'; - } else if (parseInt(icon.style.left) + panelWidth > scrollLeft + document.documentElement.clientWidth) { + iconNewLeft = scrollLeft; + } else if (parseInt(icon.style.left) + panelWidth > scrollLeft + clientWidth) { log('X adjust right'); - icon.style.left = parseInt(scrollLeft + document.documentElement.clientWidth - panelWidth) + 'px'; + iconNewLeft = parseInt(scrollLeft + clientWidth - panelWidth); + } + if (iconNewLeft != -1 && Math.abs(iconNewLeft - parseInt(icon.style.left)) <= panelWidth) { + log('X set iconNewLeft', iconNewLeft); + icon.style.left = iconNewLeft + 'px'; } content.style.display = 'block'; } @@ -715,7 +736,7 @@ /**显示翻译引擎指示器*/ function engineActivateShow() { engineActivateHide(); - icon.querySelector('img[icon-id="' + engineId + '"').setAttribute('activate', 'activate'); + icon.querySelector('img[icon-id="' + engineId + '"]').setAttribute('activate', 'activate'); } /**隐藏 icon*/ function hideIcon() {