tor.js

Minimal JavaScript library to check if your visitors are TOR users

You are NOT using TOR

Visiting the NYTimes via the TOR browser it annoyed me, that they are not automatically redirecting me to their .onion service.

With this small library anyone could easily do that or provide other special functionalities for TOR users.

My server script generates the isTor global variable by checking the requesting IP against the TOR exit nodes list.

If you don't know that yet. The TOR browser is a privacy protecting web browser defending you against tracking and surveillance and helps to circumvent censorship.

Usage

Including the script

<script src="https://simon-frey.eu/torjs/tor.js"></script>

Available functions

isTor

There is a global boolean variable declared by the script telling you if the user is a TOR user
<script>
    if(isTor){
        alert("Hello World TOR user");
    }else{
        alert("Hello World normal user");
    }
</script>

redirectTOR(url)

The redirectTOR function redirects the vistor to the same page on the given (.onion) host if the visitor is a TOR user. With this function you may redirect the visitor to you .onion service on visit.
<script>
    redirectTOR("snaajk2n2n2n2nakjnasdf.onion");
</script>

executeForTOR(function)

As helper I added the executeForTOR function that takes a function as parameter. The given function will only be exectuted if the visitor is a TOR user.
<script>
    executeForTOR(function () {
        alert("Hello World TOR user");           
    });
</script>

executeForNotTOR(function)

Same as the executeForTOR, the executeForNotTOR funtion takes a function as parameter. The given function will only be exectuted if the visitor is NOT a TOR user.
<script>
    executeForTOR(function () {
        alert("Hello World normal user");
    });
</script>

This project is Public Domain and can be found on Github