无法使用 chrome.app.window.get 获取 chrome.window 实例

Cannot get chrome.window instance with chrome.app.window.get

我在点击按钮时尝试使用以下代码:

chrome.app.window.create('sample.html', {
        id: 'test',
        'bounds': {
          'width': 200,
          'height': 200
        },
        'resizable' : false,
        'frame': { type: "none" }
    })
    console.debug(chrome.app.window.getAll())
    var windowcreated = chrome.app.window.get('test');
    windowcreated.innerBounds.height = 50;
    windowcreated.innerBounds.width = 200;

但控制台显示如下:

Uncaught TypeError: Cannot read property 'innerBounds' of null

而 getAll() 的调试仅 returns 我在 background.js 中创建的原始 window。我不明白我做错了什么...

chrome.app.window.create()asynchronous

当您的执行达到 chrome.app.window.get('test') 时,window 还不存在。

您需要在 callback of chrome.app.window.create:

中移动您的逻辑
chrome.app.window.create('sample.html', {
    id: 'test',
    'bounds': {
      'width': 200,
      'height': 200
    },
    'resizable' : false,
    'frame': { type: "none" }
}, function(createdWindow) {
  // Do stuff here, and no need for get()
  createdWindow.innerBounds.height = 50;
  createdWindow.innerBounds.width = 200;
});