简体   繁体   English

防止 Chrome 扩展程序的 popup.html 自行打开

[英]Preventing a Chrome extension's popup.html from opening itself

I'm creating a Chrome extension that has a background.html file which requests information from an API once every minute.我正在创建一个具有background.html文件的 Chrome 扩展程序,该文件每分钟从 API 请求一次信息。 Once it receives the information, it messages popup.html with the JSON information with which popup uses to append new HTML elements onto the popup's body.一旦它接收到信息,它popup.html发送 JSON 信息,popup 使用该 JSON 信息将新的 HTML 元素附加到弹出窗口的主体上。

The problem is background is constantly running (as it should), but it will ping popup even when popup is closed.问题是后台一直在运行(应该如此),但即使弹出窗口关闭,它也会 ping 弹出窗口。 This causes popup to open itself every minute which is very annoying.这会导致弹出窗口每分钟自行打开,这非常烦人。

I want to know, is there a way to see if popup is closed and not do anything if that's the case?我想知道,有没有办法查看弹出窗口是否关闭并且如果是这种情况则不做任何事情? Or is there another way to prevent popup opening on its own?还是有其他方法可以防止弹出窗口自行打开?

Here's the Github repository , but the important parts are highlighted below.这是Github 存储库,但重要部分在下面突出显示。

Here's how I'm pinging popup:以下是我 ping 弹出窗口的方式:

// background.js

function sendQuestions()                                                                                                                                                                                    
{                                                                                                                                                                                                           
    var questions = JSON.parse(db.getItem(storage));                                                                                                                                                        
    chrome.extension.sendRequest(appid, { 'questions': questions }, function() {});                                                                                                                         
}  

setInterval(sendQuestions, 60e3);

Here's how popup handles it:以下是弹出窗口的处理方式:

// popup.js

chrome.extension.onRequest.addListener(function(request) {                                                                                                                                                  
    if (request.questions) {                                                                                                                                                                                
        displayQuestions(request.questions);                                                                                                                                                                
    }
});

function displayQuestions(questions)                                                                                                                                                                        
{     
    for (i = 0; i < questions.length; i++) {                                                                                                                                                                
        var question = questions[i];  
        var htmlBlock = // ... generate a block of html ...
        $('#container').prepend(htmlBlock);
    }
}

Open a long lived connection from the popup to the background_page anytime it opens.随时打开从弹出窗口到 background_page 的长期连接 In the background_page you can check to see if the connection is currently active.在 background_page 中,您可以检查连接当前是否处于活动状态。 If it is pass the necessary messages otherwise wait until the connection is active.如果它通过了必要的消息,否则等待连接处于活动状态。

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

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