LearnNewsExamplesServices
Frontmatter
id3994
titleChrome permission "window-management" not working
stateClosed
labels
bugstale
assignees[]
createdAtFeb 6, 2023, 2:10 PM
updatedAtAug 30, 2024, 4:29 PM
githubUrlhttps://github.com/neomjs/neo/issues/3994
authorDinkh
commentsCount9
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtAug 30, 2024, 4:29 PM

Chrome permission "window-management" not working

Closed v8.1.0 bugstale
Dinkh
Dinkh commented on Feb 6, 2023, 2:10 PM

I am currently working on a multi-screen app and I need to ask the Chrome User for perssion for "window-management".

Current state in Chrome:

const state = await navigator.permissions.query(
    {name: 'window-management'} // => NOT working, but shoud
    {name: 'window-placement'}     // => works
);

Result for window-management: window-managerment Error Uncaught TypeError: Failed to execute 'query' on 'Permissions': Failed to read the 'name' property from 'PermissionDescriptor': The provided value 'window-management' is not a valid enum value of type PermissionName.

This might be especially interesting to @michaelwasserman and @quisquous and @tomayac

Dinkh added the bug label on Feb 6, 2023, 2:10 PM
tomayac
tomayac Feb 6, 2023, 4:35 PM

We are in the middle of changing the permission name, see https://crbug.com/1328581.

Meanwhile, here's a snippet (deployed here) that should work on all browsers:

document.querySelector('button').addEventListener('click', async () => {  
  let state;
  let permissionName;
  try {
    state = (
      await navigator.permissions.query({
        name: 'window-management',
      })
    ).state;
    permissionName = 'window-management'
  } catch (err) {    
    if (err.name === 'TypeError') {
      try {
        state = (
          await navigator.permissions.query({
            name: 'window-placement',
          })
        ).state;
        permissionName = 'window-placement'
      } catch (err) {
        if (err.name === 'TypeError') {
          state = 'Window management not supported';
        } else {
          state = `${err.name}: ${err.message}`;     
        }
      }
    } else {
      state = `${err.name}: ${err.message}`; 
    }
  }
  if ('request' in Permissions.prototype) {
    state = (await navigator.permissions.request({
      name: permissionName,
    })).state
  }
  document.querySelector('pre').textContent = state;
});
tomayac
tomayac Feb 7, 2023, 3:49 PM

Slightly improved:

async function getWindowManagementPermissionState() {
  let state;
  // The new permission name.
  try {
    ({ state } = await navigator.permissions.query({
      name: "window-management",
    }));
  } catch (err) {
    if (err.name !== "TypeError") {
      return `${err.name}: ${err.message}`;
    }
    // The old permission name.
    try {
      ({ state } = await navigator.permissions.query({
        name: "window-placement",
      }));
    } catch (err) {
      if (err.name === "TypeError") {
        return "Window management not supported.";
      }
      return `${err.name}: ${err.message}`;
    }
  }
  return state;
}

document.querySelector("button").addEventListener("click", async () => { const state = await getWindowManagementPermissionState(); document.querySelector("pre").textContent = state; });

michaelwasserman
michaelwasserman Feb 7, 2023, 9:58 PM

I'm guessing this is just a version issue, as window-management is rolling out in M111. @bradtriebwasser

Dinkh
Dinkh Feb 8, 2023, 10:41 AM

@michaelwasserman thanks, that way it is the next update ;)

Current Version: 110.0.5481.78

tomayac
tomayac Feb 8, 2023, 10:47 AM

Note that many popular Chromium derivates like Samsung Internet stay a long time on outdated Chromium versions. I highly recommend a defensive code pattern (also added to the article%3B)) if you use this API in production and you don't control what browsers people use. It's different in a production in-enterprise setting, where you can enforce a certain version.

Dinkh
Dinkh Feb 8, 2023, 11:43 AM

@tomayac I completely understand what you are saying. I'm not going to disable either of the permission queries.

Currently my fallback is to query the single screen ( window.screen) and parse to a same object. I.e. window.screen as currentScreen and screen[0].

This way I don't have to implement multiple approaches.

@github-actions - 2024-08-30T02:27:04Z

This issue is stale because it has been open for 90 days with no activity.

  • 2024-08-30T02:27:04Z @github-actions added the stale label
tomayac
tomayac Aug 30, 2024, 3:13 PM

In case it's still an open question, this is the permission query that works today:

await navigator.permissions.query({name: 'window-management'});
// PermissionStatus {name: 'window-management', state: 'prompt', onchange: null}
tobiu
tobiu Aug 30, 2024, 4:29 PM

@tomayac thanks for the update.

@Dinkh: i just tested it, and it works fine now.

See: https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query

=> the queries are working inside dedicated & shared workers, so there is nothing to do here (just tested it).

Screenshot 2024-08-30 at 16 28 18

no need for main thread based remote access (this would have been ugly anyways, since PermissionStatus is not spreadable & onchange can not get passed in postMessages).

tobiu closed this issue on Aug 30, 2024, 4:29 PM