Chrome扩展同步调用-仅在窗口关闭后创建窗口

Chrome Extensions synchronous calls - create window only after window closed

本文关键字:窗口 创建 扩展 同步 调用 Chrome      更新时间:2023-09-26


我有这个代码:

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[((]之前完成它所要做的一切
如有任何帮助,我们将不胜感激。感谢

JavaScript承诺救援!

function voteNewWindow(mailNum) {
  return function(){
    return new Promise( function (resolve, reject){
      chrome.windows.create({
      /* ... */
                        }, function (response) {
                            console.log(response);
                            chrome.windows.remove(response.id);
                            console.log('window ' + response.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));
  }
}

请参阅本节以了解此处发生的情况。基本上,我们创建了一个由then粘合在一起的Promises链,这确保了只有在上一个Promises完成后,下一个Promise才会开始执行。

如果您需要在execute()之后执行任何其他操作,请将其放在序列的末尾:

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库,它将chromeapi封装在promise调用中。您的嵌套回调可以修改为类似的内容

var thenChrome = require('then-chrome');
function voteNewWindow(mailNum) {
     return thenChrome.windows.create({url: 'http://www.google.com', incognito: true})
        .then(function(window) {
            return thenChrome.tabs.query({active: true, windowId: window.id})
         })
        .then(function(tabs) {
            return tabs[0];
         })
        .then(function(tab) {
            return thenChrome.tabs.executeScript(tab.id, {file: 'jquery.js'});
         })    
         ...