How to identify when the browser has multiple opened tabs?
Tabs that have ran your script? Here is:
// ==UserScript==
// @name GM_getTabs
// @version 0.1
// @include *
// @run-at document-end
// @grant GM.getTab
// @grant GM_getTab
// @grant GM.getTabs
// @grant GM_getTabs
// @grant GM_saveTab
// ==/UserScript==
// https://stackoverflow.com/questions/25793331/how-does-gm-gettabcb-work
/*
Steps:
1. Open https://example.com/
2. Open the browser console
3. Check the console messages
4. Repeat
*/
// get tab personal object
GM_getTab(function(tabObj) {
tabObj.id = Math.random();
GM_saveTab(tabObj); // update the object
// get all stored objects
GM_getTabs(function(tabsDatabase) {
var dbL = Object.keys(tabsDatabase).length;
console.log(tabsDatabase);
console.log('Script-owned tabs count:', dbL);
});
});
// modern variant
// (async () => {
// const tabObj = await GM.getTab();
// tabObj.id = Math.random();
// GM_saveTab(tabObj); // update the object
// // get all stored objects
// const tabsDatabase = await GM.getTabs();
// const dbL = Object.keys(tabsDatabase).length;
// console.log(tabsDatabase);
// console.log('Script-owned tabs count:', dbL);
// })();
That's closer to what I want. At least it seems that I can use dbL.length to know how many opened tabs the browser has. The problem is... How can I clear that when the browser is closed. I wonder why changing console.log to alert didn't show anything...
So here's what I want. I'm close to do what I want, but I need to spend a bit more time on the development of this script, but time is something that I won't have for a few days...
One of the things I will do in the future is remove location.href for security and privacy purposes, so I will try to use numbers... Also only 1 single browser tab should have the addEventListener('beforeunload' otherwise the annoying confirm to leave website popup will pop up on all opened tabs and that will be annoying
// ==UserScript==
// @name Don't Close my Browser!
// @namespace WarnOnclosingMultipleTabs
// @version 0.1
// @description Show an confirmation box asking if you really want to close your browser when you have multiple tabs opened.
// @author hacker09
// @include *
// @run-at document-end
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getTabs
// ==/UserScript==
(function() {
'use strict';
GM_setValue(location.href, "Opened URL"); //Defines the variable
setTimeout(function(){
if (GM_listValues().length > 1)
{
alert('there is more than 1 opened tab')
window.addEventListener('beforeunload', function (e) {
GM_deleteValue(location.href); //Remove the closed tab link of the script storage
e.preventDefault();
e.returnValue = '';
});
}
}, 3000); //The script must have a timeout, otherwise the script gets parts of the url multiple times before the page fully finishes loading
})();
I tried to do this without success, but now it's needless because I found that I could enable that on edge.
*The comments are wrong in some places
// ==UserScript==
// @name Don't Close my Browser!
// @namespace WarnOnclosingMultipleTabs
// @version 0.1
// @description Show an confirmation box asking if you really want to close your browser when you have multiple tabs opened.
// @author hacker09
// @include *
// @run-at document-end
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getTabs
// ==/UserScript==
(function() {
'use strict';
function DoNotClose()
{ //Starts the function DoNotClose
GM_setValue(location.href, "Opened URL"); //Store the opened url
if (GM_listValues().length === 1) //Before preventing the browser to be closed check if the actual amount of browser tabs is = 1
{ //Starts the if condition
GM_setValue("ProtectedTab", location.href); //Store the opened url
document.querySelector("head > title").innerText = 'protected'; //Adds the text protected to the tab
window.addEventListener('beforeunload', function (e) { //Make the first opened browser tab protect the browser from being closed
if (GM_listValues().length > 1) //Before preventing the browser to be closed check if the actual amount of browser tabs is greater than 1
{ //Starts the if condition
GM_deleteValue(location.href); //Remove the closed tab url of the script storage
GM_deleteValue("ProtectedTab"); //Remove the closed tab url of the script storage
e.preventDefault(); //Prevent the browser from being closed
e.returnValue = ''; //Prevent the browser from being closed
} //Finishes the if condition
}); //Finishes the beforeunload function
} //Finishes the if condition
} //Finishes the function DoNotClose
window.addEventListener('mousemove', function(){if (GM_getValue("ProtectedTab") === undefined) { GM_setValue("ProtectedTab", location.href) } }, false); //When the mouse is moved anywhere on the page run the function DoNotClose
function two()
{
window.addEventListener('beforeunload', function (e) { //Make the first opened browser tab protect the browser from being closed
if (GM_listValues().length >= 1) //Before preventing the browser to be closed check if the actual amount of browser tabs is = 1 or greater
{ //Starts the if condition
GM_deleteValue(location.href); //Remove the closed tab url of the script storage
} //Finishes the if condition
if (GM_getValue("ProtectedTab") === undefined && GM_listValues().length > 1) //Before preventing the browser to be closed check if the actual amount of browser tabs is = 1
{ //Starts the if condition
e.preventDefault(); //Prevent the browser from being closed
e.returnValue = ''; //Prevent the browser from being closed
} //Finishes the if condition
}); //Finishes the beforeunload function
}
if (GM_listValues().length > 1) //Before preventing the browser to be closed check if the actual amount of browser tabs is = 1
{ //Starts the if condition
window.addEventListener('mousemove', two, false); //When the mouse is moved anywhere on the page run the function DoNotClose
} //Finishes the if condition
var link; //Make the variable global
Array.from(document.querySelectorAll('a')).forEach(Element => Element.onmouseover = function() { //Get all the a link elements and add an advent listener to the link element
link = this.href; //Store the actual hovered link to a variable
}); //Finishes the forEach
window.onclick = function() { //Detects the mouse click on the page
if (link !== undefined) //If the user clicked on any link
{ //Starts the if condition
window.removeEventListener('beforeunload', DoNotClose, false); //Stop tracking if the browser is being closed or not
GM_deleteValue(location.href); //Remove the closed tab url of the script storage
} //Finishes the if condition
}; //Finishes the onclick advent listener
if (GM_listValues().length === 0) //Before preventing the browser to be closed check if the actual amount of browser tabs is = 1
{ //Starts the if condition
window.addEventListener('mousemove', DoNotClose, false); //When the mouse is moved anywhere on the page run the function DoNotClose
} //Finishes the if condition
})();
// ==UserScript==
// @name GM_getTabs
// @namespace GM_getTabs
// @version 0.1
// @author hacker09
// @include *
// @run-at document-end
// @grant GM_getTabs
// ==/UserScript==
GM_getTabs(function () {
alert(GM_getTabs.length); //Why is this always 1?
});