WebKitGadget GTK3 (libwebkit2gtk-4.0)

Linux specific forum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by mk-soft »

Since PB's WebGadget has not worked with the new versions of Linux for a long time, I started to write a new WebKitGadget.

It's not quite finished yet, but already fully functional

Update v1.01.1
- Change hit test

Update v1.02.1
- Added WebKit_LoadHTML
- Added WebKit_GetData ; Begin get resource data asynchronous. Sends an event to the webkit gadget when get data is finished
- Added Webkit_GetDataHTML; Call the function when get data is finished

MyWebKitGadget.pb

Code: Select all

;-TOP

; Comment : WebKitGadget (GTK3)
; Library : libwebkit2gtk-4.0
; Author  : mk-soft
; File    : MyWebKitGadget.pb
; Version : v1.02.1 (Beta)
; Create  : 19.11.2021
; Update  : 24.11.2021

; ---------------------

; Linux Libraries (deb)

; sudo apt-get install libwebkit2gtk-4.0-37
; sudo apt-get install libwebkit2gtk-4.0-dev

; Syntax: WebKit_HitTestCallback(Gadget, Type, Text.s)

; ********************* 

DeclareModule WebKit2
  
  Enumeration
    #WEBKIT_LOAD_STARTED
    #WEBKIT_LOAD_REDIRECTED
    #WEBKIT_LOAD_COMMITTED
    #WEBKIT_LOAD_FINISHED
  EndEnumeration
  
  Enumeration
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT  = 1 << 1
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK      = 1 << 2
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE     = 1 << 3
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA     = 1 << 4
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE  = 1 << 5
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR = 1 << 6
    #WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION = 1 << 7
  EndEnumeration
  
  Prototype WebKit_HitTestCallback(Gadget, Type, Text.s)
  
  Declare InitWebKit2(LibName.s = "libwebkit2gtk-4.0.so")
  
  Declare WebKitGadget(Gadget, x, y, Width, Height, url.s, Flags = 0)
  Declare FreeWebKitGadget(Gadget)
  
  Declare WebKit_LoadURI(Gadget, URI.s)
  Declare WebKit_LoadHTML(Gadget, content.s, base_uri.s = "localhost")
  
  Declare.s WebKit_GetURI(Gadget)
  Declare.s WebKit_GetTitle(Gadget)
  Declare.i WebKit_GetData(Gadget, Event = #PB_EventType_FirstCustomValue)
  Declare.s WebKit_GetDataHTML(Gadget)
  
  Declare WebKit_GoBack(Gadget)
  Declare WebKit_GoForward(Gadget)
  Declare WebKit_Reload(Gadget)
  Declare WebKit_StopLoading(Gadget)
  
  Declare WebKit_SetHitTestCallback(Gadget, *Callback.WebKit_HitTestCallback)
  
  ; ----
  
EndDeclareModule

Module Webkit2
  
  EnableExplicit
  
  Structure udtWebKitGadget
    ; Widgets
    Container.i
    Scroller.i
    WebView.i
    ; Signal handle
    signal_load_changed.i
    signal_mouse_target_changed.i
    ; User Callback
    *HitTestCallback.WebKit_HitTestCallback
    ; Get Webkit Data
    *Resource
    Event.i
    Text.s
  EndStructure
  
  PrototypeC webkit_web_view_new()
  PrototypeC webkit_web_view_load_uri(*web_view, url.p-utf8)
  PrototypeC webkit_web_view_load_html(*web_view, content.p-utf8, base_uri.p-utf8);
  PrototypeC webkit_web_view_get_uri(*web_view)
  PrototypeC webkit_web_view_get_title(*web_view)
  
  PrototypeC webkit_web_view_can_go_back(*web_view)
  PrototypeC webkit_web_view_go_back(*web_view)
  PrototypeC webkit_web_view_can_go_forward(*web_view)
  PrototypeC webkit_web_view_go_forward(*web_view)
  
  PrototypeC webkit_web_view_reload(*web_view)
  PrototypeC webkit_web_view_stop_loading(*web_view)
  
  PrototypeC webkit_hit_test_result_get_context(*hit_test_result)
  
  PrototypeC webkit_hit_test_result_context_is_link(*hit_test_result)
  PrototypeC webkit_hit_test_result_context_is_image(*hit_test_result)
  PrototypeC webkit_hit_test_result_context_is_media(*hit_test_result)
  PrototypeC webkit_hit_test_result_context_is_editable(*hit_test_result)
  PrototypeC webkit_hit_test_result_context_is_selection(*hit_test_result)
  PrototypeC webkit_hit_test_result_context_is_scrollbar(*hit_test_result)
  
  PrototypeC webkit_hit_test_result_get_link_uri(*hit_test_result)
  PrototypeC webkit_hit_test_result_get_link_title(*hit_test_result)
  PrototypeC webkit_hit_test_result_get_link_label(*hit_test_result)
  PrototypeC webkit_hit_test_result_get_image_uri(*hit_test_result)
  PrototypeC webkit_hit_test_result_get_media_uri(*hit_test_result)
  
  PrototypeC webkit_web_view_get_main_resource(*web_view) ; Result WebKitWebResource
  PrototypeC webkit_web_resource_get_data(*resource, *cancellable, *callback, user_data)
  PrototypeC webkit_web_resource_get_data_finish(*resource, *result, *length, *p_error);
  
  PrototypeC g_simple_async_result_new(*source_object, *callback, user_data, source_tag)
  Global IsWebkit2
  
  ; ----
  
  Procedure DoEvent_ResizeWebKitGadget()
    Protected Gadget = EventGadget()
    Protected *GadgetData.udtWebKitGadget = GetGadgetData(Gadget)
    
    With *GadgetData
      If *GadgetData
        gtk_widget_set_size_request_(\Scroller, GadgetWidth(\Container), GadgetHeight(\Container))
      EndIf
    EndWith
  EndProcedure
  
  ; ----
  
  Procedure InitWebKit2(LibName.s = "libwebkit2gtk-4.0.so")
    Protected lib
    
    lib = OpenLibrary(#PB_Any, libname)
    If lib
      Global webkit_web_view_new.webkit_web_view_new = GetFunction(lib, "webkit_web_view_new")
      Global webkit_web_view_load_uri.webkit_web_view_load_uri = GetFunction(lib, "webkit_web_view_load_uri")
      Global webkit_web_view_load_html.webkit_web_view_load_html = GetFunction(lib, "webkit_web_view_load_html")
      Global webkit_web_view_get_uri.webkit_web_view_get_uri = GetFunction(lib, "webkit_web_view_get_uri")
      Global webkit_web_view_get_title.webkit_web_view_get_title = GetFunction(lib, "webkit_web_view_get_title")
      
      Global webkit_hit_test_result_get_context.webkit_hit_test_result_get_context = GetFunction(lib, "webkit_hit_test_result_get_context")
      
      Global webkit_hit_test_result_context_is_link.webkit_hit_test_result_context_is_link = GetFunction(lib, "webkit_hit_test_result_context_is_link")
      Global webkit_hit_test_result_context_is_image.webkit_hit_test_result_context_is_image = GetFunction(lib, "webkit_hit_test_result_context_is_image")
      Global webkit_hit_test_result_context_is_media.webkit_hit_test_result_context_is_media = GetFunction(lib, "webkit_hit_test_result_context_is_media")
      Global webkit_hit_test_result_context_is_editable.webkit_hit_test_result_context_is_editable = GetFunction(lib, "webkit_hit_test_result_context_is_editable")
      Global webkit_hit_test_result_context_is_selection.webkit_hit_test_result_context_is_selection = GetFunction(lib, "webkit_hit_test_result_context_is_selection")
      Global webkit_hit_test_result_context_is_scrollbar.webkit_hit_test_result_context_is_scrollbar = GetFunction(lib, "webkit_hit_test_result_context_is_scrollbar")
      
      Global webkit_hit_test_result_get_link_uri.webkit_hit_test_result_get_link_uri = GetFunction(lib, "webkit_hit_test_result_get_link_uri")
      Global webkit_hit_test_result_get_link_title.webkit_hit_test_result_get_link_title = GetFunction(lib, "webkit_hit_test_result_get_link_title")
      Global webkit_hit_test_result_get_link_label.webkit_hit_test_result_get_link_label = GetFunction(lib, "webkit_hit_test_result_get_link_label")
      Global webkit_hit_test_result_get_image_uri.webkit_hit_test_result_get_image_uri = GetFunction(lib, "webkit_hit_test_result_get_image_uri")
      Global webkit_hit_test_result_get_media_uri.webkit_hit_test_result_get_media_uri = GetFunction(lib, "webkit_hit_test_result_get_media_uri")
      
      
      Global webkit_web_view_can_go_back.webkit_web_view_can_go_back = GetFunction(lib, "webkit_web_view_can_go_back")
      Global webkit_web_view_go_back.webkit_web_view_go_back = GetFunction(lib, "webkit_web_view_go_back")
      Global webkit_web_view_can_go_forward.webkit_web_view_can_go_forward = GetFunction(lib, "webkit_web_view_can_go_forward")
      Global webkit_web_view_go_forward.webkit_web_view_go_forward = GetFunction(lib, "webkit_web_view_go_forward")
      
      Global webkit_web_view_reload.webkit_web_view_reload = GetFunction(lib, "webkit_web_view_reload")
      Global webkit_web_view_stop_loading.webkit_web_view_stop_loading = GetFunction(lib, "webkit_web_view_stop_loading")
      
      Global webkit_web_view_get_main_resource.webkit_web_view_get_main_resource = GetFunction(lib, "webkit_web_view_get_main_resource")
      Global webkit_web_resource_get_data.webkit_web_resource_get_data = GetFunction(lib, "webkit_web_resource_get_data")
      Global webkit_web_resource_get_data_finish.webkit_web_resource_get_data_finish = GetFunction(lib, "webkit_web_resource_get_data_finish")
      
      Global g_simple_async_result_new.g_simple_async_result_new =GetFunction(lib, "g_simple_async_result_new")
      
      IsWebkit2 = #True
      ProcedureReturn #True
    Else
      IsWebkit2 = #False
      ProcedureReturn #False
    EndIf
  EndProcedure
  
  ; ----
  
  ProcedureC WebKit_Load_Changed_CB(*web_view,load_event, user_data)
    
    PostEvent(#PB_Event_Gadget, 0, user_data, #PB_EventType_Change, load_event)
    
  EndProcedure
  
  ProcedureC WebKit_Mouse_Target_Changed_CB(*web_view, *hit_test_result, modifiers, *GadgetData.udtWebKitGadget)
    Protected gint, *gchar, text.s
    gint = webkit_hit_test_result_get_context(*hit_test_result)
    With *GadgetData
      If *GadgetData
        If \HitTestCallback
          If gint & #WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK
            *gchar = webkit_hit_test_result_get_link_uri(*hit_test_result)
            text = PeekS(*gchar, -1, #PB_UTF8)
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK, text)
          EndIf
          If gint & #WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE
            *gchar = webkit_hit_test_result_get_image_uri(*hit_test_result)
            text = PeekS(*gchar, -1, #PB_UTF8)
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE, text)
          EndIf
          If gint & #WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA
            *gchar = webkit_hit_test_result_get_media_uri(*hit_test_result)
            text = PeekS(*gchar, -1, #PB_UTF8)
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA, text)
          EndIf
          If gint & #WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR, "")
          EndIf
          If gint & #WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION, "")
          EndIf
          If gint = #WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT
            \HitTestCallback(\Container, #WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT, "")
          EndIf
          
        EndIf
      EndIf
    EndWith
  EndProcedure
  
  ; ----
  
  Procedure WebKitGadget(Gadget, x, y, Width, Height, url.s, Flags = 0)
    Protected r1, id, *GadgetData.udtWebKitGadget, cancel
    
    If Not IsWebkit2
      ProcedureReturn 0
    EndIf
    
    id = ContainerGadget(Gadget, x, y, Width, Height, Flags)
    If Not id
      ProcedureReturn 0
    EndIf
    
    If Gadget = #PB_Any
      Gadget = id
    EndIf
    
    *GadgetData = AllocateStructure(udtWebKitGadget)
    If Not *GadgetData
      FreeGadget(Gadget)
      ProcedureReturn 0
    EndIf
    
    With *GadgetData
      \Container = Gadget
      \Scroller = gtk_scrolled_window_new_(#Null, #Null)
      If \Scroller
        gtk_scrolled_window_set_policy_(\Scroller, #GTK_POLICY_AUTOMATIC, #GTK_POLICY_AUTOMATIC)
        gtk_widget_set_size_request_(\Scroller, GadgetWidth(\Container), GadgetHeight(\Container))
        \WebView  = webkit_web_view_new()
        If \WebView
          gtk_container_add_(\Scroller, \WebView)
          gtk_container_add_(GadgetID(\Container), \Scroller)
          gtk_widget_show_(\Scroller)
          gtk_widget_show_(\WebView)
          
          If url
            r1 = webkit_web_view_load_uri(\WebView, url)
          EndIf
        Else
          cancel = #True
        EndIf
        SetGadgetData(Gadget, *GadgetData)
        
        ; Bind Signals
        \signal_load_changed = g_signal_connect_(\WebView, "load-changed", @WebKit_Load_Changed_CB(), Gadget)
        \signal_mouse_target_changed = g_signal_connect_(\WebView, "mouse-target-changed", @WebKit_Mouse_Target_Changed_CB(), *GadgetData)
        
        ; Bind Events
        BindGadgetEvent(Gadget, @DoEvent_ResizeWebKitGadget(), #PB_EventType_Resize)
        
      Else
        cancel = #True
      EndIf
      
    EndWith
    
    If cancel
      FreeGadget(Gadget)
      FreeStructure(*GadgetData)
      ProcedureReturn 0
    EndIf
    
    ProcedureReturn id
    
  EndProcedure
  
  ; ----
  
  Procedure FreeWebKitGadget(Gadget)
    Protected *GadgetData.udtWebKitGadget
    
    With *GadgetData
      If IsGadget(Gadget)
        *GadgetData = GetGadgetData(Gadget)
        If *GadgetData
          ; Unbind Events
          UnbindGadgetEvent(Gadget, @DoEvent_ResizeWebKitGadget())
          ; Unbind Signals
          If g_signal_handler_is_connected_(\WebView, \signal_load_changed)
            g_signal_handler_disconnect_(\WebView, \signal_load_changed)
          EndIf
          If g_signal_handler_is_connected_(\WebView, \signal_mouse_target_changed)
            g_signal_handler_disconnect_(\WebView, \signal_mouse_target_changed)
          EndIf
          FreeStructure(*GadgetData)
        EndIf
        FreeGadget(Gadget)
      EndIf
    EndWith
  EndProcedure
  
  ; ----
  
  Procedure WebKit_LoadURI(Gadget, URI.s)
    Protected r1, *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        r1 = webkit_web_view_load_uri(*GadgetData\WebView, URI)
      EndIf
    EndIf
    ProcedureReturn r1
  EndProcedure
  
  ; ----
  
  Procedure WebKit_LoadHTML(Gadget, Content.s, Base_URI.s = "localhost")
    Protected r1, *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        r1 = webkit_web_view_load_html(*GadgetData\WebView, Content, Base_URI)
      EndIf
    EndIf
    ProcedureReturn r1
  EndProcedure
  
  ; ----
  
  Procedure.s WebKit_GetURI(Gadget)
    Protected s1.s, *gchar, *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        *gchar = webkit_web_view_get_uri(*GadgetData\WebView)
        If *gchar
          s1 = PeekS(*gchar, -1, #PB_Ascii)
        EndIf
      EndIf
    EndIf
    ProcedureReturn s1
  EndProcedure
  
  ; ----
  
  Procedure.s WebKit_GetTitle(Gadget)
    Protected s1.s, *gchar, *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        *gchar = webkit_web_view_get_title(*GadgetData\WebView)
        If *gchar
          s1 = PeekS(*gchar, -1, #PB_Ascii)
        EndIf
      EndIf
    EndIf
    ProcedureReturn s1
  EndProcedure
  
  ; ----
  
  Procedure WebKit_GoBack(Gadget)
    Protected *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        If webkit_web_view_can_go_back(*GadgetData\WebView)
          webkit_web_view_go_back(*GadgetData\WebView)
        EndIf
      EndIf
    EndIf
  EndProcedure
  
  ; ----
  
  Procedure WebKit_GoForward(Gadget)
    Protected *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        If webkit_web_view_can_go_forward(*GadgetData\WebView)
          webkit_web_view_go_forward(*GadgetData\WebView)
        EndIf
      EndIf
    EndIf
  EndProcedure
  
  ; ----
  
  Procedure WebKit_Reload(Gadget)
    Protected *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        webkit_web_view_reload(*GadgetData\WebView)
      EndIf
    EndIf
  EndProcedure
  
  ; ----
  
  Procedure WebKit_StopLoading(Gadget)
    Protected *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        webkit_web_view_stop_loading(*GadgetData\WebView)
      EndIf
    EndIf
  EndProcedure
  
  ; ----
  
  Procedure WebKit_SetHitTestCallback(Gadget, *Callback.WebKit_HitTestCallback)
    Protected *GadgetData.udtWebKitGadget
    
    If IsGadget(Gadget)
      *GadgetData = GetGadgetData(Gadget)
      If *GadgetData
        *GadgetData\HitTestCallback = *Callback
      EndIf
    EndIf
  EndProcedure
  
  ; ----
  
  Procedure _get_response_data_finish(*resource, *result, *GadgetData.udtWebKitGadget)
    Protected *guchar, length.integer
    With *GadgetData
      *guchar = webkit_web_resource_get_data_finish(*resource, *result, @length, 0)
      If *guchar
        \Text = PeekS(*guchar, length\i, #PB_UTF8)
        PostEvent(#PB_Event_Gadget, 0, \Container, \Event)
        g_free_(*guchar)
      EndIf
    EndWith
  EndProcedure
  
  Procedure WebKit_GetData(Gadget, Event = #PB_EventType_FirstCustomValue)
    Protected r1, *GadgetData.udtWebKitGadget
    
    With *GadgetData
      *GadgetData.udtWebKitGadget = GetGadgetData(Gadget)
      If *GadgetData
        \Resource = webkit_web_view_get_main_resource(\WebView)
        If \Resource
          \Event = Event
          \Text = #Null$
          webkit_web_resource_get_data(\Resource, 0, @_get_response_data_finish(), *GadgetData)
          r1 = #True
        EndIf
      EndIf
    EndWith
    ProcedureReturn r1
  EndProcedure
  
  Procedure.s WebKit_GetDataHTML(Gadget)
    Protected s1.s, *GadgetData.udtWebKitGadget
    
    With *GadgetData
      If IsGadget(Gadget)
        *GadgetData.udtWebKitGadget = GetGadgetData(Gadget)
        If *GadgetData
          s1 = \Text
          \Text = #Null$
        EndIf
      EndIf
    EndWith
    ProcedureReturn s1
  EndProcedure
  
EndModule

; ****
Last edited by mk-soft on Wed Nov 24, 2021 9:41 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by mk-soft »

Example Mini Browser

Update v1.02.1

Code: Select all

;-TOP

; Comment : WebKitGadget Example (GTK3)
; Author  : mk-soft
; Version : v1.02.1 (Beta)
; Create  : 19.11.2021
; Update  : 24.11.2021

; ---------------------

IncludeFile "MyWebKitGadget.pb"

;- Example

UseModule WebKit2

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
  #MainButtonLoad
  #MainButtonStop
  #MainButtonBack
  #MainButtonForward
  #MainButtonReload
  #MainStringURI
  #MainWebKitGadget
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration


Procedure UpdateWindow()
  Protected height
  height = StatusBarHeight(#MainStatusBar) + 40
  ResizeGadget(#MainWebKitGadget, 0, 45, WindowWidth(#Main), WindowHeight(#Main) - height)
EndProcedure

Procedure MyHitTestCB(Gadget, Type, Text.s)
  
  Select Type
    Case #WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK
      StatusBarText(#MainStatusBar, 1, "HitTest: " + Text)
      Debug "HitTestCB Link: " + Text
      
    Case #WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE
      Debug "HitTestCB Image: " + Text
      
    Case #WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA
      Debug "HitTestCB Media: " + Text
      
    Case #WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT
      Debug "HitTestCB Dokument"
      StatusBarText(#MainStatusBar, 1, "HitTest: Dokument")
      
  EndSelect
  
                      
EndProcedure  
  
Procedure Main()
  
  Protected height, x, text.s
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If Not InitWebKit2()
    Debug "Error Load WebKit2!"
    End
  EndIf
  
  If OpenWindow(#Main, 100, 100, 800, 600, "WebKit" , #MainStyle)
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(120)
    AddStatusBarField(#PB_Ignore)
    
    x = 5
    ButtonGadget(#MainButtonLoad, x, 5, 90, 30, "Load")
    x + 95
    ButtonGadget(#MainButtonReload, x, 5, 90, 30, "Reload")
    x + 95
    ButtonGadget(#MainButtonStop, x, 5, 90, 30, "Stop")
    x + 95
    ButtonGadget(#MainButtonBack, x, 5, 90, 30, "Back")
    x + 95
    ButtonGadget(#MainButtonForward, x, 5, 90, 30, "Forward")
    x + 95
    StringGadget(#MainStringURI, x, 5, WindowWidth(#Main) - x - 5, 30, "about:blank")
    
    height = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - 45
    WebKitGadget(#MainWebKitGadget, 0, 45, 800, height, "") ;, "http://www.webkitgtk.org")
    
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow())
    
    WebKit_SetHitTestCallback(#MainWebKitGadget, @MyHitTestCB())
    
    WebKit_LoadURI(#MainWebKitGadget, "https://www.purebasic.com")
    ;WebKit_LoadURI(#MainWebKitGadget, "http://www.webkitgtk.org")
    
    ;WebKit_LoadHTML(#MainWebKitGadget, "Hello")
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainButtonLoad
              WebKit_LoadURI(#MainWebKitGadget, GetGadgetText(#MainStringURI))
              
            Case #MainButtonReload
              WebKit_Reload(#MainWebKitGadget)
              
            Case #MainButtonStop
              WebKit_StopLoading(#MainWebKitGadget)
              
            Case #MainButtonBack
              WebKit_GoBack(#MainWebKitGadget)
              
            Case #MainButtonForward
              WebKit_GoForward(#MainWebKitGadget)
              
            Case #MainWebKitGadget
              Select EventType()
                Case #PB_EventType_Change
                  Select EventData()
                    Case #WEBKIT_LOAD_STARTED
                      StatusBarText(#MainStatusBar, 0, "Load started")
                      
                    Case #WEBKIT_LOAD_COMMITTED
                      StatusBarText(#MainStatusBar, 0, "Load committed")
                      
                    Case #WEBKIT_LOAD_FINISHED
                      StatusBarText(#MainStatusBar, 0, "Load finished")
                      SetWindowTitle(#Main, "WebKit - " + WebKit_GetURI(#MainWebKitGadget))
                      WebKit_GetData(#MainWebKitGadget)
                      
                    Case #WEBKIT_LOAD_REDIRECTED
                      StatusBarText(#MainStatusBar, 0, "Load redirected")
                      SetWindowTitle(#Main, "WebKit - " + WebKit_GetURI(#MainWebKitGadget))
                      
                      
                  EndSelect
                  
                Case #PB_EventType_FirstCustomValue
                  text = WebKit_GetDataHTML(#MainWebKitGadget)
                  Debug "**********************************"
                  Debug text
                  Debug "**********************************"
                  
              EndSelect
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by mk-soft »

Update v1.02.1
- Added WebKit_LoadHTML
- Added WebKit_GetData ; Begin get resource data asynchronous. Sends an event to the webkit gadget when get data is finished
- Added Webkit_GetDataHTML; Call the function when get data is finished


It was really difficult to read the HTML code from the webkit. :x

Reading the resource data from the web view can only be done asynchronously. To do this, however, you first have to get the GAsyncReadyCallback callback to work. :!:

This is how it works. :wink:
With WebKit_GetData(...) the reading is triggered. A custom EventType can be specified for this. Once the read is complete, the event is sent to Gadget. After this, the data is taken over with WebKit_GetDataHTML.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by Fred »

PB 6.00 beta 1 now includes a WebGadget() for GTK3 (not with all features but should work).
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by idle »

webBrowser works on Manjaro with PB 6.00 Beta 1 x64
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WebKitGadget GTK3 (libwebkit2gtk-4.0)

Post by mk-soft »

Perhaps it was not all obsolete

Link: WebGadget Extension
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply