Last active
October 24, 2017 09:50
-
-
Save zhanhongtao/9dd492aa72281976d08f99835a918a57 to your computer and use it in GitHub Desktop.
fixed.rewrite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name x181-fixed-zhihu-rewrite | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author tt | |
// @match https://*.zhihu.com/* | |
// @match https://www.google.com/* | |
// @match https://www.google.com.hk/* | |
// @match https://www.so.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function walk(node, handle, context) { | |
while (node) { | |
if (handle(node) === true) { | |
return node; | |
} | |
if (node === context) { | |
break; | |
} else { | |
node = node.parentNode; | |
} | |
} | |
return null; | |
} | |
function handle(href, node) { | |
var link; | |
href.replace(/(?:\?|&)(?:target|url)=([^&]*)/i, function(m, $1) { | |
link = decodeURIComponent($1); | |
}); | |
if (link) { | |
node.target = '_blank'; | |
node.href = link; | |
} | |
} | |
document.addEventListener('mousedown', function(e) { | |
var target = e.target; | |
var node = walk(e.target, function(node) { | |
if (node.nodeName == 'A') { | |
return true; | |
} | |
}); | |
if (node) { | |
handle(node.href, node); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment