Mac Webview didFinishLoadForFrame

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hey guys, im struggling with an new project which I'm working on to automize a load of workflows over website forms. For now I'm doing it with applescript by injecting jQuery into Safari. Now I need to make this userfriendly and all.

I'm known how to inject jquery into an webview. And I found this function:

Code:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
}
This function should kick in when the webview finished loading the frame.

However, it seems to get called for every ?'subframe'?? aswell?
At loading 1 page, it gets called twice. Sometimes 3 till 6 times.

So I cannot rely on this function to wait till an page is fully finished loading. So I can start running certain jQuery funtions.

Does anyone have any idea for me, how I can check for tha page being loaded completly??
 

Anim

macrumors 6502a
Dec 16, 2011
614
22
Macclesfield, UK
Check the sender or your webView for mainFrame

Code:
    if (frame != [sender mainFrame])
	{
        return;
	}
 
Last edited by a moderator:

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Check the sender or your webView for mainFrame

Code:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
   if (frame != [sender mainFrame]) {
      return;
   } else {
      NSLog(@"Page done loading..");
   }
}
Ok this is almost what I'm looking for, but not exactly. It does only gets called once. Only when
Code:
[[[self myWebView] mainFrame] loadRequest:urlRequest];
is being called. But not when I'm manually inside the frame following an link.

Anything I can do about this ?
Looking through
Code:
[myWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
Also issnt an real success cus it will comes up every time at the didFinishLoadForFrame.
 

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
How about creating a counter when it prepares a frame and when it completes a frame, when count is zero, your done. Include a timeout too.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Webkit/Protocols/WebFrameLoadDelegate_Protocol/Reference/Reference.html
I'm not sure I really understand what you mean with that tho.
The state changes functions arent making sense to me. I put in seperate counters for the didFinshLoadForFrame, didStartProvisionalLoadForFrame, didCommitLoadForFrame and didChangeLocationWithinPageForFrame.
But its not making sense to me, none of any counters are equal when an page is fully loaded.
 

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
For now I'm running an timer with an check if the WebView is still loading.

Code:
-(void)checkLoading {
    [self runEscalationsScript];
    if([myWebView estimatedProgress] == 0.00) {
        doneLoading = YES;
    } else {
        doneLoading = NO;
    }
}
 
Last edited:

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Anyone having an good sollution for this?
My sollution up here, is only working when the user has changed page. Not when the app itself changes an page, for example the first load of the webview.

Cheers!