
[英]Is there a way to close a chrome extension popup, when the user is on a separate chrome window?
[英]how to close a popup window when the user clicks on the main browser window?
chrome.windows.create(
{
url: url.href,
type: "popup",
left: w,
width: 500,
height: 900
});
我使用这段代码在主浏览器 window 上启动弹出窗口 window。如果用户单击主浏览器 window,如何使弹出窗口 window 自动关闭。我还使用 Manifest V3 作为扩展.
chrome.windows.create(
{
url: url.href,
type: "popup",
left: w,
width: 500,
height: 900
});
window.addEventListener("blur", function()
{
window.close();
});
我试图寻找“模糊”事件来自动关闭 window,但这没有用。
// gets resolution of the main window to open the popup in the right corner
chrome.windows.getCurrent((cw) => {
let w, h;
w = cw.width - 500;
// creates new popup window
chrome.windows.create(
{
url: url.href,
type: "popup",
left: w,
width: 500,
height: 900,
});
// gets ID of the popup window
chrome.windows.getCurrent((pop) =>
{
let popid = pop.id;
// when focus changes the window closes
chrome.windows.onFocusChanged.addListener( (stat) =>
{
chrome.windows.remove(popid);
});
});
试过这个,它在打开时立即关闭 window。
// All rights reserved to Isabella Rasku and Aditya Ponde 2023.
// making the context menu
chrome.runtime.onInstalled.addListener(async () => {
chrome.contextMenus.create({
id: "Lookup",
title: "Lookup On RMP",
type: 'normal',
contexts: ['selection']
});
});
// opens RMP when context menu is clicked with the search perms using selected text
chrome.contextMenus.onClicked.addListener((item, tab) => {
var name = item.selectionText;
var uname = name.replace(/ /g, "+");
let url = new URL("https://www.ratemyprofessors.com/search.jsp?queryBy=teacherName&schoolName=&queryoption=HEADER&")
url.searchParams.set('query', uname)
// gets resolution of the main window to open the popup in the right corner
chrome.windows.getCurrent((cw) => {
let w, h;
w = cw.width - 500;
// creates new popup window
chrome.windows.create(
{
url: url.href,
type: "popup",
left: w,
width: 500,
height: 900,
});
// gets ID of the popup window
chrome.windows.getCurrent((pop) =>
{
let popid = pop.id;
// when focus changes the window closes
chrome.windows.onFocusChanged.addListener( (stat) =>
{
chrome.windows.onFocusChanged.addListener( (stat) =>
{
chrome.windows.remove(popid);
});
});
});
});
});
这行得通!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.