Hide Referrer Using Iframe
by skibulk
HTML
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<a href="https://via.placeholder.com/350x150">Test Link</a>
JavaScript
/*
NOTES:
This code is a proof of concept only. Navigation away from the parent page is cancelled and the target url is messaged to an iframe. The iframe loads a dara url, which counts as a "null" origin document. When the frame receives the message, it redirects the user to the target url with a "null" referrer. Since the frame has a null origin, it cannot be messaged directly. As a result, another web page could potentially intercept the message via their own anonymous iframe. In production, you should still use rel="noreferrer" on your links, in case your users have disabled javascript, or a javascript error occurs on your page. In the case of old browsers with JS disabled, the referrer could still be exposed. This example may only be loaded after the body of the web page, so any clicks before the page has fully loaded may not be processed by the script.
*/
document.write('<iframe\
id="anonymizer"\
sandbox="allow-scripts allow-top-navigation"\
src="data:text/html;charset=utf-8,<scr\ipt>console.log(\'Hello World\'); window.addEventListener(\'message\', function(event){ if(event.origin == \'' + window.origin + '\') top.window.location = event.data; });"\
style="displayyy: none !important;">\
</iframe>');
var links = document.getElementsByTagName("a");
// var links = document.querySelectorAll("a[rel~=noreferrer]");
for( var i = 0; i < links.length; i++ )
{
links[i].onclick = function(event)
{
console.log( document.getElementById("anonymizer").contentWindow );
document.getElementById("anonymizer").contentWindow.postMessage( event.target.href, '*' );
return false;
};
}