Skip to content

Instantly share code, notes, and snippets.

@hghwng
Last active April 16, 2016 12:21
Show Gist options
  • Save hghwng/08ad06559fd59906603677005abc9deb to your computer and use it in GitHub Desktop.
Save hghwng/08ad06559fd59906603677005abc9deb to your computer and use it in GitHub Desktop.
No Wiki Wandering
// ==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();
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment