简体   繁体   中英

How can a Cocoa application get notified when a URL is changed/loaded in Safari for OS X?

I know you can retrieve the current URL of the active tab using AppleScript, but rather than poll for changes I'm wondering if there is a way to be notified when a URL has changed in Safari for OS X?

The app wanting to be notified is a non-sandboxed Cocoa app.

Here's an idea. In general Safari doesn't send out any notifications that I know of when a web page changes so I don't think there's a direct way to get notified. However there might be an indirect way.

In ~/Library/Safari there are some files pertaining to the browser history. So when a user goes to a new web page those history files get updated with new information, thus the modification dates of those files changes. As such you can put a watcher on the history files. When they change then you know the website has most likely changed.

So you can find a way to watch Safari's history files rather than polling Safari. I'm pretty sure there are some cocoa methods to watch files and get notified when they change. Off hand I can't recall them but I hope this points you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM