Created
April 12, 2022 09:32
-
-
Save shapeshifta78/978354f9dc8ea513a08c1b6cb9140db1 to your computer and use it in GitHub Desktop.
Create tabid
This file contains 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
import uuidv4 from "uuid/v4"; | |
class CurrentTab { | |
/** | |
* create a new tabid or grab it from storage | |
* | |
* @constructor | |
*/ | |
constructor() { | |
this.debug = DEVELOPMENT; | |
// check for cloned tabs | |
if (CurrentTab.sameTabCheck() === false) { | |
this.clonedTab = true; | |
} | |
try { | |
if ( | |
sessionStorage.tabId && | |
CurrentTab.sameTabCheck() !== false && | |
!this.clonedTab | |
) { | |
this.tabId = sessionStorage.tabId; | |
} else { | |
this.tabId = uuidv4(); | |
sessionStorage.tabId = this.tabId; | |
window.name = this.tabId; | |
} | |
} catch (e) { | |
this.tabId = uuidv4(); | |
} | |
if (this.debug) { | |
/* eslint-disable-next-line no-console */ | |
console.debug(`Tabid is ${this.tabId}`); | |
} | |
} | |
/** | |
* return current tabId | |
* | |
* @returns {string} tabid - uuid v4 | |
*/ | |
getId() { | |
return this.tabId; | |
} | |
/** | |
* check storage if tab was cloned | |
* | |
* @returns {boolean} | |
*/ | |
static sameTabCheck() { | |
try { | |
return window.name === sessionStorage.tabId; | |
} catch (e) { | |
return false; | |
} | |
} | |
} | |
export let currentTab = new CurrentTab(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment