如何检测新浏览器选项卡中的 url 更改并取回 url?

Yir*_*ong 5 javascript knockout.js typescript

我有一个链接,可以在新选项卡中打开一个网址,如下所示:

<a href="#" target="teamViewer" style="color: deepskyblue"><span data-bind="text: connectorLoginLink, visible: shouldShowConnectorLoginLink, click: openTeamViewerUrl"></span></a>
Run Code Online (Sandbox Code Playgroud)

由于我想检测新选项卡中的 URL 更改,因此我认为必须使用 window.open(url) 来打开此链接。然后,跟踪新窗口。

在视图模型中:

public openTeamViewerUrl() {
    var newWindow = window.open('http://www.google.com', '_blank', 'location=yes');
}
Run Code Online (Sandbox Code Playgroud)

我想将打开的选项卡设置为 newWindow 对象,然后使用如下方式监视它:

var currentPage = newWindow.location.href;

    setInterval(function () {
        console.log(currentPage);
        if (currentPage !== newWindow.location.href) {
            // page has changed, set new page as 'current'
            currentPage = newWindow.location.href;

            console.log(currentPage);
            // do other thing...
        }
    }, 1000);
Run Code Online (Sandbox Code Playgroud)

我在尝试 window.open 时收到错误:阻止在新窗口中打开“ http://www.google.com/ ”,因为该请求是在未设置“allow-popups”权限的沙盒框架中发出的。

真的被困在这件事上了。如何绕过允许弹出窗口?这个功能可以通过其他方法实现吗?

GôT*_*ôTô 2

要检测 URL 的更改,您可以unload在 上使用 javascript 事件window,请参阅https://developer.mozilla.org/en-US/docs/Web/Events/unload

window.addEventListener('unload', function(event) {
    console.log('Navigation occuring');
});
Run Code Online (Sandbox Code Playgroud)