通过鼠标右键,按下后向左右上下移动,分别实现的后退、前进、关闭、刷新当前标签页的四大功能
当前为
// ==UserScript==
// @name RightClickShortcutKey
// @namespace http://tampermonkey.net/
// @version 0.1.9
// @description 通过鼠标右键,按下后向左右上下移动,分别实现的后退、前进、关闭、刷新当前标签页的四大功能
// @author Enjoy
// @icon https://foruda.gitee.com/avatar/1671100286067517749/4867929_enjoy_li_1671100285.png!avatar60
// @match *://*/*
// @exclude *hrwork*
// @exclude *zhaopinyun*
// @grant GM_addElement
// @grant GM_addStyle
// @grant GM_setClipboard
// @license GPL License
// ==/UserScript==
// 新页面打开链接
GM_addStyle('a>*{pointer-events: none;}')
window.addEventListener('mousedown',(e) => {
let dom = e.target
if (dom.tagName === "A") {
dom.removeAttribute('rel')
dom.setAttribute('target',dom.innerText.replace(/\s/g,''))
}
})
let ops = { x: 0,y: 0 }
let winSize = { width: 1,height: 1 }
// 右击操作
window.addEventListener('mousedown',function onMousedown(e) {
if (e.button !== 2) return
winSize = { width: window.innerWidth,height: window.innerHeight }
ops = { x: e.clientX,y: e.clientY }
})
// 右击操作
window.addEventListener('mouseup',function onMousedown(e) {
if (e.button !== 2) return
let offsetX = e.clientX - ops.x
let offsetY = e.clientY - ops.y
if (Math.abs(offsetX) >= 10 || Math.abs(offsetY) >= 10) {
if (offsetX < 0) {
//X轴左方
if (Math.abs(offsetY / offsetX) < 1) {
console.log('向左,返回上一页')
window.history.back()
} else if (offsetY > 0 && Math.abs(offsetY / offsetX) > 1) {
console.log('向下,刷新当前页')
location.reload()
} else if (offsetY < 0 && Math.abs(offsetY / offsetX) > 1) {
console.log('向上,关闭当前页')
// openCurrentOpener(window)
window.close()
}
} else if (offsetX >= 0) {
//X轴右方
if (Math.abs(offsetY / offsetX) < 1) {
console.log('向右,前进上一页')
window.history.forward()
} else if (offsetY > 0 && Math.abs(offsetY / offsetX) > 1) {
console.log('向下,刷新当前页')
location.reload()
} else if (offsetY < 0 && Math.abs(offsetY / offsetX) > 1) {
console.log('向上,关闭当前标签页')
// openCurrentOpener(window)
window.close()
}
}
}
})
function openCurrentOpener(window) {
let opener = window.opener
if (!opener) return
window.open(opener.location.href,opener.name)
}