Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按条件显示的Chrome扩展弹出窗口_Javascript_Google Chrome Extension_Popup - Fatal编程技术网

Javascript 按条件显示的Chrome扩展弹出窗口

Javascript 按条件显示的Chrome扩展弹出窗口,javascript,google-chrome-extension,popup,Javascript,Google Chrome Extension,Popup,我想通过单击显示弹出窗口,但仅当条件为false时。 单击扩展图标后,背景js搜索当前名称的选项卡。如果选项卡发现后台js继续工作。如果找不到-我想显示带有说明的弹出窗口。无法理解在这种情况下如何显示弹出窗口。 我可以通过browserAction.setPopup()设置弹出窗口,但只有在下次单击后才会显示弹出窗口。 我只想显示我的弹出窗口一次。 这绝对是可能的,我在其他扩展上见过这种行为 var pcTabs; // tabs array chrome.browserAction.onCl

我想通过单击显示弹出窗口,但仅当条件为false时。 单击扩展图标后,背景js搜索当前名称的选项卡。如果选项卡发现后台js继续工作。如果找不到-我想显示带有说明的弹出窗口。无法理解在这种情况下如何显示弹出窗口。 我可以通过browserAction.setPopup()设置弹出窗口,但只有在下次单击后才会显示弹出窗口。 我只想显示我的弹出窗口一次。 这绝对是可能的,我在其他扩展上见过这种行为

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。 这是我的。所有代码也在存储库中。
如何将警报替换为弹出窗口?

简言之:你不能按照你描述的方式来做

设置弹出页面时,
chrome.browserAction.onClicked
将不会启动,弹出页面将显示

当未设置弹出页面时,您的事件侦听器将执行,但您可以。您最多只能为下一次单击设置一个弹出窗口

那怎么办呢

1) 你可以做一个埃德温回答中描述的丑陋的黑客(类似的)。始终显示弹出窗口,尽快检查条件,在后台发送消息,如果条件满足,则执行
window.close()

当然,它是丑陋的

2) 正确的方法是在条件可能发生变化时更新弹出窗口。也就是说,无论何时从
pcTabs
添加/删除数据,都应该使用

3) 做这件事的方法是使用实验性的JavaScript方法,这只在Chrome中受支持

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

好的,这是我的代码:

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

你能编辑你的问题吗?缺少的逗号把我搞糊涂了(我不想成为一个DIK和“我想通过点击显示弹出窗口”单击当前页面或浏览器操作此js文件在后台文件或弹出窗口中的何处?而且您不需要放置
!=01,因为在javascript中,
1==true`和
0==false
在后台。感谢您对true/false的解释确定您想要的是:当您单击browserAction时,您希望如果你发现某些标签页不显示弹出窗口,那么显示它?是的。例如extenstion。如果谷歌音乐页面未打开,则显示带有链接的弹出窗口。如果打开-只是暂停\播放音乐。我想实现完全相同的行为供你参考。第二种方式看起来不错。我可以将侦听器设置为chrome.tabs(打开/关闭/更新).但这是个好主意吗?所有标签都会被检查很多次,大部分都是绝对不必要的。我想如果我留下警报就可以了。如果有人使用此扩展,他知道为什么要这样做,并且不需要点击按钮。你也可以使用
chrome.notifications
以比警报更不显眼的方式使用,或者使用second/third方法来启用/禁用按钮,而不是设置弹出窗口。有一个API以编程方式显示弹出窗口,但它被列为白名单:(这是如何回答这个问题的?这不是他想要做的,而是不同的功能意味着不打开新的页面,而是他可以在那里写他想要的东西,比如不更改当前选项卡(突出显示)但我不确定他想做什么。原则上,如果你不确定,最好在回答之前先问。问题的标题很清楚:如何打开(浏览器操作)弹出窗口,或者不取决于条件。这就是条件
if(!tabs[0].url.includes('google.com'))
这是怎么回事?
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
            }
        });
    };
});