Javascript 相同选项卡的Clients.openWindow()等效项

Javascript 相同选项卡的Clients.openWindow()等效项,javascript,Javascript,因此,当我收到浏览器通知时,我有两个选择。如果客户端没有打开网站,我将打开一个新选项卡。但是我如何将客户发送到网站上已经存在的某个地方呢 基本上,我想要像client.focus(“some/highlevel/url”)这样的东西,而不是clients.openWindow(“https://someurl.com“”请使用此代码实现所需的行为: // Attempt to extract notification URL var url = event.notification.data.u

因此,当我收到浏览器通知时,我有两个选择。如果客户端没有打开网站,我将打开一个新选项卡。但是我如何将客户发送到网站上已经存在的某个地方呢


基本上,我想要像
client.focus(“some/highlevel/url”)
这样的东西,而不是
clients.openWindow(“https://someurl.com“”

请使用此代码实现所需的行为:

// Attempt to extract notification URL
var url = event.notification.data.url;

// Check if it exists
if (url) {
    // Parse target URL
    var targetUrl = new URL(url);

    // Attempt to fetch open tabs
    event.waitUntil(clients.matchAll({ type: 'window' }).then(function (clientList) {
        // No match by default
        var matchFound = false;

        // Traverse clients list
        for (var client of clientList) {
            // Parse url
            var clientUrl = new URL(client.url);

            // Check host matches
            if (clientUrl.host === targetUrl.host && 'focus' in client) {
                // Update URL
                client.navigate(url);

                // Focus existing window
                client.focus();

                // Avoid opening a new window
                noMatch = true;
            }
        }

        // If no open tabs, or none match the host, open a new window
        if (!matchFound) {
            event.waitUntil(clients.openWindow(url));
        }
    }));

请使用此代码实现所需的行为:

// Attempt to extract notification URL
var url = event.notification.data.url;

// Check if it exists
if (url) {
    // Parse target URL
    var targetUrl = new URL(url);

    // Attempt to fetch open tabs
    event.waitUntil(clients.matchAll({ type: 'window' }).then(function (clientList) {
        // No match by default
        var matchFound = false;

        // Traverse clients list
        for (var client of clientList) {
            // Parse url
            var clientUrl = new URL(client.url);

            // Check host matches
            if (clientUrl.host === targetUrl.host && 'focus' in client) {
                // Update URL
                client.navigate(url);

                // Focus existing window
                client.focus();

                // Avoid opening a new window
                noMatch = true;
            }
        }

        // If no open tabs, or none match the host, open a new window
        if (!matchFound) {
            event.waitUntil(clients.openWindow(url));
        }
    }));

您是否试图通过编程方式将焦点设置为特定选项卡?您好@guest271314,感谢您的快速响应。不完全是。我想将焦点设置为特定选项卡,并在该选项卡内设置为特定位置。举个例子。如果我有一个带有“我想专注于该选项卡并重定向到”的选项卡,您是否尝试重定向到片段标识符?我尝试了window.open(“url”),但得到:“ReferenceError:window未定义”。您正在尝试从
ServiceWorker
代码重定向客户端,正确吗?您是否试图通过编程方式将焦点设置为特定选项卡?您好@guest271314感谢您的快速响应。不完全是。我想将焦点设置为特定选项卡,并在该选项卡内设置为特定位置。举个例子。如果我有一个带有“我想关注该选项卡并重定向到”的选项卡,您是否尝试重定向到片段标识符?我尝试了window.open(“url”),但得到:“ReferenceError:window未定义”。您正在尝试从
ServiceWorker
代码重定向客户端,对吗?无论我尝试什么,它仍然会打开一个新窗口。如果我在for循环中尝试console.log,我将找不到要尝试的输出,并查看clientUrl和targetUrl不匹配的原因。@dev jeff请确保发送的JSON通知有效负载对象带有“url”参数({“url”:“}),并且您发送的URL的主机名与您当前在另一个选项卡中打开的网站主机名完全匹配。无论我如何尝试,它仍然会打开一个新窗口。如果我在for循环中尝试console.log,我将找不到要尝试的输出,并查看clientUrl和targetUrl不匹配的原因。@dev jeff请确保发送的JSON通知有效负载对象带有“url”参数({“url”:“}),并且您发送的URL的主机名与当前在另一个选项卡中打开的网站主机名完全匹配。