简体   繁体   English

Chrome扩展程序弹出按条件显示

[英]Chrome extension popup showing by condition

I want to show popup by click, but only if condition is false. 我想通过单击显示弹出窗口,但仅当条件为false时才显示。 After click to extension icon background js searchig for tab with current name. 点击扩展图标后,使用当前名称搜索选项卡。 If tab found background js continues working. 如果找到选项卡,则后台js继续有效。 If not found - i want to show popup with instructions. 如果没有找到 - 我想显示弹出窗口的说明。 Can`t understand how to just show popup in this case. 在这种情况下,不能理解如何只显示弹出窗口。 I can set popup by browserAction.setPopup(), but popup will be displayed only after next clicks. 我可以通过browserAction.setPopup()设置弹出窗口,但弹出窗口只会在下次点击后显示。 I just want to show my popup one time. 我只想一次显示我的弹出窗口。 It is definitely posible, I've seen this behavior on other extension. 这绝对是可能的,我在其他扩展上看到过这种行为。

var pcTabs; // tabs array

chrome.browserAction.onClicked.addListener(buttonClick);

function buttonClick() {
 // getting tabs...
 if(pcTabs.length != 0){
    // working with finded tabs
 } else{ // tabs not found
    // show popup.html here. this is the question
 }
}

upd . 更新 This is my background.js . 这是我的background.js All code also in repository. 所有代码也在存储库中。 How to replace alerts to popups? 如何将警报替换为弹出窗口?

In short: you can't do it the way you describe. 简而言之:你不能按照你描述的方式去做。

When a popup page is set, chrome.browserAction.onClicked won't fire, and the popup will show. 设置弹出页面时,不会触发chrome.browserAction.onClicked ,弹出窗口将显示。

When a popup page is not set, your event listener will execute, but you cannot show a popup programmatically . 如果未设置弹出页面,则会执行事件侦听器,但无法以编程方式显示弹出窗口 The most you can do is to set a popup for the next click. 您可以做的最多是为下一次单击设置弹出窗口。

So, what to do with it? 那么,该怎么办呢?

1) You can do an ugly hack (kind of) described in Edwin's answer. 1)你可以做一个在Edwin的回答中描述的丑陋的黑客(某种)。 Always show the popup, check the condition as soon as possible, message the background and execute window.close() if the condition is met. 始终显示弹出窗口,尽快检查条件,向后台发送消息并在满足条件时执行window.close()

Of course, it is ugly. 当然,它很难看。

2) The proper way to do this would be updating the popup whenever the condition can potentially change. 2) 执行此操作的正确方法是在条件可能发生变化时更新弹出窗口。 That is, whenever you add/remove data from pcTabs , you should set/unset the popup with chrome.browserAction.setPopup 也就是说,无论何时从pcTabs添加/删除数据,都应该使用chrome.browserAction.setPopup设置/取消设置弹出chrome.browserAction.setPopup

// ... somewhere ...
pcTabs.push(/*...*/);
chrome.browserAction.setPopup({popup: ''});

// ... somewhere else ...
pcTabs.pop(/*...*/);
if(!pcTabs.length) chrome.browserAction.setPopup({popup: 'popup.html'});    

chrome.browserAction.onClicked.addListener(function() {
  // Assume condition is met, popup didn't show
});

3) The fancy way to do it is to use experimental JavaScript method, Array.observe , that is only supported in Chrome. 3)实现它的奇特方法是使用仅在Chrome中支持的实验性JavaScript方法Array.observe

var pcTabs = [];
Array.observe(pcTabs, function(changes) {
  changes.forEach(function(change) {
    if(change.object.length) {
      chrome.browserAction.setPopup({popup: ''});
    }   else {
      chrome.browserAction.setPopup({popup: 'popup.html'});  
    }
  });
});

Alright this is my code: 好吧这是我的代码:

chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
    if (!tabs[0].url.includes('google.com')) { //check if current tab is not google: if false show popup, you can just put an else at the end and do w.e with your popup
        chrome.tabs.query({currentWindow: true, url: 'https://*.google.com/*'}, function(tabs) { //since current tab is not google query tabs with google
            if (tabs.length) { //check if there are any pages with google
                chrome.tabs.highlight({tabs: tabs[0].index}, function(w) {}); //this is what I chose to do idk what you want to do but you can write w.e here
            } else { 
                chrome.tabs.create({url: 'https://google.com'}); //other wise no pages with google open one
            }
        });
    };
});

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

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