Page 1 of 1

GetGadgetItemText() in WebGadget returns old data...

Posted: Thu Sep 11, 2014 4:19 pm
by Kukulkan
Hi,

I'm loading some html and JS into the WebGadget. The JS now changes the HTML content of a textarea. But the function GetGadgetItemText() returns the original code and not the one changed by using JS. Or did I do it wrong?

Code: Select all

If OpenWindow(0, 0, 0, 800, 600, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(0, 10, 10, 780, 580, "") 
  
  html.s = "<html><body>"+
           "<textarea id='editor'></textarea><br>"+
           "<input type='button' value='test' onclick=" + Chr(34) + 
           "document.getElementById('editor').value='something';" + Chr(34) + ">"+
           "</body></html>"
  
  SetGadgetItemText(0, #PB_Web_HtmlCode, html.s)
  
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
  
  newhtml.s = GetGadgetItemText(0, #PB_Web_HtmlCode)
  
  Debug html.s
  
EndIf
Run this. The textarea is empty. Now click on the button. The textarea gets the value 'something'. Now close the window. The debug window should output the new HTML including the value of the textarea. But textarea is still empty :-( It does not output the HTML source of the current browser. It is the old value...

Question in another direction: How do I get the changed content out of the WebGadget without using API?

Kukulkan

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Thu Sep 11, 2014 6:41 pm
by freak
Why should the html change just because the browser content changed?

Before you close the window, do a right-click and select "Show source". It will also show the unchanged html code like the debug output does after closing the window. So the output is correct in my opinion.

If you want to react to events in the browser window, the best way is to navigate to a different url using JavaScript and then using a navigation callback to catch that.

Another way would be to communicate directly with the JavaScript interpreter of the WebGadget, but that requires some API (code can be found here on the forums).

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Thu Sep 11, 2014 6:54 pm
by Danilo
freak wrote:the best way is to navigate to a different url using JavaScript and then using a navigation callback to catch that.
Could you add the navigation callback and other possibilities to all OS, please? 90% of WebGadget functionality is Windows-only.

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Thu Sep 11, 2014 7:34 pm
by Shardik
Danilo wrote:Could you add the navigation callback and other possibilities to all OS, please? 90% of WebGadget functionality is Windows-only.
+1

How to implement the navigation callback for Linux and MacOS I have already demonstrated in this feature request.

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Fri Sep 12, 2014 7:14 am
by Kukulkan
I can understand this point of view. But I try to get around some missing HTML editor with basic formatting functionality by using the WebGadget with CKEditor. But I need it cross-platform and it turns out to be a beast. Missing key-input for copy&paste, delete etc. Can not get the result out of it. Problems with Same Origin Policy. Everything can get solved, maybe, but different solutions for each platform and I'm wondering what happens in the next OS version, PB version etc. I suppose that I have to worry about that.

[EDIT]I do not see how navigation callback handling would solve my problem. I need the typed content if the user clicks a button in the window. How to force a POST in the WebGadget if I need to know the entered text? Sounds like a weird workaround to me.[/EDIT]

I think this is not a good way to go. Any other ideas for getting some WYSIWYG editor? I know this is discussed multiple times in the forum but there was no real cross-platform solution mentioned, I think. RTF to HTML seems a crap and most other solutions are not cross-platform. Isn't there a really good solution to get Bold, Italics, Underline, Lists, Alignment and some Colour in a WYSIWYG editor in PB? I start to become desperate on this :-(

Kukulkan

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Fri Sep 12, 2014 11:27 am
by wilbert
OSX only

Code: Select all

Procedure ButtonHandler()
  
  EditorData.i = CocoaMessage(0, GadgetID(0), "stringByEvaluatingJavaScriptFromString:$", @"CKEDITOR.instances.editor1.getData()")
  MessageRequester("Editor data", PeekS(CocoaMessage(0, EditorData, "UTF8String"), -1, #PB_UTF8))
  
EndProcedure

If OpenWindow(0, 0, 0, 800, 600, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  WebGadget(0, 10, 50, 780, 540, "http://ckeditor.com/demo")
  ButtonGadget(1, 10, 10, 120, 30, "Show data")
  BindGadgetEvent(1, @ButtonHandler())
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
  
EndIf

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Fri Sep 12, 2014 11:45 am
by Kukulkan
Thanks Wilbert,

this is short and great! Need the same short solution for Windows but some SetDOMElement() and GetDOMElement() and maybe RunJSFunction() would be great for the Webgadget...

Thanks again,

Kukulkan

Re: GetGadgetItemText() in WebGadget returns old data...

Posted: Fri Sep 12, 2014 2:57 pm
by Kukulkan
Got it on Windows using COM object:

Code: Select all

Protected Content.s = ""
Protected browser.COMateObject, documentDispatch.COMateObject, script.COMateObject
Protected result
browser = COMate_WrapCOMObject(GetWindowLongPtr_(GadgetID(GadID), #GWL_USERDATA))
If browser
  documentDispatch = browser\GetObjectProperty("Document")
  If documentDispatch
    script = documentDispatch\GetObjectProperty("script")
    If script
      result = script\Invoke("ExecScript('var content = CKEDITOR.instances.editor1.getData()', 'JavaScript')")
      Content.s = script\GetStringProperty("content")
      script\release()
    EndIf 
    documentDispatch\Release()
  EndIf
  browser\Release()
EndIf 

Debug Content.s
So I get the result on Windows and MacOS. This is my priority number one so far. Thanks!

Kukulkan