/How to get active Tab url in safari browser using java script

烂漫一生 提交于 2020-01-23 14:02:29

问题


How to get the browser window or active tab url using java script in a safari browser ?

I tried to do it using ->

document.url

window.location.href

But both this solutions are giving me so many urls . I dont know why ?

Also I have searched that safari.application.browserWindow.activeTab gives obj of current active window . But I dont know which methods are provided by activeTab class

If anyone knows , plz help me . Thanks in advance.


回答1:


If you just want the URL of the "top" document in the current tab, you can use

safari.application.activeBrowserWindow.activeTab.url

from within your extension's global page.

The properties and methods of the SafariBrowserTab class are given here: https://developer.apple.com/library/safari/#documentation/UserExperience/Reference/SBrowserTabClassReference/SafariBrowserTab.html#//apple_ref/doc/uid/TP40009793

I infer from your question that your extension is using an injected script, which is reporting the URL of the page in which it is being run to the global page. Your global page is receiving multiple URLs because the injected script is running not only in the "top" document of the tab, but also in every iframed document inside it.

If you want to prevent the injected script from running inside iframes, wrap the code in a statement like this:

if (window == window.top) {
    // do stuff
}


来源:https://stackoverflow.com/questions/12130070/how-to-get-active-tab-url-in-safari-browser-using-java-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!