Bookmarklet to Toggle Google Accounts

I use Google Apps at work and also have a personal Google account for many other things. Switching between these accounts can be annoying, as can dealing with clicking on share links that open in the wrong account.

To get around this, I put together this bookmarklet that switches between two accounts automatically. You’ll need to be logged into both accounts at once in order for this to work.

To make it work, drag this bookmark to your address bar.

<a href=”javascript:%28function %28%29 %7B %09var newUser %3D 1%3B %09var newURL%3B %09url %3D window.location.href%3B %09var match1 %3D url.match%28%2F%5C%2Fu%5C%2F%28%5Cd%29%5C%2F%2Fi%29%3B %09var match2 %3D url.match%28%2Fauthuser%3D%28%5Cd%29%2Fi%29%3B %09var match3 %3D url.match%28%2F%5C%2Fb%5C%2F%28%5Cd%29%5C%2F%2Fi%29%3B %09var match4 %3D url.match%28%2F%5Ehttps%3A%5C%2F%5C%2Fdocs%5C.google%5C.com%5C%2F%28%5B%5E%5C%2F%5D%2B%29%5C%2Fd%5C%2F%2Fi%29%3B %09if %28match1%29 %7B %09%09console.log%28%27match1%27%29%3B %09%09if %28match1%5B1%5D %3D%3D 1%29 %7B %09%09%09newUser %3D 0%3B %09%09%7D %09%09newURL %3D window.location.href.replace%28%2F%5C%2Fu%5C%2F%5Cd%5C%2F%2Fi%2C “%2Fu%2F” %2B newUser %2B “%2F”%29%3B %09%7D %09else if %28match2%29 %7B %09%09console.log%28%27match2%27%29%3B %09%09if %28match2%5B1%5D %3D%3D 1%29 %7B %09%09%09newUser %3D 0%3B %09%09%7D %09%09newURL %3D window.location.href.replace%28%2Fauthuser%3D%5Cd%2Fi%2C “authuser%3D” %2B newUser%29%3B %09%7D %09else if %28match3%29 %7B %09%09console.log%28%27match3%27%29%3B %09%09if %28match3%5B1%5D %3D%3D 1%29 %7B %09%09%09newUser %3D 0%3B %09%09%7D %09%09newURL %3D window.location.href.replace%28%2F%5C%2Fb%5C%2F%5Cd%5C%2F%2Fi%2C “%2Fb%2F” %2B newUser %2B “%2F”%29%3B %09%7D %09else %7B %09%09newURL %3D window.location.origin %2B window.location.pathname%3B %09%09if %28window.location.search%29 %7B %09%09%09newURL %2B%3D window.location.search %2B “%26authuser%3D” %2B newUser%3B %09%09%7D %09%09else %7B %09%09%09newURL %2B%3D “%3Fauthuser%3D” %2B newUser%3B %09%09%7D %09%09newURL %3D newURL %2B%3D window.location.hash%3B %09%7D %09window.location.href %3D newURL %7D %29%28%29”>Toggle Google Account</a>

I’ve only tested this in Chrome, but it ought to work in other browsers. Also, if you have three or more accounts, this will just switch between the first two.

Twitter, Facebook

Written on July 31, 2019