Page 1 of 1

Re: 5 WebGadget functions implemented for Linux and MacOS

Posted: Mon Jul 23, 2018 4:37 pm
by Shardik
wilbert wrote:On MacOS, this works fine.
It's only the documentation that's wrong.
How could I have overlooked that GetGadgetItemText() with #PB_Web_HtmlCode, #PB_Web_PageTitle and #PB_Web_SelectedText is already implemented in PureBasic for Mac, especially after having found out that SetGadgetItemText(#WebGadget, #PB_Web_HtmlCode, HTML$) is working fine as you can see in my usage of CompilerDefault in "Case 5" (for MacOS and Windows) in my 2nd example above...:oops:

But there still seems to be an error in the MacOS implementation of GetGadgetItemText(#WebGadget, #PB_Web_HtmlCode) when reading a real website. Your example is working fine but try to replace

Code: Select all

WebGadget(0, 10, 10, 580, 240, "")
ButtonGadget(1, 10, 260, 200, 30, "GetGadgetItemText")

SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><title>Test</title><body><p>This is a <i>simple</i> test</body></html>")
with

Code: Select all

WebGadget(0, 10, 10, 580, 240, "http://www.purebasic.com")
ButtonGadget(1, 10, 260, 200, 30, "GetGadgetItemText")

; SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><title>Test</title><body><p>This is a <i>simple</i> test</body></html>")
Reading out title and a selected text works fine but reading out the complete HTML doesn't work.

I have now modified my 2nd example from above to use CompilerDefault for MacOS and Windows when reading out title and selected text. I have left my 2nd example from above unchanged for those who want to know a possible technique for reading out title and selected text.

Code: Select all

EnableExplicit

#HTML = "<h1>Finally you can use the most important WebGadget functions " +
  "cross-platform ;-)</h1>"
#URL = "http://www.purebasic.com"

Define SelectedText.S
Define Title.S

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    CompilerIf Subsystem("gtk2")
      ImportC "-lwebkitgtk-1.0"
    CompilerElse
      ImportC "-lwebkitgtk-3.0"
    CompilerEndIf
      webkit_web_data_source_get_data(*WebDataSource)
      webkit_web_frame_get_data_source(*WebFrame)
      webkit_web_view_can_copy_clipboard(*WebView)
      webkit_web_view_copy_clipboard(*WebView)
      webkit_web_view_get_main_frame(*WebView)
      webkit_web_view_get_title(*WebView)
      webkit_web_view_has_selection(*WebView)
      webkit_web_view_load_string(*WebView, Content.P-UTF8, *MimeType,
        *Encoding, *BaseURI)
    EndImport
      
    Define *Title
    Define WebData.I
    Define WebFrame.I
    Define *WebPageContent.GString
  CompilerCase #PB_OS_MacOS
    Define *HTML
    Define JavaScriptString.S
CompilerEndSelect

OpenWindow(0, 100, 100, 590, 600, "Cross-platform WebGadget functions")

FrameGadget(0, 10, 10, WindowWidth(0) - 20, 65, "GetGadgetItemText()")
ButtonGadget(1, 20, 30, 160, 25, "#PB_Web_HTMLCode")
ButtonGadget(2, 215, 30, 160, 25, "#PB_Web_PageTitle")
ButtonGadget(3, 410, 30, 160, 25, "#PB_Web_SelectedText")

FrameGadget(4, 10, GadgetY(0) + GadgetHeight(0) + 20, WindowWidth(0) - 20, 65,
  "SetGadgetItemText()")
ButtonGadget(5, 220, GadgetY(4) + 20, 150, 25, "#PB_Web_HTMLCode")

WebGadget(6, 10, GadgetY(4) + GadgetHeight(4) + 15, WindowWidth(0) - 20,
  WindowHeight(0) - GadgetHeight(0) - 120, #URL)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Debug GetGadgetText(1) + ":"

          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Linux
              WebFrame = webkit_web_view_get_main_frame(GadgetID(6)) 
              WebData = webkit_web_frame_get_data_source(WebFrame)
              *WebPageContent = webkit_web_data_source_get_data(WebData)
              Debug PeekS(*WebPageContent\str, -1, #PB_Ascii)
            CompilerCase #PB_OS_MacOS
              JavaScriptString = "document.documentElement.outerHTML;"
              *HTML = CocoaMessage(0, GadgetID(6),
                "stringByEvaluatingJavaScriptFromString:$", @JavaScriptString)
              Debug PeekS(CocoaMessage(0, *HTML, "UTF8String"), -1, #PB_UTF8)
            CompilerCase #PB_OS_Windows
              Debug GetGadgetItemText(6, #PB_Web_HtmlCode)
          CompilerEndSelect

          Debug ""
        Case 2
          Title = ""
          Debug GetGadgetText(2) + ":"

          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Linux
              *Title = webkit_web_view_get_title(GadgetID(6))

              If *Title
                Title = PeekS(*Title, -1, #PB_UTF8)
              EndIf
            CompilerDefault
              Title = GetGadgetItemText(6, #PB_Web_PageTitle)
          CompilerEndSelect

          If Title = ""
            Title = "No title found!"
          EndIf

          Debug Title
          Debug ""
        Case 3
          SelectedText = ""
          Debug GetGadgetText(3) + ":"

          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Linux
              If webkit_web_view_has_selection(GadgetID(6))
                If webkit_web_view_can_copy_clipboard(GadgetID(6)) = #False
                  SelectedText = "The copying of selected text to the " +
                    "clipboard failed!"
                Else
                  webkit_web_view_copy_clipboard(GadgetID(6))
                  SelectedText = GetClipboardText()
                EndIf
              EndIf 
            CompilerDefault
              SelectedText = GetGadgetItemText(6, #PB_Web_SelectedText)
          CompilerEndSelect

          If SelectedText = ""
            SelectedText = "No text is selected!"
          EndIf

          Debug SelectedText
          Debug ""
        Case 5
          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Linux
              webkit_web_view_load_string(GadgetID(6), #HTML, 0, 0, 0)
            CompilerDefault
              SetGadgetItemText(6, #PB_Web_HtmlCode, #HTML)
          CompilerEndSelect
      EndSelect 
  EndSelect
ForEver

Re: 5 WebGadget functions implemented for Linux and MacOS

Posted: Mon Jul 23, 2018 4:47 pm
by wilbert
Shardik wrote:But there still seems to be an error in the MacOS implementation of GetGadgetItemText(#WebGadget, #PB_Web_HtmlCode) when reading a real website.
Very strange.
The url of the PB website you are mentioning doesn't show a source.
If you try the url of the PB forum, SpiderBasic, CNN or Google website, it does show source code. :?

Re: 5 WebGadget functions implemented for Linux and MacOS

Posted: Mon Jul 23, 2018 9:19 pm
by Shardik
I have tested wilbert's example also on older PB versions and it's always working like a charm:
- PB 4.51 x86 (Carbon framework)
- PB 4.61 x86 (Carbon framework)
- PB 5.00 x86 (default Cocoa framework and subsystem "Carbon")

So the documentation of the implemented WebGadget functions for the MacOS version is wrong since many years. I have therefore posted this problem in the "Bugs - Documentation" subforum.

Re: 5 WebGadget functions implemented for Linux and MacOS

Posted: Mon Jul 23, 2018 9:50 pm
by Shardik
the.weavster wrote:Do you know of a way calling PB procedures from JavaScript?
For which operating system? Until now I only have seen a Windows solution posted by waldschrath in this thread.

Re: 5 WebGadget functions implemented for Linux and MacOS

Posted: Sun Jul 29, 2018 10:28 pm
by wombats
It seems #PB_Web_HtmlCode, #PB_Web_SelectedText and #PB_Web_PageTitle are implemented for the Qt SubSystem on Linux.

I thought it would be possible to do a NavigationCallback with the QtScript command, but I'm having no luck. It would be great if it worked under Qt, too.

Code: Select all

Runtime Procedure OnWebGadgetClick()
  MessageRequester("", "Click!")
EndProcedure  

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(0, 10, 10, 580, 280, "http://www.purebasic.com") 
  
  QtScript(~"gadget(\"0\").page.LinkDelegationPolicy = 2") ; http://doc.qt.io/archives/qt-4.8/qwebpage.html#LinkDelegationPolicy-enum
  QtScript(~"gadget(\"0\").clicked.connect(function() { Runtime.call(\"OnWebGadgetClick()\"); })")

  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf