鼠标中键点击页面关闭标签页

2022/6/27 15:32:38

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        鼠标中键点击页面关闭标签页
// @author      Arden
// @namespace   Violentmonkey Scripts
// @match        *://*/*
// @grant       none
// @version     1.0.0
// @author      Arden
// @description 2022/6/27 15:32:38
// @license     MIT
// ==/UserScript==
(function(){
  document.querySelector('html').addEventListener('mousedown', e => {
  // console.log(e.button);
  let mouseBtn = e.button
  if (mouseBtn == 1) {
    e.preventDefault(); //阻止中键滚动,参考:https://chrome.google.com/webstore/detail/no-auto-scroll/oeplmakppkomhkgoaieajhcbfogjhmdi/related
    e.stopPropagation() //阻止冒泡,好像没什么用
    
    // window.close()只能关自己打开的窗口,所以才这样写
    // 参考:https://blog.51cto.com/humorchen/3020014
    window.opener = null;
    window.open('', '_self', '');
    window.close();//以上三行可关闭单个页面
    window.open('', '_top');
    window.top.close();
    window.location.href = 'about:blank ';
    window.close();//上面两次关闭适用于FireFox等浏览器
  }
})
})()