Cocoa: Delegate for WebView finishing

Mac OSX specific forum
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Cocoa: Delegate for WebView finishing

Post by jamirokwai »

Hi there,

I try to set CSS-things inside a WebGadget. So far, the concept works, but only when I add a delay before applying the new values.
I assume, webviewdidfinishload, is the answer. But I can't get it to work. To use it, you have to add the delegate to the WebGadget, but how!?

Thanks for any help!

This is my code so far:

Code: Select all

ImportC ""
  sel_registerName(str.p-ascii)
  class_addMethod(class, selector, imp, types.p-ascii)
EndImport

Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")

ProcedureC PolicyDecisionMaker(obj, sel, webView, actionInformation, request, frameName, listener)
  Protected URL = CocoaMessage(0, request, "URL")
  Protected URLString.s = PeekS(CocoaMessage(0, CocoaMessage(0, URL, "absoluteString"), "UTF8String"), -1, #PB_UTF8)
  Debug "You clicked: " + urlstring
  CocoaMessage(0, listener, "use")
 ndProcedure

ProcedureC didFinishLoadForFrame(obj, sel, webView, frameName)
  Debug "Yes, I do!"
EndProcedure

class_addMethod(delegateClass, sel_registerName("webView:decidePolicyForNavigationAction:request:frame:decisionListener:"), @PolicyDecisionMaker(), "v@:@@@@@")
class_addMethod(delegateClass, sel_registerName("webView:didFinishLoadForFrame:"), @didFinishLoadForFrame(), "v@:@")

If OpenWindow(0, 270, 200, 600, 300, "WebGadget")
  WebGadget(0, 10, 10, 580, 280, "")
  CocoaMessage(0, GadgetID(0), "setPolicyDelegate:", appDelegate)
  windowScriptObject = CocoaMessage(0, GadgetID(0), "windowScriptObject")
  
  SetGadgetItemText(0,#PB_Web_HtmlCode,"<html><head></head><body><div id='bla' onmouseup='javascript:alert(1);'>Hihi!</div></body></html>")
  
  MessageRequester("!","!")
  
  text.s = "document.body.style.backgroundColor = '#123456';document.getElementById('bla').innerHTML='Haha!';"
  CocoaMessage(0, windowScriptObject, "evaluateWebScript:$", @text)
  
  Repeat
    event = WaitWindowEvent()
    
  Until event = #PB_Event_CloseWindow
EndIf
Regards,
JamiroKwai
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Cocoa: Delegate for WebView finishing

Post by jamirokwai »

Answering my own question... Although a delegate would be better :-)

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  WebGadget(0, 10, 10, 580, 280, "http://www.purebasic.com")
  Repeat
    event = WaitWindowEvent()
    If EventGadget() = 0 And EventType() = #PB_EventType_DownloadEnd
      Debug "Finished Loading..."
    EndIf
  Until event = #PB_Event_CloseWindow
EndIf
Regards,
JamiroKwai
Post Reply