我几乎完成了一个简单的Chrome扩展程序的构建,最后要添加的内容是关闭当前标签页。
该扩展程序的作用是,当它检测到“缩放加入会议”页面时,单击该按钮,以便打开“缩放”应用程序。之后,我希望它关闭“缩放”加入页面。
有很多问题显示了如何执行此操作,但是我找不到有关如何执行操作的完整示例。在有关如何发送消息的文档中,它具有三个代码块,我无法理解该怎么去以及如何使用它来关闭当前选项卡。
这个问题说您需要关闭选项卡的ID,所以我希望能够获取ID然后关闭页面(似乎我需要向后台页面传递消息)。
如果您想使用,
chrome.tabs
则将消息从content_script传递到后台脚本并使用chrome.tabs
。
这是我当前的扩展名:
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.
*/
那么,基本上,我如何向关闭Chrome标签页的后台页面发送消息?
您可以使用chrome.runtime.sendMessage API将消息从内容脚本发送到后台脚本。
chrome.runtime.sendMessage({ msg: "tab_close_msg", time: time }, function (response) {
console.log("response from the bg", response)
});
而且,您可以使用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
}
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句