单击按钮时打开从 popup.html 打开的相同 window

Open the same window opened from the popup.html when a button is clicked

我有一个 chrome 扩展和一个 popup.html,这个 popup.html UI 有一个按钮,点击它会在外部弹出窗口中打开一个网页 window

popup.html

<body>
    <article style="width: 34rem">
        <button id="open">Click to open the App</button>
    </article>
<script src="popup.js"></script>
<body>

popup.js代码

$(document).ready(function() {
    $("#open").click(function() {
        window.open("https://www.example.com",'The title','height=678,width=650').focus();
    });
});

用户单击 popup.html 上的按钮,它会打开一个 window 并且新的 window 获得焦点(我对此没有问题),新的 window 可以在未激活时由用户最小化。

假设用户最小化了新 window 并再次单击 popup.html 上的按钮,创建了新 window 的另一个实例。

我正在寻找一种方法来打开同样早先最小化的焦点 window 而不是新的。如何实现?

希望这能解决您的问题

$(document).ready(function() {
    $("#open").click(function() {
        chrome.tabs.query({
            title: 'Your title name'
        }, tabs => {
            if (!tabs.length) {
                window.open("https://www.example.com", 'Your title name', 'height=678,width=650').focus();
                return;
            }
            chrome.windows.update(tabs[0].windowId, {
                focused: true
            });
            chrome.tabs.update(tabs[0].id, {
                active: true
            });
        });
    });
});