Last active
May 5, 2021 20:19
-
-
Save DV8FromTheWorld/326b9ba0be674095df4cf09e241d090e to your computer and use it in GitHub Desktop.
Switch Google Account - Bookmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(() => { | |
/** | |
* User format where the user index is defined as part of the path as '/u/:userIndex/' | |
* This format is seen in google calendar. | |
* Example: https://calendar.google.com/calendar/u/1/r | |
**/ | |
const formatUserInPath = { | |
getIndex() { return this._regex.exec(location.href)?.[1]; }, | |
replace(index) { location.href = location.href.replace(this._regex, `/u/${index}/`); }, | |
_regex: /\/u\/(.*?)\// | |
}; | |
/** | |
* User format where the user index is defined as a query parameter as '?authuser=:userIndex' | |
* This format works for most other google products. | |
* Example: https://meet.google.com/g1z-ycan-ugs?authuser=1 | |
*/ | |
/** ?authUser=:userId **/ | |
const formatUserInQueryParam = { | |
getIndex() { | |
const params = this._getParams(); | |
return params.get('authuser') || "0"; | |
}, | |
replace(index) { | |
const params = this._getParams(); | |
params.set('authuser', index); | |
location.search = params.toString(); | |
}, | |
_getParams() { return new URLSearchParams(location.search); } | |
}; | |
const isGoogleSite = location.href.includes('.google.com'); | |
if (!isGoogleSite) { | |
alert("Not on a google site"); | |
return; | |
} | |
/** Order here is important because 'formatUserInQueryParam' will return "0" even if the parameter is not being used. */ | |
const accountFormats = [ | |
formatUserInPath, | |
formatUserInQueryParam, | |
] | |
.filter(format => format.getIndex()); | |
const accountFormat = accountFormats[0]; | |
const currentAccount = accountFormat.getIndex(); | |
const nextAccount = currentAccount === "0" ? "1" : "0"; | |
accountFormat.replace(nextAccount); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment