Get URL and page title data from SafariNavigateEvent

 ̄綄美尐妖づ 提交于 2019-12-12 09:47:25

问题


I am debugging something for a colleague who is on holiday and know very little about Safari Extension development.

I have a Safari extension that listens for 'open', 'beforeNavigate', 'navigate' and 'activate' events. When any of these events are fired I want to track the activeTab's url and page title.

It seems however that in certain instances (namely beforeNavigate and navigate when a new tab is opened) the page title isn't always defined in either the safari.application.activeBrowserWindow.activeTab object or the event object passed to my handlers.

When I print the event object out to the console it is populated correctly, but if I access event['target']['title'] directly in an event handler it comes back Untitled. I would imagine then that there is a delay in populating the data, but I can't figure out how to handle the delay nor can I find any documentation on it.

FYI I have the website access set to ALL in my info.plist.

Has anyone run into this problem? Any thoughts on how to fix it?

He's a snippet of code:

safari.application.addEventListener("beforeNavigate", function (event) {
    console.log('//-- Event Data -------------------------------------');
    console.log(event);
    console.log(event['target']);
    console.log('Url: ' + event['target']['url']);
    console.log('Title: ' + event['target']['title']);

    console.log('//-- Tab Data -------------------------------------');
    console.log('Url: ' + safari.application.activeBrowserWindow.activeTab.url);
    console.log('Title: ' + safari.application.activeBrowserWindow.activeTab.title);
}, true);

And the output to the console:

//-- Event Data -------------------------------------
SafariBeforeNavigateEvent
    BUBBLING_PHASE: 3
    CAPTURING_PHASE: 1
    TARGETING_PHASE: 2
    bubbles: true
    cancelable: true
    currentTarget: null
    defaultPrevented: false
    eventPhase: 0
    target: SafariBrowserTab
        browserWindow: SafariBrowserWindow
        page: SafariWebPageProxy
        reader: SafariReader
        title: "Google"
        url: "https://www.google.ca/"
        __proto__: CallbackObject
    timeStamp: 1379353767889
    type: "beforeNavigate"
    url: "http://www.google.ca/"
    __proto__: CallbackObject

SafariBrowserTab
    browserWindow: SafariBrowserWindow
    page: SafariWebPageProxy
    reader: SafariReader
    title: "Google"
    url: "https://www.google.ca/"
    __proto__: CallbackObject
Url:
Title: Untitled
//-- Tab Data -------------------------------------
Url: 
Title: Untitled

回答1:


I would guess that the problem is that the title is actually defined within the page HTML. Since this is a beforeNavigate event, the page hasn't been loaded yet so in general the title isn't available yet, just the URL.

One solution might be to use the navigate event instead of beforeNavigate, provided you don't specifically need to do anything before the page actually loads. However I'm not sure that actually guarantees the page will be loaded, but it does make it more likely.

If it's still a problem, a more complicated alternative is to use an injected script that sends a message to the global page whenever a page loads. Use the DOM ready event in the injected script, then just dispatch a message to the global page containing the page title. Use if (window == window.top) to prevent embedded iframes from also sending a message.



来源:https://stackoverflow.com/questions/18834362/get-url-and-page-title-data-from-safarinavigateevent

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