Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Excerpt

You can close tabs in a browser that are associated with a specific domain like "foo.com" using the following JavaScript code


Code Block
function close_tabs(domain_name)
{
	// Get all open windows in the browser
	const windows = window.top;

	// Loop through each window
	for (let i = 0; i < windows.length; i++) {
  		// Get all the tabs in the current window
  		const tabs = windows[i].document.getElementsByTagName('a');

  		// Loop through each tab
  		for (let j = 0; j < tabs.length; j++) {
    			// Check if the tab's URL contains "foo.com"
    domain_name
			if (tabs[j].href.includes('google.com'domain_name)) {
      				// Close the tab
      			windows[i].close();
    			}
		}
  	}
}
close_tabs("foo.com");