简体   繁体   English

javascript,在chrome web应用中创建窗口,如何包含成功调用新窗口功能的承诺?

[英]javascript, create window in chrome web app, how to include a promise for successfully calling a function of the new window?

I want to create a window and when it is created, run a function that has been pre-loaded. 我要创建一个窗口,并在创建窗口时运行已预加载的函数。 I do have to wait until the window is created, otherwise the function is not available and fails. 我必须等到创建窗口之后,否则该功能将不可用并失败。 And I'd say I can do it with a promise. 我会说我可以兑现承诺。

How to do it? 怎么做? Here there's the code with my attempt to do it 这是我尝试执行的代码

one().then(function() {
  new_panel.contentWindow.load_content(something);
})

function one() {
  var promise = chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;
  });

  return promise;
}

It complains about the then of the one() function: Uncaught TypeError: Cannot read property 'then' of undefined 它抱怨那个one()函数的那时:Uncaught TypeError:无法读取未定义的属性'then'

UPDATE: Slightly modified code, still doesn't work, I've used the promise from angular since I'm using it 更新:稍加修改的代码,仍然无法正常工作,因为我一直在使用我的承诺

one().then(function() {
  new_panel.contentWindow.load_content(something);
})

function one() {
  return chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;

    var deferred = $q.defer();
    deferred.resolve();
    console.log(deferred.promise)
    return deferred.promise;
  });
}

In theory, deferred.promise should only be returned when it is resolved, but it is like the then() function is executed before it actually happens. 从理论上讲,deferred.promise仅应在解析后返回,但这就像then()函数在实际发生之前就已执行一样。 Why? 为什么?

UPDATE2: or another way to do the same (doesn't work either) UPDATE2:或执行相同操作的另一种方法(也不起作用)

one();

function one() {
  chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;

    var deferred = $q.defer();
    deferred.resolve();
    console.log('first '+deferred.promise)
    return deferred.promise;
  }).then(function() {
    console.log('second')
    new_panel.contentWindow.load_content(something);
  });
}

The log 'second' is printed before 'first'. 在“第一”之前打印日志“第二”。 Is there anything wrong with the code? 代码有什么问题吗?

You want to synchronously return a promise from one() and resolve the promise with the new window once it has been created: 您想同步地从one()返回一个promise,并在创建新窗口后用新窗口解析promise:

one().then(function(createdWindow) {
  // Window has been created
  createdWindow.contentWindow.load_content(something);
})

function one() {
  // create a promise
  var deferred = $q.defer();

  chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
     // resolve our promise and pass the window to the `then` block
     deferred.resolve(createdWindow)
  });

  // synchronously return a promise
  return deferred.promise;
}

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

相关问题 chrome.app.window.create隐身窗口? - chrome.app.window.create Incognito Window? 使用JavaScript和Google Chrome创建并打开新窗口和打印图像 - Create and Open new Window and Print Images using JavaScript and Google Chrome javascript在窗口对象上调用函数 - javascript calling a function on window object 如何在“onclick”javascript函数中打开新窗口 - How to open new window in "onclick" javascript function 在新窗口中打开Chrome(Chrome应用) - Open Chrome in a new window (Chrome app) 如何在优胜美地使用JavaScript自动打开新的Chrome窗口? - How can I open a new Chrome window with javascript automation in yosemite? 如何禁用在JavaScript中调用另一个函数的window.onscroll函数? - How to disable window.onscroll function calling another function in javascript? 如何为新的 window 创建自定义谷歌浏览器扩展并在扩展的替代点击中保持关注新的 window - How to create custom google chrome extension for new window And keep focus the new window in alternative click of extension 如何使用 Javascript 打开新的隐身窗口? (谷歌浏览器) - How to open new incognito window with Javascript? (Google Chrome) 如何在 Javascript 中使用“window.open”打开新的 chrome - How to open new chrome using 'window.open ' in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM