Skip to content

Instantly share code, notes, and snippets.

@zhanhongtao
Last active October 24, 2017 09:50
Show Gist options
  • Save zhanhongtao/9dd492aa72281976d08f99835a918a57 to your computer and use it in GitHub Desktop.
Save zhanhongtao/9dd492aa72281976d08f99835a918a57 to your computer and use it in GitHub Desktop.
fixed.rewrite
// ==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