简体   繁体   English

如何使用 Chrome 扩展程序关闭当前的 Chrome 选项卡?

[英]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.我几乎完成了构建一个简单的 Chrome 扩展程序,我想添加的最后一件事是关闭当前选项卡。

What this extension does is, when it detects a Zoom join meeting page, it clicks the button so that the Zoom application opens.此扩展程序的作用是,当它检测到 Zoom 加入会议页面时,它会单击按钮以打开 Zoom 应用程序。 I want it to close the Zoom join page after that.我希望它在那之后关闭 Zoom 加入页面。

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). 这个问题说你需要标签的ID来关闭它,所以我希望能够获取ID然后关闭页面(似乎我需要向后台页面传递一条消息)。

If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs .如果您想使用chrome.tabs ,则将消息从 content_script 传递到后台脚本并使用chrome.tabs

Here's my current extension:这是我当前的扩展:

manifest.json 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加入.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?那么基本上我如何向关闭 Chrome 标签的后台页面发送消息?

You can send message to the background script from content script by using the chrome.runtime.sendMessage API.您可以使用 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 API 从后台页面接收消息

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

}) })

Thanks to @Noob dev but for manifest v3 I needed to change from message to msg .感谢@Noob dev,但对于 manifest v3,我需要从message更改为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:在你的 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);
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM