GetGadgetItemText() in WebGadget returns old data...

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

GetGadgetItemText() in WebGadget returns old data...

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

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

Post 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).
quidquid Latine dictum sit altum videtur
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

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

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2079
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

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

Post 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.
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

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

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
User avatar
Kukulkan
Addict
Addict
Posts: 1422
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
Post Reply