Calling a PB procedure from Javascript

Mac OSX specific forum
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Calling a PB procedure from Javascript

Post by firace »

Is there any way of having the HTML button in this webgadget trigger a PB procedure?

EDIT: Perhaps by adapting this code: viewtopic.php?f=3&t=54007&hilit=NavigationCallback

I will play around with it and see if I can get anywhere.

Code: Select all

OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body>Hello <form><button onclick='document.body.innerText=1234;'>Test</button></form></body></html>")

Repeat
  EventID = WaitWindowEvent()  
Until EventID = #PB_Event_CloseWindow
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Calling a PB procedure from Javascript

Post by Sicro »

Code: Select all

OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>")

Repeat
  EventID = WaitWindowEvent()
  
  If url$ <> GetGadgetText(0)
    url$ = GetGadgetText(0)
    If StringField(url$, 2, "#") = "click"
      Debug "Button was clicked!"
    EndIf
  EndIf
  
Until EventID = #PB_Event_CloseWindow
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Calling a PB procedure from Javascript

Post by firace »

Nice approach! I couldn't try it on a Mac yet, but it seems to work fine on Windows :)
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Calling a PB procedure from Javascript

Post by mk-soft »

With the Link: viewtopic.php?f=3&t=54007 :wink:

Code: Select all

EnableExplicit

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)

  If URLString = "#click" 
    MessageRequester("", "Button clicked!")
  Else
    CocoaMessage(0, listener, "use")
  EndIf
EndProcedure

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

If OpenWindow(0, 270, 100, 600, 300, "WebGadget")
  WebGadget(0, 10, 10, 580, 280, "")
  SetGadgetItemText(0, #PB_Web_HtmlCode, ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>")

  CocoaMessage(0, GadgetID(0), "setPolicyDelegate:", appDelegate)
 
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
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
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Calling a PB procedure from Javascript

Post by Shardik »

I have tried Sicro's example on these operating systems with PB 5.71 x64:
  • Linux 19.3 'Tricia' x64: the contents of the root directory are displayed in the WebGadget; the debug message "Button was clicked!" is not displayed!
  • MacOS 10.14.6 'Mojave': the debug message "Button was clicked!" is not displayed!
  • Windows 10 x64 V1809: the debug message "Button was clicked!" is correctly displayed.
mk-soft's example is working fine on MacOS (it utilizes MacOS API functions)!

If you want to detect on MacOS when a button in a HTML form is clicked, you may also try this example from the German forum.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Calling a PB procedure from Javascript

Post by Sicro »

Shardik wrote:Linux 19.3 'Tricia' x64: the contents of the root directory are displayed in the WebGadget; the debug message "Button was clicked!" is not displayed!
Before I posted my above code here, I tested it on Linux and Windows XP.
Under Linux additional code was needed for the WebGadget because JavaScript is not enabled by default and SetGadgetItemText() is not supported. I omitted this additional code here because I assumed that only a macOS solution is requested and not a cross-platform solution.
Here the above code with the additional code for Linux:

Code: Select all

ImportC "-lwebkitgtk-3.0"
  webkit_web_settings_new()
  webkit_web_view_set_settings(*webkitWebView, *webkitSettings)
  webkit_web_view_load_string(*webView, content.p-utf8, *mimeType, *encoding, *baseURI)
EndImport

OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
; Enable Javascript
*webKitSettings = webkit_web_settings_new()
webkit_web_view_set_settings(GadgetID(webGadget), *webkitSettings)
; Set HTML code
webkit_web_view_load_string(GadgetID(0), ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>", 0, 0, 0)

Repeat
  EventID = WaitWindowEvent()
  
  If url$ <> GetGadgetText(0)
    url$ = GetGadgetText(0)
    If StringField(url$, 2, "#") = "click"
      Debug "Button was clicked!"
    EndIf
  EndIf
Until EventID = #PB_Event_CloseWindow
Shardik wrote:MacOS 10.14.6 'Mojave': the debug message "Button was clicked!" is not displayed!
Pity. I just saw in the PB help that SetGadgetItemText() is also not supported on macOS.
And if you use this instead of SetGadgetItemText():

Code: Select all

Procedure SetHTMLCode(webGadget, htmlCode$)
  Protected filePath$ = GetTemporaryDirectory() + "temp.html"
  Protected file = CreateFile(#PB_Any, filePath$)
  If file
    WriteString(file, htmlCode$)
    CloseFile(file)
    SetGadgetText(webGadget, "file:///" + filePath$)
  EndIf
EndProcedure

SetHTMLCode(0, ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>")
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Calling a PB procedure from Javascript

Post by firace »

OK, I had a chance to try Sicro's first code on a Mac last weekend (I don't personally have a Mac at home), and in fact it did work for me: the "Button is clicked" debug statement was triggered, but with a caveat: it was triggered when the mouse button was released too. In case that matters, OS X version was pretty old (10.11.6).

Didn't have a chance to try the other codes yet.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Calling a PB procedure from Javascript

Post by Derren »

Sicro wrote:

Code: Select all

OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>")

Repeat
  EventID = WaitWindowEvent()
  
  If url$ <> GetGadgetText(0)
    url$ = GetGadgetText(0)
    If StringField(url$, 2, "#") = "click"
      Debug "Button was clicked!"
    EndIf
  EndIf
  
Until EventID = #PB_Event_CloseWindow
Interesting idea, but a second or third click won't be recognized.
You would need to set the URL back to "withouth #click". But I haven't tested if this can be done without the whole website refreshing (perhaps set it to thisisthebaseURL#null" instead

Code: Select all

If StringField(url$, 2, "#") = "click"
    Debug "Button was clicked!"
    SetGadgetText(0, "#null_or_really_anything_else_that_is_not_click")
EndIf


edit: nvm. in this code the Button disappears, so it's not an issue, but generally speaking, it is
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Calling a PB procedure from Javascript

Post by Sicro »

@Derren:

Code: Select all

OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, ~"<html><body>Hello <form><button onclick='window.location.href = \"#click_\" + Date.now(); return false'>Test</button></form></body></html>", 0, 0, 0)

Repeat
  EventID = WaitWindowEvent()
 
  If url$ <> GetGadgetText(0)
    url$ = GetGadgetText(0)
    If StringField(StringField(url$, 2, "#"), 1, "_") = "click"
      Debug "Button was clicked!"
    EndIf
  EndIf
 
Until EventID = #PB_Event_CloseWindow
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Calling a PB procedure from Javascript

Post by Shardik »

Sicro wrote:
Shardik wrote:MacOS 10.14.6 'Mojave': the debug message "Button was clicked!" is not displayed!
Pity. I just saw in the PB help that SetGadgetItemText() is also not supported on macOS.
And if you use this instead of SetGadgetItemText():

Code: Select all

Procedure SetHTMLCode(webGadget, htmlCode$)
  Protected filePath$ = GetTemporaryDirectory() + "temp.html"
  Protected file = CreateFile(#PB_Any, filePath$)
  If file
    WriteString(file, htmlCode$)
    CloseFile(file)
    SetGadgetText(webGadget, "file:///" + filePath$)
  EndIf
EndProcedure

SetHTMLCode(0, ~"<html><body>Hello <form><button onclick='document.body.innerText=1234; window.location.href = \"#click\"; return false'>Test</button></form></body></html>")
On MacOS 10.14.6 'Mojave' your first example with the added procedure SetHTMLCode() is displaying the debug message "Button was clicked!" correctly!
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Calling a PB procedure from Javascript

Post by Sicro »

Shardik wrote:On MacOS 10.14.6 'Mojave' your first example with the added procedure SetHTMLCode() is displaying the debug message "Button was clicked!" correctly!
Glad to hear it. Thanks for testing.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply