首页 > javascript > Chrome扩展程序:如何获取新创建的标签网址

Chrome扩展程序:如何获取新创建的标签网址 (Chrome extension: how to get newly created tab url)

问题

在我的扩展/插件后台脚本中,我在尝试使用新标签的网址时遇到问题,它会继续使用'about:blank',如果尚未正确加载,这是公平的。

我的问题是,一旦准备好,你如何获得新创建的标签的网址?

browser.tabs.onCreated.addListener(function(tab) {
    var m_Url = tab.url;
    console.log("m_Url: " + m_Url);
});

解决方法

请改用onUpdated。

查看文档https://developer.chrome.com/extensions/tabs#event-onUpdated

创建选项卡时触发。请注意,此事件触发时可能未设置选项卡的URL,但您可以侦听onUpdated事件,以便在设置URL时收到通知。

因此,在这种情况下,onUpdated事件是适当的事件。

问题

In my extension/addon background script I'm having issues trying to a new tab's url, it keeps coming back with 'about:blank', which is fair enough if it hasn't loaded properly yet.

My question is how do you get the url of a newly created tab as soon as it is ready?

browser.tabs.onCreated.addListener(function(tab) {
    var m_Url = tab.url;
    console.log("m_Url: " + m_Url);
});

解决方法

Use onUpdated instead.

Check the doc https://developer.chrome.com/extensions/tabs#event-onUpdated

Fired when a tab is created. Note that the tab's URL may not be set at the time this event is fired, but you can listen to onUpdated events so as to be notified when a URL is set.

So, onUpdated event is the appropriate event in this case.

相似信息