调整窗口位置计算规则,更好的兼容各种网页布局;host 节点样式覆盖步骤调整;图标选中 JS 选择器 bug 修正

This commit is contained in:
example 2019-03-28 18:09:57 +08:00
parent ba44ead52d
commit 96da12e18e

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name 划词翻译:多词典查询 // @name 划词翻译:多词典查询
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 3.6 // @version 3.7
// @description 划词翻译调用“有道词典有道翻译、金山词霸、Bing 词典必应词典、剑桥高阶、沪江小D、谷歌翻译” // @description 划词翻译调用“有道词典有道翻译、金山词霸、Bing 词典必应词典、剑桥高阶、沪江小D、谷歌翻译”
// @author https://github.com/barrer // @author https://github.com/barrer
// @match http://*/* // @match http://*/*
@ -26,6 +26,7 @@
var style = document.createElement('style'); var style = document.createElement('style');
style.textContent = ` style.textContent = `
/*组件样式*/ /*组件样式*/
:host{all:unset!important}
:host{all:initial!important} :host{all:initial!important}
*{word-wrap:break-word!important;word-break:break-word!important} *{word-wrap:break-word!important;word-break:break-word!important}
a{color:#36f;text-decoration:none;cursor:pointer} 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 scrollTop = Math.max(parseInt(document.documentElement.scrollTop), parseInt(document.body.scrollTop));
var scrollLeft = Math.max(parseInt(document.documentElement.scrollLeft), parseInt(document.body.scrollLeft)); 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) { if (parseInt(icon.style.top) < scrollTop) {
log('Y adjust top'); log('Y adjust top');
icon.style.top = scrollTop + 'px'; iconNewTop = scrollTop;
} else if (parseInt(icon.style.top) + panelHeight > scrollTop + document.documentElement.clientHeight) { } else if (parseInt(icon.style.top) + panelHeight > scrollTop + clientHeight) {
log('Y adjust bottom'); 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) { if (parseInt(icon.style.left) < scrollLeft) {
log('X adjust left'); log('X adjust left');
icon.style.left = scrollLeft + 'px'; iconNewLeft = scrollLeft;
} else if (parseInt(icon.style.left) + panelWidth > scrollLeft + document.documentElement.clientWidth) { } else if (parseInt(icon.style.left) + panelWidth > scrollLeft + clientWidth) {
log('X adjust right'); 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'; content.style.display = 'block';
} }
@ -715,7 +736,7 @@
/**显示翻译引擎指示器*/ /**显示翻译引擎指示器*/
function engineActivateShow() { function engineActivateShow() {
engineActivateHide(); engineActivateHide();
icon.querySelector('img[icon-id="' + engineId + '"').setAttribute('activate', 'activate'); icon.querySelector('img[icon-id="' + engineId + '"]').setAttribute('activate', 'activate');
} }
/**隐藏 icon*/ /**隐藏 icon*/
function hideIcon() { function hideIcon() {