Chrome扩展同步调用 - 创建窗口窗口关闭后,才 [英] Chrome Extensions synchronous calls - create window only after window closed

查看:206
本文介绍了Chrome扩展同步调用 - 创建窗口窗口关闭后,才的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我有这个code:


I have this code:

function voteNewWindow(mailNum) {
    chrome.windows.create({
        url: 'http://www.google.com',
        incognito: true
    }, function (window) {
        console.log('created ' + window.id);
        chrome.tabs.query({
            active: true,
            windowId: window.id
        }, function (tabs) {
            var tab = tabs[0];
            chrome.tabs.executeScript(tab.id, {
                file: "jquery-2.1.1.min.js"
            }, function () {
                chrome.tabs.executeScript(tab.id, {
                    file: "content_script.js"
                }, function () {
                    chrome.tabs.sendMessage(tab.id, {
                        email: JSON.parse(localStorage.mailList)[mailNum]
                    }, function (response) {
                        console.log(response);
                        chrome.windows.remove(window.id);
                        console.log('window ' + window.id + " removed");
                    });
                });

            });
        });

    });
}


function execute() {

    for (var i = 0; i < JSON.parse(localStorage.mailList).length; i++) {

        voteNewWindow(i);
    }
}

的问题是,所有窗口同时打开。我想,当一个之前被关闭一个窗口只开放。我想voteNewWindow()来完成所有它之前,另一voteNewWindow做()执行。结果
任何帮助将是AP preciated。谢谢

The problem is that all windows open at the same time. I want a window to open only when the one before is closed. I want voteNewWindow() to finish all it has to do before another voteNewWindow() executes.
Any help would be appreciated. Thanks

推荐答案

JavaScript的诺言救援!

function voteNewWindow(mailNum) {
  return function(){
    return new Promise( function (resolve, reject){
      chrome.windows.create({
      /* ... */
                        }, function (response) {
                            console.log(response);
                            chrome.windows.remove(window.id);
                            console.log('window ' + window.id + " removed");
                            resolve(); // Proceed to the next
                        });
      /* ... */
    }
  }
}

function execute() {
  var sequence = Promise.resolve();
  for (var i = 0; i < JSON.parse(localStorage.mailList).length; i++) {
    sequence = sequence.then(voteNewWindow(i));
  }
}

请参阅本节以了解发生了什么这里。基本上,我们通过创建粘在一起承诺链然后,以确保下一个会开始只有previous一个人完成之后执行。

See this section to understand what's happening here. Basically, we're creating a chain of Promises glued together by then, which ensures the next one will start executing only after the previous one has finished.

如果您需要在做任何其他操作的execute(),把它在序列的末尾:

If you need to do any other action after execute(), put it at the end of the sequence:

function execute(callback) {
  var sequence = Promise.resolve();
  for (var i = 0; i < JSON.parse(localStorage.mailList).length; i++) {
    sequence = sequence.then(voteNewWindow(i));
  }
  sequence.then(callback);
}

这篇关于Chrome扩展同步调用 - 创建窗口窗口关闭后,才的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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