No Wiki Wandering

Prevent Wikipedia wandering. Show a dialog when you try to visit a new page, and the action is prevented. No wandering, more productivity!

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        No Wiki Wandering
// @name:zh-CN  阻止维基漫游
// @namespace   hghwng
// @version     1
// @grant       none
// @include     *.wikipedia.org/wiki/*
// @description Prevent Wikipedia wandering. Show a dialog when you try to visit a new page, and the action is prevented. No wandering, more productivity!
// @description:zh-CN 防止维基百科漫游。在尝试访问新页面时弹框报警,并阻止加载。离开漫游,拥抱效率!
// ==/UserScript==

(function () {
  var startTime = Date.now();
  
  document.addEventListener('click', function (e) {
    var target = e.target;
    while (target) {
      if (target.tagName == 'A') break;
      target = target.parentNode;
    }
    if (target.tagName != 'A') return;
    
    var targetUrl = target.href;
    var currentUrl = window.location.origin + window.location.pathname;
    if (targetUrl.startsWith(currentUrl)) {
      if (targetUrl.length <= currentUrl.length) return;
      if (target.href[currentUrl.length] == '#') return;
    }
    
    var diff = (Date.now() - startTime) / 1000;
    alert("You have wasted " + diff.toFixed() + " seconds on Wikipedia!");
    e.preventDefault();
  })
})()