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