规范格式

This commit is contained in:
example 2019-02-14 19:04:02 +08:00
parent 4b319d8715
commit 830ae3ade5

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name Translate // @name Translate
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 4.3 // @version 4.4
// @description 划词翻译调用“金山词霸、有道词典有道翻译、Google Translate谷歌翻译、沪江小D、搜狗翻译、必应词典必应翻译、Microsoft Translator必应在线翻译、海词词典、百度翻译、Oxford Learner's Dictionaries、Oxford Dictionaries、Merriam-Webster、汉典、PDF 划词翻译”网页翻译 // @description 划词翻译调用“金山词霸、有道词典有道翻译、Google Translate谷歌翻译、沪江小D、搜狗翻译、必应词典必应翻译、Microsoft Translator必应在线翻译、海词词典、百度翻译、Oxford Learner's Dictionaries、Oxford Dictionaries、Merriam-Webster、汉典、PDF 划词翻译”网页翻译
// @author https://github.com/barrer // @author https://github.com/barrer
// @match http://*/* // @match http://*/*
@ -379,14 +379,16 @@
img.setAttribute('title', obj.name); img.setAttribute('title', obj.name);
img.addEventListener('mouseup', function () { img.addEventListener('mouseup', function () {
if (iconDrag.elementOriginalLeft == myParseInt(icon.style.left) && if (iconDrag.elementOriginalLeft == myParseInt(icon.style.left) &&
iconDrag.elementOriginalTop == myParseInt(icon.style.top)) // 没有拖动鼠标抬起的时候触发点击事件 iconDrag.elementOriginalTop == myParseInt(icon.style.top)) { // 没有拖动鼠标抬起的时候触发点击事件
dataTransfer.beforePopup(obj.popup); dataTransfer.beforePopup(obj.popup);
}
}); });
if (isIconImgMore) { if (isIconImgMore) {
img.setAttribute('is-more', 'true'); img.setAttribute('is-more', 'true');
} }
if (obj.id == 'more') if (obj.id == 'more') {
isIconImgMore = true; isIconImgMore = true;
}
icon.appendChild(img); icon.appendChild(img);
}); });
// 翻译图标添加到 DOM // 翻译图标添加到 DOM
@ -465,10 +467,11 @@
var isMore = gm.get(gm.MORE, false); var isMore = gm.get(gm.MORE, false);
log('isMore: ' + isMore); log('isMore: ' + isMore);
icon.querySelectorAll('img[is-more]').forEach(function (ele) { icon.querySelectorAll('img[is-more]').forEach(function (ele) {
if (isMore) if (isMore) {
ele.style.display = 'inline-block'; ele.style.display = 'inline-block';
else } else {
ele.style.display = 'none'; ele.style.display = 'none';
}
}); });
// 兼容部分 Content Security Policy // 兼容部分 Content Security Policy
icon.style.position = 'absolute'; icon.style.position = 'absolute';
@ -482,8 +485,9 @@
/**日志输出*/ /**日志输出*/
function log() { function log() {
var debug = false; var debug = false;
if (!debug) if (!debug) {
return; return;
}
if (arguments) { if (arguments) {
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]); console.log(arguments[i]);
@ -501,8 +505,9 @@
} }
/**数组移动*/ /**数组移动*/
function arrayMove(arr, oldIndex, newIndex) { function arrayMove(arr, oldIndex, newIndex) {
if (oldIndex < 0 || oldIndex >= arr.length || newIndex < 0 || newIndex >= arr.length) if (oldIndex < 0 || oldIndex >= arr.length || newIndex < 0 || newIndex >= arr.length) {
return arr; return arr;
}
if (newIndex >= arr.length) { if (newIndex >= arr.length) {
var k = newIndex - arr.length + 1; var k = newIndex - arr.length + 1;
while (k--) { while (k--) {
@ -525,12 +530,14 @@
function escExit(e) { function escExit(e) {
e = e || window.event; e = e || window.event;
var isEscape = false; var isEscape = false;
if ("key" in e) if ("key" in e) {
isEscape = (e.key == "Escape" || e.key == "Esc"); isEscape = (e.key == "Escape" || e.key == "Esc");
else } else {
isEscape = (e.keyCode == 27); isEscape = (e.keyCode == 27);
if (isEscape) }
if (isEscape) {
window.close(); window.close();
}
} }
/**触发事件*/ /**触发事件*/
function tiggerEvent(el, type) { function tiggerEvent(el, type) {
@ -605,8 +612,9 @@
}; };
this.dragElement = function (e) { this.dragElement = function (e) {
log('dragging'); log('dragging');
if (!ref.dragging) if (!ref.dragging) {
return; return;
}
e.preventDefault(); e.preventDefault();
// move element // move element
element.style.left = ref.elementOriginalLeft + (e.clientX - ref.mouseDownPositionX) + 'px'; element.style.left = ref.elementOriginalLeft + (e.clientX - ref.mouseDownPositionX) + 'px';
@ -701,8 +709,9 @@
show.setAttribute('href', 'javascript:void(0)'); show.setAttribute('href', 'javascript:void(0)');
show.addEventListener('click', function () { show.addEventListener('click', function () {
if (this.innerHTML == 'show') { // 隐藏 if (this.innerHTML == 'show') { // 隐藏
if (this.getAttribute('show-id') != 'settings') if (this.getAttribute('show-id') != 'settings') {
hideConfig[this.getAttribute('show-id')] = true; hideConfig[this.getAttribute('show-id')] = true;
}
} else { // 显示 } else { // 显示
delete hideConfig[this.getAttribute('show-id')]; delete hideConfig[this.getAttribute('show-id')];
} }
@ -729,10 +738,11 @@
var tempArray = []; var tempArray = [];
// hide // hide
iconArray.forEach(function (obj) { iconArray.forEach(function (obj) {
if (hide && !isNotNull(hideConfig[obj.id])) if (hide && !isNotNull(hideConfig[obj.id])) {
tempArray.push(obj); tempArray.push(obj);
else if (!hide) } else if (!hide) {
tempArray.push(obj); tempArray.push(obj);
}
}); });
// sort // sort
var sorted = {}; var sorted = {};