Lakshya Raj
Lakshya Raj

Reputation: 1775

How to close the current Chrome tab with a Chrome Extension?

I am almost finished with building a simple Chrome Extension, and the last thing I want to add to it is the closing of the current tab.

What this extension does is, when it detects a Zoom join meeting page, it clicks the button so that the Zoom application opens. I want it to close the Zoom join page after that.

There are many questions that show how to do it, but I can't find a full example on how to do it. In the documentation on how to send messages, it has three code blocks and I can't understand what goes where and how I can use it to close the current tab.

This question says you need the tab's ID to close it, so I want to be able to get the ID and then close the page (seems I need to pass a message to the background page).

If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs.

Here's my current extension:

manifest.json

{
    "name": "Auto Zoom Join",
    "version": "1.0.0",
    "manifest_version": 2,
    "author": "",
    "description": "Automatically joins zoom meetings.",
    "permissions": ["tabs"],
    "content_scripts":
    [{
        "matches": ["https://*.zoom.us/j/*"],
        "js": ["join.js"]
    }]
}

join.js

if(location.href.length-(location.href.indexOf("j/")+11)-location.hash.length>0){
  document.getElementsByClassName("_1FvRrPS6")[0].click();
}
// you can ignore above line; just detects if valid zoom join meeting page, clicks button

chrome.tabs.query({
  currentWindow:true,
  active:true
},function(tabs){
  chrome.tabs.remove(tabs[0].id);
});

/*
  The above line doesn't work.  It only works in background page, so I need to
  somehow send a message to the background page.
*/

So basically how do I send a message to the background page which closes the Chrome tab?

Upvotes: 5

Views: 2673

Answers (2)

Noob dev
Noob dev

Reputation: 144

You can send message to the background script from content script by using the chrome.runtime.sendMessage API.

chrome.runtime.sendMessage({ msg: "tab_close_msg", time: time }, function (response) {
   console.log("response from the bg", response)
});

And You can receive the message from the background page by using the chrome.runtime.onMessage.addListener API

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if (request.message === "tab_close_msg") {
    // add your code to close tab
  } 
})

Upvotes: 3

Daniel Maiochi
Daniel Maiochi

Reputation: 627

Thanks to @Noob dev but for manifest v3 I needed to change from message to msg.

The following worked for me:

on your content script page:

function closeLastTab() {
    chrome.runtime.sendMessage(
        { 
            msg: "tab_close_msg"
        }, 
        function (response) {
            console.log("response from the bg", response)
        }
    );
}

on your background.js:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.msg === "tab_close_msg") {
        chrome.tabs.query({
            currentWindow: true,
            active: true
        }, function (tabs) {
            chrome.tabs.remove(tabs[0].id);
        });
    }
});

Upvotes: 1

Related Questions