防止Chrome扩展程序的popup.html打开自身 [英] Preventing a Chrome extension's popup.html from opening itself

查看:179
本文介绍了防止Chrome扩展程序的popup.html打开自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Chrome扩展程序,该扩展程序包含一个 background.html 文件,该文件每分钟向API请求一次信息。一旦它收到信息,它就会弹出 popup.html 和JSON信息,弹出窗口用来将新的HTML元素附加到弹出窗体上。

I'm creating a Chrome extension that has a background.html file which requests information from an API once every minute. 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.

问题在于背景不断运行(因为它应该),但即使在弹出窗口关闭的情况下,它也会弹出窗口。这会导致弹出窗口每分钟打开一次,这非常烦人。

The problem is background is constantly running (as it should), but it will ping popup even when popup is closed. This causes popup to open itself every minute which is very annoying.

我想知道,是否有办法查看popup是否已关闭,如果是案件?或者是有另一种方法来防止弹出自己打开?

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?

以下是 Github存储库,但重要的部分在下面突出显示。

Here's the Github repository, but the important parts are highlighted below.

以下是我弹出ping命令的方式:

Here's how I'm pinging popup:

// 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);
    }
}


推荐答案

打开您可以随时从长时间连接弹出窗口到background_page。在background_page中,您可以检查连接是否当前处于活动状态。如果它传递必要的消息,则等待连接处于活动状态。

Open a long lived connection from the popup to the background_page anytime it opens. In the background_page you can check to see if the connection is currently active. If it is pass the necessary messages otherwise wait until the connection is active.

这篇关于防止Chrome扩展程序的popup.html打开自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆