【问题标题】:Check whether an URL is already open or not in Background script在后台脚本中检查 URL 是否已打开
【发布时间】:2022-07-28 01:03:23
【问题描述】:

我使用chrome.runtime.sendMessage({}); 从我的content.js 发送消息,后台脚本会打开一个HTML 文件:

background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    chrome.tabs.create({url: 'popup.html'});
});

如果popup.html已经打开,我不想执行chrome.runtime.sendMessage({});,所以应该有一个if条件来检查它是否已经打开。

但是我在if 条件旁边放了什么?

【问题讨论】:

    标签: javascript google-chrome if-statement tabs


    【解决方案1】:

    var tab, PAGE_URL = "https://www.google.com", btn = document.getElementById('btn');
    
      btn.addEventListener('click', function() {
        if (!tab || tab.closed) {
          tab = window.open(PAGE_URL, "_blank");
        } else {
          alert('window is already opened');
        }
      });
    <span id="btn">Open Tab</span>

    我认为它可能对你有用

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2023-03-25
    • 1970-01-01
    • 2014-07-18
    • 2022-01-06
    相关资源
    最近更新 更多