Skip to content

Instantly share code, notes, and snippets.

@chrisdavidmiles
Created July 3, 2017 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdavidmiles/923b03766e02c2a1c077eb85672efb36 to your computer and use it in GitHub Desktop.
Save chrisdavidmiles/923b03766e02c2a1c077eb85672efb36 to your computer and use it in GitHub Desktop.
Detect Tor Browser users and redirect them to the .onion version of my site.
var myoniondomain = 'chrisdm326o2d7iq.onion';
var thecurrentdomain = window.location.hostname;
if (myoniondomain != thecurrentdomain) {
function isTorBrowser() {
var img = document.createElement('img');
img.src = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
var canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
var ctx = canvas.getContext('2d');
var imagedata = ctx.getImageData(0, 0, canvas.width, canvas.height);
return imagedata.data[0] == 255 && imagedata.data[1] == 255 && imagedata.data[2] == 255 && imagedata.data[3] == 255;
};
if (isTorBrowser()) {
if (myoniondomain != thecurrentdomain) {
var spinner = '<style>.loader{border:8px solid #ececec;border-radius:50%;border-top:8px solid #607d8b;width:60px;height:60px;-webkit-animation:spin 2s linear infinite;animation:spin 1s linear infinite;position: absolute; left: 50%; top: calc(50vh - 46px); margin: -30px 0 0 -30px;}@-webkit-keyframes spin{0%{-webkit-transform:rotate(0)}100%{-webkit-transform:rotate(360deg)}}@keyframes spin{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}</style><div class="loader-container"><div class="loader"></div></div>';
document.getElementsByTagName('html') [0].innerHTML = spinner;
window.location = window.location.href.replace('https://chrisdavidmiles.com', 'http://chrisdm326o2d7iq.onion');
}
}
}
@chrisdavidmiles
Copy link
Author

chrisdavidmiles commented Jul 5, 2017

EDIT:

Don't use this code. I wrote it, and used it for a time, but this no longer works in most browsers that work with the tor network. Instead, use the Onion-Location HTTP response header. Websites can include this response header and have it include a link to the Onion site.

@da2x
Copy link

da2x commented May 10, 2021

Anyone stumbling across this today should instead use the Onion-Location HTTP response header to redirect traffic to their Onion site.

@chrisdavidmiles
Copy link
Author

Yes. I'll update my comment here and my old tor-redirect.js repo with information about Onion-Location header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment