How to communicate between multiple windows of the same chrome app?

前端 未结 2 924
别跟我提以往
别跟我提以往 2020-12-16 05:58

When using chrome developer tools, it appears that each app window (and the background \'page\') has its own javascript context (space of objects, thread of execution), and

相关标签:
2条回答
  • 2020-12-16 06:35

    That is a great question James!

    Multiple chrome windows are not completely separate. They share a single thread and object space, however the window object is different for each. In javascript unscoped references to things are looked up on the current window, so this makes the windows appear to be different object spaces - but they are not really.

    So, you can reach into another window and execute a function there, or manipulate state in other ways (e.g. set a variable on another window to a function from the current window) and it is acceptable and supported.

    0 讨论(0)
  • 2020-12-16 06:38

    You might find the chrome.app.window.getAll() and chrome.app.window.get() methods useful. They are however new to Chrome 33 which is not yet in the stable channel.

    As an alternative you could hold an array of opened AppWindow objects in the background page context.

    You can then get a reference to the background page context from any window using the chrome.runtime.getBackgroundPage() method

    0 讨论(0)
提交回复
热议问题