gpt4 book ai didi

google-chrome - clients.openWindow() "Not allowed to open a window."在 serviceWorker Google Chrome 上

转载 作者:行者123 更新时间:2023-12-03 22:59:48 41 4
gpt4 key购买 nike

我正在 Chrome 版本 42.0.2311.152m 下进行测试,我想实现在通知点击上打开一个窗口,如下例所示:(来源:https://developer.mozilla.org/en-US/docs/Web/API/WindowClient
)

self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();

// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});

我的文件结构是这样的:
https://myurl.no-ip.org/app/index.html
https://myurl.no-ip.org/app/manifest.json
https://myurl.no-ip.org/app/service-worker.js

我有一个问题,我总是得到一个

InvalidAccessError



在 service-worker.js 中调用 clients.openWindow('/') 或 clients.openWindow(' https://myurl.no-ip.org/app/index.html ') 时,我收到错误:
{code: 15,
message: "Not allowed to open a window.",
name: "InvalidAccessError"}

永远不会到达“return client.focus()”行,因为 client.url 永远不会只是'/'。
看着
clients.matchAll({type: "window"})
.then(function (clientList) {
console.log(clientList[0])});

我看到了我当前的 WindowClient:
{focused: false,
frameType: "top-level",
url: "https://myurl.no-ip.org/app/index.html",
visibilityState: "hidden" }

'focused' 和 'visibilityState' 属性是正确的并且可以正确更改。
通过进行手动焦点调用
clients.matchAll({type: "window"})
.then(function (clientList) {
clientList[0].focus()});

我收到错误:
{code: 15,
message: "Not allowed to focus a window.",
name: "InvalidAccessError"}

我认为问题在于 url 不仅仅是'/'。你对此有什么想法吗?

非常感谢你!
最好的祝福
和我

最佳答案

您的代码对我来说很好,所以我将解释使用 openWindow 的要求/focus ,以及如何避免“不允许 [打开|聚焦] 窗口”错误消息。
clients.openWindow()windowClient.focus()仅在单击通知后才允许(至少在 Chrome 47 中),最多为 一个 在单击处理程序的持续时间内,可以调用这些方法中的一些。此行为在 https://github.com/slightlyoff/ServiceWorker/issues/602 中指定。 .

如果您的 openWindow/focus调用被拒绝并显示错误消息

"Not allowed to open a window." for openWindow
"Not allowed to focus a window." for focus



那么你没有满足 openWindow的要求/ focus .例如(所有点也适用于 focus ,而不仅仅是 openWindow )。
  • openWindow在未点击通知时调用。
  • openWindownotificationclick 之后调用处理程序返回,您没有调用 event.waitUntil promise 。
  • openWindow在 promise 传递给 event.waitUntil 后调用已解决。
  • promise 没有解决,但它花了“太长时间”( 10 seconds in Chrome ),所以调用 openWindow 的临时权限已到期。
  • openWindow 真的很有必要/ focus最多调用一次,并且在 notificationclick 之前调用处理程序完成。

    正如我之前所说,问题中的代码有效,所以我将展示另一个带注释的示例。

    // serviceworker.js
    self.addEventListener('notificationclick', function(event) {
    // Close notification.
    event.notification.close();

    // Example: Open window after 3 seconds.
    // (doing so is a terrible user experience by the way, because
    // the user is left wondering what happens for 3 seconds.)
    var promise = new Promise(function(resolve) {
    setTimeout(resolve, 3000);
    }).then(function() {
    // return the promise returned by openWindow, just in case.
    // Opening any origin only works in Chrome 43+.
    return clients.openWindow('https://example.com');
    });

    // Now wait for the promise to keep the permission alive.
    event.waitUntil(promise);
    });
    index.html
    <button id="show-notification-btn">Show notification</button>
    <script>
    navigator.serviceWorker.register('serviceworker.js');
    document.getElementById('show-notification-btn').onclick = function() {
    Notification.requestPermission(function(result) {
    // result = 'allowed' / 'denied' / 'default'
    if (result !== 'denied') {
    navigator.serviceWorker.ready.then(function(registration) {
    // Show notification. If the user clicks on this
    // notification, then "notificationclick" is fired.
    registration.showNotification('Test');
    });
    }
    });
    }
    </script>

    PS。 Service Worker 仍在开发中,因此值得一提的是,我已经验证上述备注在 Chrome 49 中是正确的,并且该示例在 Chrome 43+ 中有效(并且打开 / 而不是 https://example.com 也适用于 Chrome 42)。

    关于google-chrome - clients.openWindow() "Not allowed to open a window."在 serviceWorker Google Chrome 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30302636/

    41 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com