A Chrome extension that protects you from accidentally closing important tabs by showing a confirmation dialog on specified websites.
Chrome doesn't allow extensions to silently block Ctrl+W (by design, to protect users). Instead, Tab Close Guard shows a browser confirmation dialog when you try to close a tab, giving you a chance to cancel.
Important: The confirmation dialog only appears after you've interacted with the page (clicked, typed, etc.). This is a browser security feature to prevent malicious sites from trapping you.
- Show confirmation dialog on all websites or specific sites
- Add sites using wildcard patterns (e.g.,
*.google.com) - Easy-to-use popup interface
- Works for all close methods: Ctrl+W, Cmd+W, clicking X, closing window, etc.
- Lightweight and fast
- Open Chrome and navigate to
chrome://extensions/ - Enable "Developer mode" using the toggle in the top-right corner
- Click "Load unpacked"
- Select this directory
- The extension should now appear in your extensions list
- Click the puzzle piece icon in Chrome's toolbar
- Find "Tab Close Guard" in the list
- Click the pin icon to keep it visible in your toolbar
- Click the extension icon in your toolbar
- Check the "Block on all websites" checkbox
- Now all websites will show a confirmation when closing
- Click the extension icon
- Ensure "Block on all websites" is unchecked
- Enter a website pattern in the input field:
- Exact domain:
app.zoom.us - Wildcard subdomain:
*.google.com - Specific subdomain:
mail.google.com
- Exact domain:
- Click "Add Site" or press Enter
- Alternatively, click "Add Current" to add the current tab's site
- Click the extension icon
- Find the site you want to remove
- Click the "Remove" button next to it
Sites you might want to protect:
app.zoom.us- Zoom meetings (prevent accidentally leaving meetings)*.google.com- All Google sites (Gmail, Docs, etc.)github.com- GitHub (protect unsaved code)stackoverflow.com- Stack Overflow*.reddit.com- All Reddit siteslocalhost- Local development servers*.figma.com- Figma designsnotion.so- Notion workspace
The extension uses:
- Content scripts running in both MAIN and ISOLATED worlds
- beforeunload event to trigger the browser's native confirmation dialog
- Chrome storage API to save your site preferences
- Wildcard pattern matching to check if the current site should be protected
When you try to close a tab on a protected site, the browser shows its standard "Leave site?" confirmation dialog.
manifest.json- Extension configurationinjected.js- Runs in page context, handles beforeunload eventcontent.js- Manages settings from chrome.storagepopup.html- Extension popup UIpopup.css- Popup stylingpopup.js- Popup functionalityicon*.png- Extension icons
This extension requires:
storage- To save your site preferencesactiveTab- To get the current tab's URL when using "Add Current"
To modify the extension:
- Make your changes to the source files
- Go to
chrome://extensions/ - Click the refresh icon on the extension card
- Refresh any tabs where you want to test the changes
-
Make sure you've interacted with the page
- Click anywhere on the page or type something
- The confirmation only works after user interaction (browser security feature)
-
Check the extension is loaded:
- Go to
chrome://extensions/ - Make sure "Tab Close Guard" is enabled
- Go to
-
Verify settings:
- Click the extension icon
- Ensure either "Block on all websites" is checked OR the current site is in your list
- For specific sites, make sure the pattern matches (use
app.zoom.us, nothttps://app.zoom.us)
-
Check the console:
- Open DevTools (F12)
- Go to the Console tab
- Refresh the page
- Look for
[Tab Close Guard] Protection loaded - Should also see
[Tab Close Guard] Protection ENABLED ✓
-
Refresh the page:
- After adding a site or changing settings, refresh the page
- Content scripts only run when the page loads
Problem: No confirmation dialog appears
- Make sure you clicked or typed on the page first
- Check console to verify protection is enabled
- Verify the site pattern matches (check console for the hostname)
- Try enabling "Block on all websites" to test
Problem: Extension doesn't work on some pages
- Chrome internal pages (
chrome://,chrome-extension://) don't allow extensions - Some pages have strict security policies
- Check your wildcard patterns -
*.zoom.uswon't matchapp.zoom.us(use exact match or*.zoom.us+zoom.us)
Problem: Console shows "Protection disabled"
- The current site isn't in your enabled list
- Check the hostname in the console and add it via the popup
- Or enable "Block on all websites"
Chrome processes Ctrl+W at the browser level before JavaScript can intercept it. This means we cannot silently prevent the shortcut. The beforeunload event is the only reliable way to show a confirmation dialog. This is intentional browser behavior to protect users from malicious sites.
MIT License - see LICENSE file for details.
Copyright (c) 2024 Avinash Joshi