Autore Topic: Chiamare una Sub routine da WebView  (Letto 932 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Chiamare una Sub routine da WebView
« il: 05 Dicembre 2013, 00:18:55 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:


" Hi all

I currently helping to port a VB6 program to Gambas3, but to my
surprise, it does weird things like these from the post title: it is
calling methods and functions from the html code inside a webbrowser
control.

It has a wb control that acts like a sort of wizard/help thing, and the
html links in that view are calling methods inside the application. That links are as

'
Codice: html [Seleziona]
<a href="act:some_class.method">some action</a>
'

So the question is, could it be done with gambas somehow?

Regards
--
Jesus Guardon
"


" Jesus (meant as an interjection)! I know I shouldn't question other people's
design choices but this is just mad. Actually it might have seemed like an
elegant way to communicate with an HTML page to the original author of that
program - I understand that - but this is just asking for trouble, IMHO.

However, I guess Object.Call() is your best bet here.

Regards,
Tobi
"


" Hi Tobi, thanks for reply

I am at your side about some (mad) practices, but...

I think Object.Call is suitable for calling or executing code inside or
outside of the current class, but not the other way round, IIUC. How
could I execute Object.Call from inside a webview? None of the events
webview exposes are suitable for doing this IMO, since the webview.url
is a property, not a method.

I will experiment, thanks for the tip!

Regards
--
Jesus Guardon
"


" Ahh, I got you wrong apparently... My answer can be applied if you have any
occasion (event) to parse an (X)HTML element for this special attribute
href="act:some_class.method". Then you can extract the class and method name and use Object.Call().

You can (ab)use the WebView's Click event like this: Modify the <a> element to look like this:


Codice: html [Seleziona]
<a href="#act:some_class.method">some action</a>


Then, when clicked, the WebView will load the valid URL (the anchor sign had
to be prepended to make it valid - to trigger the Click event, in turn) and
provide you a WebFrame object in whose Url property you have the wanted
act:some_class.method string somewhere. Now parse and execute.

I think it's too late for me to find clear words, so I attach a minimal
example.
(vedi allegato)
(Note that the code is executed twice in this project which is
certainly not desirable. You'll sure find a way to prevent this yourself.)

Regards,
Tobi
"


" Many thanks, Tobi

I think this is a good starting point. Just hope I will not get crazy
after that. Now I'm going to sleep and tomorrow I will give it a fresh (re)view.

Regards
--
Jesus Guardon
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Chiamare una Sub routine da WebView
« Risposta #1 il: 05 Dicembre 2013, 14:20:05 »
...continua...


" Hi:
i'm the mad guy who made that VB code wich calls Funcitions from the HTML code :)

I'n new  to Gambas, but from what I've read, it is possible to set up an Object Observer
...so why not intercept the webview control  and do  things BEFORE the Click event takes place?

Saludos
Martin
"


" That's indeed a cleaner solution because it leaves the WebView alone but in
the Observer_Click() handler, you would do exactly the same as in the WebView_Click() handler.

Regards,
Tobi
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline nelchael81

  • Gamberetto
  • *
  • Post: 14
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #2 il: 29 Luglio 2017, 19:42:32 »
Sto provando ad utilizzare questo metodo, ma senza successo.
Qualcuno l'ha provato?

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #3 il: 29 Luglio 2017, 19:58:56 »
Hai scaricato e provato il file allegato sopra, chiamato "execute-code-from-webview-0.0.1.tar.gz" ?

A me funziona.
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.158
  • Tonno verde
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #4 il: 30 Luglio 2017, 10:07:21 »
Sto provando ad utilizzare questo metodo, ma senza successo.
Qualcuno l'ha provato?

Anch'io ho provato il programma e funziona.
Ma tu cosa vorresti ottenere? Qual'è il tuo scopo?
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #5 il: 30 Luglio 2017, 15:25:34 »
...potremmo modificare quel codice, eliminando la Classe secondaria e ponendo così la sub-routine all'interno della Classe principale.

Codice: [Seleziona]
Private wv As WebView


Public Sub Form_Open()

  Crea_html()
 
  With Me
    .W = 300
    .H = 300
  End With
 
  With wv = New WebView(Me) As "Wview"
    .X = 0
    .Y = 0
    .W = Me.W
    .H = Me.H
  End With
 
  wv.Url = "/tmp/html.html"

End


Public Sub Wview_Click(hFrame As WebFrame)

  Dim aSpecial, aModuleMethod As String[]

   If hFrame.Url Like "*#act:*" Then
     Object.Call(FMain, "Da_Richiamare", ["Testo qualsiasi", 123456])
   Endif

End


Public Sub Da_Richiamare(testo As String, numero As Integer)
 
  Print testo, numero
 
End


Private Procedure Crea_html()
 
  File.Save("/tmp/html.html", "<HTML>\n <BODY>\n\n" &
            "  <a href=\"#act:FMain.Da_Richiamare\">Click me!</a>\n\n" &
            " </body>\n</html>")
 
End
Un'altra novità è che il Metodo "Object.Call( )", che chiama la sub-routine, le passa anche due argomenti.


Volendo poi inserire due o più collegamenti sul WebView:
Codice: [Seleziona]
Private wv As WebView


Public Sub Form_Open()

  Crea_html()
 
  With Me
    .W = 300
    .H = 300
  End With
 
  With wv = New WebView(Me) As "Wview"
    .X = 0
    .Y = 0
    .W = Me.W
    .H = Me.H
  End With
 
  wv.Url = "/tmp/html.html"

End


Public Sub Wview_Click(hFrame As WebFrame)

  Dim aSpecial, aModuleMethod As String[]
  Dim ss As String[]
 
   ss = ["Chimata sub-routine 'Secondo()'"]

   If hFrame.Url Like "*#act:*" Then
     aSpecial = Split(hFrame.Url, "#")
     aModuleMethod = Scan(aSpecial[aSpecial.Max], "act:*.*")

     Select Case aModuleMethod[1]
       Case "Primo"
         Object.Call(Classes[aModuleMethod[0]].Instance, aModuleMethod[1])
       Case "Secondo"
         Object.Call(Classes[aModuleMethod[0]].Instance, aModuleMethod[1], ss)
     End Select
  Endif

End


Public Sub Primo()
 
  Print "Chimata sub-routine 'Primo()'"
 
End

Public Sub Secondo(s As String)
 
  Print s
 
End


Private Procedure Crea_html()
 
  File.Save("/tmp/html.html", "<HTML>\n <BODY>\n\n" &
            "  <a href=\"#act:FMain.Primo\">Click me 1 !</a>\n" &
            "<P><a href=\"#act:FMain.Secondo\">Click me 2 !</a></p>\n\n" &
            " </body>\n</html>")
 
End


« Ultima modifica: 30 Luglio 2017, 20:13:59 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline nelchael81

  • Gamberetto
  • *
  • Post: 14
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #6 il: 31 Luglio 2017, 13:08:49 »
@gianluigi
Vorrei mostrare nella webview l'output di uno script a riga di comando.
Vorrei creare qualcosa di simile al comportamento di una cgi chiamata tramite un server web.

@vuott
Proverò il tuo codice e faccio sapere, grazie 1000  :ok:

Offline nelchael81

  • Gamberetto
  • *
  • Post: 14
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #7 il: 31 Luglio 2017, 13:20:22 »
Sto avendo lo stesso problema di prima.
Ho aggiunto Print hFrame.Url alla sub Wview_Click e l'output che ottengo quando faccio click sui link è "about:blank"
Quindi hFrame.Url Like "*#act:*" è sempre falso.
Uso gambas 3.9.2

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #8 il: 31 Luglio 2017, 13:41:34 »
Quale Componente grafico stai usando ?
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline nelchael81

  • Gamberetto
  • *
  • Post: 14
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #9 il: 31 Luglio 2017, 13:45:14 »
Sono riuscito a farlo funzionare, ho dovuto modificare

wv.url = "/tmp/html.html"

in

wv.url = "file:///tmp/html.html"

Il problema è che vorrei evitare di salvare l'output in un file, e vorrei passarlo come stringa.
Ma se sostituisco il comando wv.url con il seguente

wv.html = "<HTML>\n <BODY>\n\n" &
            "  <a href=\"#act:FMain.Da_Richiamare\">Click me!</a>\n\n" &
            " </body>\n</html>"

quando faccio click sul link non funziona più, perchè hFrame.Url mi passa "about:blank" invece che "#act:FMain.Da_Richiamare"

Secondo voi è risolvibile ? O salvare l'output su un file è l'unica via ?

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #10 il: 31 Luglio 2017, 13:51:55 »
...evitare di salvare l'output in un file, e vorrei passarlo come stringa.
Perdonami, ma mon riesco a capire bene.
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.158
  • Tonno verde
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #11 il: 31 Luglio 2017, 15:52:13 »
Anch'io stento a capire.
Tu crei un file html per poterlo aprire e utilizzare, cosa che ottieni solo in minima parte con xx.html =
Del reso puoi scegliere di crearlo a runtime oppure scriverlo come in un modulo.
Vedi immagini allegate.

Ma forse non ho capito la domanda
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline nelchael81

  • Gamberetto
  • *
  • Post: 14
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #12 il: 31 Luglio 2017, 15:53:27 »
Invece di salvare il file html in /tmp/test.html e caricarlo nella webview tramite il comando wv.url, vorrei caricato direttamente l'html dentro la webview usando il comando wv.html.
Solo che facendo così non si riesce a far funzionare i link come vorrei.

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #13 il: 31 Luglio 2017, 17:11:51 »
... vorrei caricato direttamente l'html dentro la webview usando il comando wv.html.
Solo che facendo così non si riesce a far funzionare i link come vorrei.
...proprio per questo si è costretti ad utilizzare un file .html di appoggio.
Non tutti i tag html sono agibili con la proprietà .HTML della Classe WebView .
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.158
  • Tonno verde
    • Mostra profilo
Re:Chiamare una Sub routine da WebView
« Risposta #14 il: 31 Luglio 2017, 17:30:41 »
Comunque tutto il discorso fatto sulla ML mi appare contorto,
perché non è possibile fare così?
Vedi file allegato
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro