获取:untured(在承诺中)TypeError:试图打开窗口时出了问题

发布于 2025-01-25 08:40:33 字数 1325 浏览 2 评论 0原文

这是我在服务工作者中的代码通知单击事件。一切正常,但是我会得到此控制台错误未被发现(在Promise)TypeError:试图打开窗口时出现问题。单击“操作”按钮时。。

为什么显示此控制台错误?

self.addEventListener('notificationclick', e => {
  const nf = e.notification;
  nf.close();
  if (!e.action) {
    if(nf.data && nf.data.url)
    {
      const url = nf.data.url;
      urltoopen(url,e);
    }
    return;
  }
  if(e.action === 'close'){
    return;
  }
  if(nf.data && nf.data.listurl)
  {
    const listurl = nf.data.listurl;
    Object.keys(listurl).forEach((k) => {
      if(k===e.action)
      {
        const actionurl=listurl[k];
        urltoopen(actionurl,e);
      }
    });
  }
});

function urltoopen(url,e) {
  const urlToOpen = new URL(url, self.location.origin).href;
  const promiseChain = clients.matchAll({
    type: 'window',
    includeUncontrolled: true
  })
  .then((windowClients) => {
    let matchingClient = null;

    for (let i = 0; i < windowClients.length; i++) {
      const windowClient = windowClients[i];
      if (windowClient.url === urlToOpen) {
        matchingClient = windowClient;
        break;
      }
    }

    if (matchingClient) {
      return matchingClient.focus();
    } else {
      return clients.openWindow(urlToOpen);
    }
  });
  e.waitUntil(promiseChain);
}

This is my code in service worker for notification click event. Everything works fine, but I get this console error Uncaught (in promise) TypeError: Something went wrong while trying to open the window. when I click the action button.

Why is this console error shown?

self.addEventListener('notificationclick', e => {
  const nf = e.notification;
  nf.close();
  if (!e.action) {
    if(nf.data && nf.data.url)
    {
      const url = nf.data.url;
      urltoopen(url,e);
    }
    return;
  }
  if(e.action === 'close'){
    return;
  }
  if(nf.data && nf.data.listurl)
  {
    const listurl = nf.data.listurl;
    Object.keys(listurl).forEach((k) => {
      if(k===e.action)
      {
        const actionurl=listurl[k];
        urltoopen(actionurl,e);
      }
    });
  }
});

function urltoopen(url,e) {
  const urlToOpen = new URL(url, self.location.origin).href;
  const promiseChain = clients.matchAll({
    type: 'window',
    includeUncontrolled: true
  })
  .then((windowClients) => {
    let matchingClient = null;

    for (let i = 0; i < windowClients.length; i++) {
      const windowClient = windowClients[i];
      if (windowClient.url === urlToOpen) {
        matchingClient = windowClient;
        break;
      }
    }

    if (matchingClient) {
      return matchingClient.focus();
    } else {
      return clients.openWindow(urlToOpen);
    }
  });
  e.waitUntil(promiseChain);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文