Javascript close all tabs

Close all tabs to right of current tab with JavaScript or greasemonkey?

In case you don’t mind changing the default browser config to allow scripts to close tabs, use to raise a flag that will be periodically checked by your script in other tabs: And make sure the actually includes the urls of those other tabs. I’m currently coding a bot to do something for me and currently it has to open a large number of tabs every iteration and to make the bot fully automatic I have to find a way to close them all except the original the bot is running from.

Close all tabs to right of current tab with JavaScript or greasemonkey?

I’m currently coding a bot to do something for me and currently it has to open a large number of tabs every iteration and to make the bot fully automatic I have to find a way to close them all except the original the bot is running from. (The tabs have to be closed before the next iteration or what it’s doing fails.)

Читайте также:  Setting Border inside DIV Element

I found a way to actually do this using a function that would detect the URL and do a specific function for that and then close it. Here’s the code.

if (window.location.href.indexOf("https://www.google.com") != -1)  

So this detects that my URL contains a certain string and therefore activates on all pages after this URL. It works for what I want.

  • Not possible generally via userscripts because modern browsers block the attempts to close tabs /windows (Firefox has a config value to allow it but not all users would be willing to enable it). You will have to convert the userscript to an extension/addon.
  • In case you don't mind changing the default browser config to allow scripts to close tabs, use GM_setValue to raise a flag that will be periodically checked by your script in other tabs:
var dontCloseMe = false; setInterval(function() < var shouldClose = Date.now() - GM_getValue("terminate", 0) < 2 * 100; if (shouldClose && !dontCloseMe) < window.close(); >>, 100); . if (shouldCloseOtherTabs)

How do I close a firefox tab from a greasemonkey script?, You need to change configuration settings of Firefox (about:config) to allow this. Steps: Go to address bar and type about:config; Go to parameter dom.allow_scripts_to_close_windows; Set its value as true; Now your script can close the TAB with 'window.close()' Code samplefunction closeTab()Feedback

How to run script in Firefox when a tab was closed?

At our company we are using a web application with shared licenses. Unfortunately if someone simply closes the tab the application is running in it wont release the license lock. I am wondering whether it is possible to run/trigger a scipt when a Firefox tab is closed, so I could automatically release the licenses? I think greasemonkey might be able to do this, but I haven't found a solution yet.

There is both window.onbeforeunload and window.onunload , which are used differently depending on the browser. You can assing them either by setting the window properties to functions, or using the .addEventListener :

window.onbeforeunload = function() < // Do something >// OR window.addEventListener("beforeunload", function(e)< // Do something >, false); 

Usually, onbeforeunload is used if you need to stop the user from leaving the page (ex. the user is working on some unsaved data, so **** should save before leaving).

You can try to release locks in unload events, as Bcfm suggested in his answer, but what if browser or computer simply crashes? Or script takes too long to execute and gets killed by browser anyway?

Another approach would be to make the site constantly ping license server (i.e. every 10 seconds) so that lock is hold until there is no ping for proportional amount of time (i.e. 30 seconds). This way the license lock is freed in all cases.

Of course this may be not relevant to your scenario, it's just a suggestion.

Close all tabs to right of current tab with JavaScript or, Not possible generally via userscripts because modern browsers block the attempts to close tabs/windows (Firefox has a config value to allow it but not all users would be willing to enable it). In case you don't mind changing the default browser config to allow scripts to close tabs, use GM_setValue to raise a …

Need userscript to close the 2nd tab and reload the 1st tab if a word fround in 2nd tab

I have 2 tab opened in firefox. I need the following thing.

If the following word is found in the 2nd tab content, then the userscript will close the 2nd tab and then reload the 1st tab.

Sorry, this page isn't available.

I have tried but it is closing the 2nd tab but not reloading the 1st tab.

// ==UserScript== // @name Ins Sorry // @namespace Ins Sorry // @version 1 // @include https://instagram.com/* // @match https://instagram.com/* // ==/UserScript== if ( ( document.documentElement.textContent || document.documentElement.innerText ).indexOf('Sorry') > -1 )

An userscript does not have global access to your internet browser, it knows only the page on which it was called and knows absolutely nothing about the other tabs. Its action is limited to the context of the page and the tab on which it executes.

This is why you can not ask your script to update the first tab and close the second.

What happens using your code:

location.reload(); // Try to reload the current tab (the 2nd one) window.top.close(); // Close the current tab (the 2nd one) 

However, usesrcript have access to a shared memory space of your browser called Web Storage . You can write content in this space with a script that runs on the second tab, and the script of the first tab can read this content.

What is useful is that there is an event that triggers when this memory space is changed.

See this related answer: Javascript; communication between tabs/windows with same origin

So you can attach a handler to this event on your script which runs in the first tab, and close the tab when a value is changed.

2nd tab userscript:

if ((document.documentElement.textContent || document.documentElement.innerText).indexOf('Sorry') > -1)

1st tab userscript:

window.addEventListener("storage", function(event) < if (event.key == "word_found") < location.reload(); >>); 

Close firefox tab from javascript, According to Mozilla Firefox Deverlopers forum, it is not possible now. Read below. "In the past, when you called the window object's close() method directly, rather than calling close() on a window instance, the browser closed the frontmost window, whether your script created that window or not.

Javascript window.close() for all browsers?

I've been searching around for this problem, couldn't find anything that'll solve this. I found a lot of SO questions, but none could help me.

Basically, the problem has no difference from the questions asked, but I tried every answers. No luck.

I want to close the tab of the browser, when a user clicks to a button. I tried the the stuff below:

None of the above works both in Firefox and Chrome. If one works in Chrome, then it doesn't in Firefox. What is the solution for this?

Better you write the code inside a function and call that function in each button

try to pass true window.open('','_parent','', true); for each creating window

I finally managed to work it out. The thing is that all of the answers are true, along with my trials. All of them work. But in Firefox, one must make dom.allow_scripts_to_close_windows true (take a look at here) to make window.close() to work. But this can be only done by the user itself. And the another problem is that the window must be opened by the script, if one wants to close by the script. So, I will change my structure in a way that this particular page will be opened by a script.

How to run script in Firefox when a tab was closed?, 2 Answers. There is both window.onbeforeunload and window.onunload, which are used differently depending on the browser. You can assing them either by setting the window properties to functions, or using the .addEventListener: window.onbeforeunload = function () < // Do something >// OR …

Источник

JavaScript: A Guide to Bookmarking and Closing Multiple Tabs

The issue with the inability to bookmark and remove multiple tabs is partly due to the function relying on a global variable that is altered by an asynchronous event handler responsible for tracking the active tab. One solution could be to communicate information through writing to cookies or local storage and having all tabs monitor that area, instead of relying on the global variable.

Close all browser tabs except current tab via javascript

It's not possible to have direct control over other tabs. However, an alternative is to write to cookies or local storage and have all tabs keep an eye on that area, enabling you to share information with them.

In my opinion, prioritizing the resolution of the issue where your website fails to function properly in multiple tabs would be beneficial.

Javascript - Detect browser or tab closing, After my initial research i found that when we close a browser, the browser will close all the tabs one by one to completely close the browser. Hence, i observed that there will be very little time delay between closing the tabs. So I taken this time delay as my main validation point and able to achieve the browser and … Code sample

dismiss.

$("#Notice").click(function()

How to bookmark and close all tabs with javascript?

Your code may be too complex for the task at hand. One reason why you cannot use the Bookmark function to bookmark and remove multiple tabs is because it relies on a global variable that is altered by an asynchronous event handler that tracks the active tab. To make the function more versatile, you can modify it to accept an argument that is provided to the function.

Kindly note that I have removed the tab from the bookmarkTab function (also known as Bookmark in your code). It is not advisable to have it there while only calling the Bookmark function. As a solution, I have included a new function named bookmarkAndRemoveTab() which is explicitly named for the two tasks it performs.

Only the parts linked to the browserAction are applicable, such as:

var currentBookmark; /* Add a bookmark for a tab * tabsTab - The tabs.Tab object for the tab containing the page to bookmark * callback - Called with the tabs.Tab object when the bookmark is complete */ function bookmarkTab(tabsTab, callback) < chrome.bookmarks.create(, function(bookmark) < currentBookmark = bookmark; if(typeof callback === 'function')< callback(tabsTab); >>); > /* Remove a Tab * tabsTab - The tabs.Tab object for the tab to remove * callback - Called with the, now invalid, tab ID of the removed tab */ function removeTab(tabsTab, callback) < let rememberedId = tabsTab.id; //Unknown if object changes upon removal chrome.tabs.remove(rememberedId,function()< if(typeof callback === 'function')< callback(rememberedId); >>); > /* Bookmark and remove a tab once the bookmark has been made * tabsTab - The tabs.Tab object for the tab to remove */ function bookmarkAndRemoveTab(tabsTab) < //When we get here from the browserAction click, tabsTab is the active tab // in the window where the button was clicked. But, this function can be used // anytime you have a tabs.Tab object for the tab you want to bookmark and delete. bookmarkTab(tabsTab,removeTab); >chrome.browserAction.onClicked.addListener(bookmarkAndRemoveTab); 

One option is to create a function that performs bookmarkAndRemoveTab() for each tab.

/* Bookmark and remove all tabs */ function bookmarkAndRemoveAllTabs() < //Get all tabs in 'normal' windows: // May want to test. Could want to get all tabs in all windows // Of windowTypes:["normal","popup","panel","devtools", probably only // want "normal" and "popup" tabs to be bookmarked and closed. let queryInfos = [,]; queryInfos.forEach(function(queryInfo) < chrome.tabs.query(queryInfo, function(tabs) < for (let tab of tabs) < bookmarkAndRemoveTab(tab); >>); >); > 

Javascript - Script to close other tabs or browser, Javascript. //Function OpenInNewTab function OpenInNewTab (url) < var win = window.open (url, '_blank'); win.focus (); return false; >I was playing with this script, but it only closes the current tab. I want the current tab to be open and close all other tabs or close the entire browser itself.

Script to close browser tabs

@echo off start chrome --new-window "www.cnn.com" "www.espn.com" "www.microsoft.com" 

Javascript Code to close tab that works in IE, Firefox, If you open a window using JavaScript, you can close it later with window.close (), which has been around since the days of Netscape Navigator. I'm not aware of any other method. If you didn't open the window in the first place, there's no way for you to close it. It's a security mechanism that (again) has been …

Close all child tabs when parent tab is closed

Upon executing the window.open() function, a handle is assigned to you.

myWindow = window.open(/* open stuff*/); 

If you maintain a list of these handles in a container like an array, you can subsequently utilize:

Whenever you want to open a window:

wnds[wnds.length] = window.open(/* open stuff*/); 

Close all browser tabs except current tab via javascript, In my web site, when a user uses many tabs in same browser it fails. I want to close all tabs except the current one when any events triggered. Here is a solution that I found: function close_window () < if (confirm ("Close Window?")) < close (); >> This will work but how can I access other tabs? javascript browser …

Источник

Оцените статью