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

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

本文关键字:window chrome get 获取 实例 app      更新时间:2023-09-26

当我点击按钮时,我正在尝试以下代码:

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;

但控制台上写着:

未捕获的类型错误:无法读取空的属性"innerBounds"

getAll()的调试只返回我在background.js中创建的原始窗口。。。

chrome.app.window.create()是异步的。

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

您需要在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;
});