WebKit: Inspecting browser contents

Morning all

I’ve been looking over the documentation here, and struggling with something.

I’ve designed an application that guides a user through setting a process up on a website… I have a window, with two views: one of them the view that guides them, the other is a webView, and it’s all working.

I’m guiding them by looking at what the web page title it:

on webView_didReceiveTitle_forFrame_(theView, theTitle, theFrame)
		-- we will use this to change, dynamically, the text in the menu bar when performing
		tell viewWebKit's |window|() to setTitleWithRepresentedFilename_(theTitle)
		set my URLTitleDetected to (theTitle as text)
		if my URLTitleDetected contains "step 1" then
			set my webKitMenuBarText to "You're at the start"
		else if my URLTitleDetected contains "step 2" then
			set my webKitMenuBarText to "You're at the end"
		end if		
	end webView_didReceiveTitle_forFrame_

You can guess the rest…

Anyway, there’s one particular step of the process where the title doesn’t change. The current URL is useless also.

I’m guessing that this is the answer: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/SaveAndLoad.html#//apple_ref/doc/uid/TP40001472-CJBEHAAG but not sure how to translate it into ASOC

Thanks in advance…

Either you have your links crossed, or you’re barking up the wrong tree. Those methods have been deprecated since 10.4, and their replacements aren’t going to be any help either – they’re about saving documents.

If the title doesn’t change, you’re going to have to look at one of the other WebFrameLoadDelegate methods, probably webView:didFinishLoadForFrame:.

Shane,

since posting my request for help, I’ve made huge progress

I found this post, which appeared relevent:

http://macscripter.net/viewtopic.php?id=9171

I’ve had to make changes from

set the_source to (call method "getWebStringSource:" of class "Methods" with parameter webview) 

to

set bob to methods's getWebStringURL(theFrame)

However, as you say, if the title / url doesn’t change, you’re stuck

So, I’ve placed this in :

on webView_didFinishLoadForFrame_(theView, theFrame)
        --get URL
        set bob to methods's getWebStringURL(theView)
        log bob
       end webView_didStartProvisionalLoadForFrame_

This isn’t going well. I get:

013-10-02 12:47:39.951 Thingy[99862:303] +[methods getWebStringURL]: unrecognized selector sent to class 0x100002290
2013-10-02 12:47:39.955 Thingy[99862:303] *** -[AppDelegate webView:didFinishLoadForFrame:]: +[methods getWebStringURL]: unrecognized selector sent to class 0x100002290 (error -10000)

What IS working is this:


on webView_didStartProvisionalLoadForFrame_(theView, theFrame)
        set my URLPathDetected to absoluteString() of |URL| of request of provisionalDataSource of theFrame
        log my URLPathDetected
end webView_didStartProvisionalLoadForFrame_

I realise the URL is NOT the source, but, for me, it’s a step forward

The problem I have with this is that I can’t coerse this into a string :frowning:

What exactly are you trying to get?

The URL of the current view and the content (text or html)

OK. WebViews have a -mainFrameURL method that should give you the URL. To get the source you’ll probably need to get the WebView’s mainFrame(), get the main frame’s datasource(), and get its data(). Something like (untested):

on webView_didFinishLoadForFrame_(theView, theFrame)
set theData to theView's mainFrame()'s datasource()'s |data|()
set theSource to current application's NSString's alloc()'s initWithData_encoding_(theData, current application's NSUTF8StringEncoding)
...

Didn’t work too well, alas. Errored with the usual “unrecognised selector”

I did get the URL being returned though.

For reference:


on webView_didStartProvisionalLoadForFrame_(theView, theFrame)
        set URLPathDetected to absoluteString() of |URL| of request of provisionalDataSource of theFrame
        set theSource to current application's NSString's alloc()'s initWithString_(URLPathDetected)
        set bob to theSource as text
end webView_didStartProvisionalLoadForFrame_


Damn you case sensitivity!

 datasource

should be

 dataSource