Using chrome.scripting.executeScript to return window.screen in service_worker

782 views
Skip to first unread message

Nathan Pierce

unread,
Oct 7, 2022, 9:07:33 AM10/7/22
to Chromium Extensions
Hey everyone! I'm curious about how to get the window.screen from the current window/tab when I launch my extension. I'm trying to see if opening a window on the side of the current window/tab will push it off the side of the screen or not.

Here is what I ended up with (which doesn't work):

```
    await chrome.scripting.executeScript(
      {
        func: () => { return window },
        target: { tabId: currentTab[0].id },
        world: 'MAIN',
      },
      (result) => { console.log(result)}
    )
```

If I change return window to, say, document.title, I do see the window title logged, so I at least know this sort of works. However, return window, or even return window.screen just shows an empty object in the log output under "result".

Any ideas what I'm missing?

Jói Sigurdsson

unread,
Oct 7, 2022, 10:26:38 AM10/7/22
to Chromium Extensions, nors...@gmail.com
I'm not sure why that doesn't work, but wanted to chime in to say that an approach we use in our MV3 version (available at https://chrome.google.com/webstore/detail/crankwheel-screen-sharing/emhfnoelnchcaemjmagagkfednmilfgh and the code is not obfuscated, if you want to take a look) is to have the service worker open a minimized browser window that does some work, which can access the screen object and so on, and then closes itself. Depending on your use case that might be a solution?

Cheers,
Jói

wOxxOm

unread,
Oct 7, 2022, 10:29:20 AM10/7/22
to Chromium Extensions, nors...@gmail.com
You can probably just use chrome.system.display.getInfo.
Don't forget to add "system.display" permission as noted at the beginning of the documentation page.

As for a `window` object, it cannot be transferred neither in web messaging nor in the extension-specific API, which currently supports only the primitive types compatible with JSON: string, number, boolean, null, and arrays/objects of these types. To see what will be transferred use JSON.stringify(obj). The window object is not jsonifiable. The window.screen object returns `{}` because it's the way the specification declares its properties. You can transfer individual properties of `screen` e.g. in an array like func: ()=>[screen.availWidth, screen.availHeight]

Nathan Pierce

unread,
Oct 7, 2022, 5:04:40 PM10/7/22
to Chromium Extensions, wOxxOm, Nathan Pierce
chrome.system.display.getInfo() is perfect for me! Here is what I do to resize if it'll run off the side of the screen when I create/attach my window/panel:

// resize the main window if it's too wide to fit with the vlt window open
const displayInfo = await chrome.system.display.getInfo()
if ((vltWindowWidth + rootWindow.width) >= displayInfo[0].bounds.width) {
await chrome.windows.update(rootWindow.id, { width: (displayInfo[0].bounds.width - vltWindowWidth) })
}


Uladzimir Yankovich

unread,
Oct 8, 2022, 1:04:55 PM10/8/22
to Chromium Extensions, nors...@gmail.com, wOxxOm
Here, too, they talk about system.display. I would be grateful if you could support the error of this API - https://bugs.chromium.org/p/chromium/issues/detail?id=1303129&q=&can=4 
Reply all
Reply to author
Forward
0 new messages