Skip to content

Instantly share code, notes, and snippets.

@chrdek
Last active November 22, 2022 21:07
Show Gist options
  • Save chrdek/1f0195dc64d6a479e84ad2221b25410e to your computer and use it in GitHub Desktop.
Save chrdek/1f0195dc64d6a479e84ad2221b25410e to your computer and use it in GitHub Desktop.
Track/ Enumerate tabs in web browser session
console.clear();document.removeEventListener('visibilitychange',function(){});
document.addEventListener( 'visibilitychange' , function() {
if (document.URL.includes(document.URL.substr("https://stackoverflow.com"))) {
console.log('%cDeveloping . . . 👨‍💻', 'color:#bdecb6; background:#333c43; border-radius:15px; border:5px; border-color:lightgreen; text-shadow:2px; padding:10px');
}
if(document.URL != "" && document.URL.substr("https://stackoverflow") != "https://stackoverflow.com") {
console.log('%cJust Browsing . . 🌐', 'color:#58777d; font-size:0.98rem; font-weight:bold; font-family:Arial; background:#acbdd3; border-radius:50%;box-shadow: 10px 5px 5px #333c43; text-shadow:10px; padding:65px');
}
}, false );
//Add, Subtract a number for every tab that is open in browser.
function incrementTabsOpen() {
let tabsOpen = readObjCookie('tabsOpen') || {};
if (tabsOpen[window.location.href]) tabsOpen[window.location.href]++;
else tabsOpen[window.location.href] = 0;
writeObjCookie('tabsOpen', tabsOpen);
}
function decrementTabsOpen() {
let tabsOpen = readObjCookie('tabsOpen') || {};
if (tabsOpen[window.location.href]) tabsOpen[window.location.href]--;
if (tabsOpen[window.location.href] === 0) delete tabsOpen[window.location.href];
writeObjCookie('tabsOpen', tabsOpen);
}
function readObjCookie(name) {
let result = document.cookie.match(new RegExp(name + '=([^;]+)'));
if (result) result = JSON.parse(result[1]);
return result;
}
function writeObjCookie(name, value) {
document.cookie = name + '=' + JSON.stringify(value);
}
//Add tracking event on tabs opening/closing..
window.addEventListener('load', function() {
incrementTabsOpen();
});
window.addEventListener('unload', function() {
decrementTabsOpen();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment