我找到了一个适用于 Chrome 和 Firefox 的解决方案。我已经在用户脚本中实现了代码,不要跟踪我谷歌.
演示(在 Firefox 9 和 Chrome 17 中测试):http://jsfiddle.net/RxHw5/
Webkit(Chrome,..)和 Firefox 37+ (33+*) 的引荐来源网址隐藏
Webkit-based browsers (such as Chrome, Safari) support <a rel="noreferrer">
spec.
Referrer hiding can fully be implemented by combining this method with two event listeners:
-
mousedown
- 单击、中键单击、右键单击上下文菜单,...
-
keydown
(Tab Tab Tab ... Enter).
Code:
function hideRefer(e) {
var a = e.target;
// The following line is used to deal with nested elements,
// such as: <a href="."> Stack <em>Overflow</em> </a>.
if (a && a.tagName !== 'A') a = a.parentNode;
if (a && a.tagName === 'A') {
a.rel = 'noreferrer';
}
}
window.addEventListener('mousedown', hideRefer, true);
window.addEventListener('keydown', hideRefer, true);
* rel=noreferrer
is supported in Firefox since 33, but support was limited to in-page links. Referrers were still sent when the user opened the tab via the context menu. This bug was fixed in Firefox 37 [bug 1031264].
旧版 Firefox 的引荐来源网址隐藏
Firefox did not support rel="noreferrer"
until version 33 `[bug 530396] (or 37, if you wish to hide the referrer for context menus as well).
数据-URI +<meta http-equiv=refresh>
可用于隐藏 Firefox(和 IE)中的引荐来源网址。实现这个功能比较复杂,也需要两个事件:
-
click
- On click, on middle-click, Enter
-
contextmenu
- On right-click, Tab Tab ... Contextmenu
In Firefox, the click
event is fired for each mouseup
and hitting Enter on a link (or form control). The contextmenu
event is required, because the click
event fires too late for this case.
基于数据 URI 和瞬间超时:
当。。。的时候click
事件被触发时,href
属性暂时替换为 data-URI。事件完成,并且发生默认行为:打开 data-URI,取决于target
属性和 SHIFT/CTRL 修饰符。
与此同时,href
属性恢复到原来的状态。
当。。。的时候contextmenu
事件被触发时,链接也会瞬间发生变化。
- The
Open Link in ...
选项将打开数据 URI。
- The
Copy Link location
选项指的是恢复后的原始 URI。
- ☹ The
Bookmark
选项指的是 data-URI。
- ☹
Save Link as
指向 data-URI。
Code:
// Create a data-URI, redirection by <meta http-equiv=refresh content="0;url=..">
function doNotTrack(url) {
// As short as possible. " can potentially break the <meta content> attribute,
// # breaks the data-URI. So, escape both characters.
var url = url.replace(/"/g,'%22').replace(/#/g,'%23');
// In case the server does not respond, or if one wants to bookmark the page,
// also include an anchor. Strictly, only <meta ... > is needed.
url = '<title>Redirect</title>'
+ '<a href="' +url+ '" style="color:blue">' +url+ '</a>'
+ '<meta http-equiv=refresh content="0;url=' +url+ '">';
return 'data:text/html,' + url;
}
function hideRefer(e) {
var a = e.target;
if (a && a.tagName !== 'A') a = a.parentNode;
if (a && a.tagName === 'A') {
if (e.type == 'contextmenu' || e.button < 2) {
var realHref = a.href; // Remember original URI
// Replaces href attribute with data-URI
a.href = doNotTrack(a.href);
// Restore the URI, as soon as possible
setTimeout(function() {a.href = realHref;}, 4);
}
}
}
document.addEventListener('click', hideRefer, true);
document.addEventListener('contextmenu', hideRefer, true);
结合两种方法
不幸的是,没有直接的方法来检测此功能(更不用说考虑错误了)。所以你可以根据选择相关的代码navigator.userAgent
(即 UA 嗅探),或使用其中一种复杂的检测方法如何检测 rel="noreferrer" 支持?.