Executing Javascript on an external website

Just starting out? Need help? Post your questions and find answers here.
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Executing Javascript on an external website

Post by dige »

Hello folks,

what is the best (correct) way to control an external website via Javascript?
I have an OSM website and would like to use and control this page in Purebasic via the WebGadget.
Means, I would like to execute Javascript in the website via PureBasic.

The following code seems to make this possible. I call the JavaScript function f_showMessage() with PureBasic and this is successfully executed in the browser (WegGadget).

Before I now try to integrate the map extensively, I would like to ask again whether it should be done in this way?

Thank you! :)

Code: Select all

; Adapted from https://www.purebasic.fr/english/viewtopic.php?p=273634

Prototype.s ProtoExecuteJavaScript(gadget, Function$, Arguments$, Separator$, timeout=500) 

JSMutex = CreateMutex()

Procedure.s JavaScriptQuote(code.s)
  code = ReplaceString(code, "\", "\\")
  code = ReplaceString(code, Chr(34), "\"+Chr(34))
  ProcedureReturn Chr(34)+code+Chr(34)
EndProcedure

Procedure.l JavaScriptResetTitle(gadget.l)
	Shared JSMutex
	LockMutex(JSMutex)
	SetGadgetText(gadget, "javascript:void(function(){document.title='';})();")
	UnlockMutex(JSMutex)
EndProcedure

Procedure JavaScriptExamineThread(gadget.l)
	title.s = ""
  Repeat
    title = GetGadgetItemText(gadget, #PB_Web_PageTitle)
    If title = ""
      Delay(100)
    EndIf
  Until title > ""
EndProcedure

Procedure.s ProcExecuteJavaScript(gadget, Function$, Arguments$, Separator$, timeout) 
  Shared JSMutex
  If GadgetType(gadget) <> #PB_GadgetType_Web
    ProcedureReturn ""
  EndIf
  code.s = ""
  If Trim(Arguments$) = "" 
    code = ""
  ElseIf Seperator$ = ""
    code = JavaScriptQuote(Arguments$)
  Else            
    Count = CountString(Arguments$, Separator$)+1 
    For i = 1 To Count            
      code = code + JavaScriptQuote(StringField(Arguments$, i, Separator$))
      If i <> Count
        code = code + ","
      EndIf
    Next i 
  EndIf 
  code = Function$+"("+code+");"
  
  JavaScriptResetTitle(gadget)
  LockMutex(JSMutex)
  code = JavaScriptQuote(code)
  js.s = "javascript:void(function(){document.title='';try{var ret = eval("+code
  js = js + ");document.title=ret;}catch(e){document.title='exception: '+e;}})();"
  SetGadgetText(gadget, js)
	thread = CreateThread(@JavaScriptExamineThread(), gadget)
	If thread <> 0
		WaitThread(thread, timeout)
	EndIf
  UnlockMutex(JSMutex)
  title.s = GetGadgetItemText(gadget, #PB_Web_PageTitle)
  ProcedureReturn title
EndProcedure

ExecuteJavaScript.ProtoExecuteJavaScript = @ProcExecuteJavaScript()

Enumeration
  #Gadget_Web
  #Gadget_Command
  #Gadget_Args
  #Gadget_Text
  #Gadget_Button
EndEnumeration

Procedure Resize()
  Width = WindowWidth(0)
  Height = WindowHeight(0)
  ResizeGadget(#Gadget_Web, 5, 5, Width-10, Height-60)
  ResizeGadget(#Gadget_Command, 5, Height-50, (Width-105)/4, 20)
  ResizeGadget(#Gadget_Args, 10+(Width-105)/4, Height-50, ((Width-105)*3)/4, 20)
  ResizeGadget(#Gadget_Text, 5, Height-25, Width-10, 20)
  ResizeGadget(#Gadget_Button, Width-90, Height-50, 85, 20)
EndProcedure

If OpenWindow(0, 0, 0, 800, 600, "Javascript test", #PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)
    WebGadget(#Gadget_Web, 0, 0, 0, 0, "", #PB_Web_Edge)
    StringGadget(#Gadget_Command, 0, 0, 0, 0, "f_showMessage")
    StringGadget(#Gadget_Args, 0, 0, 0, 0, "Message via ExecuteJavascript :-)")
    TextGadget(#Gadget_Text, 0, 0, 0, 0, "Type commandname into the first, and arguments into the second box. Do not include ()", #PB_Text_Border)
    ButtonGadget(#Gadget_Button, 0, 0, 0, 0, "Execute")
    Resize()
    
     html.s = ~"<!DOCTYPE html><html lang=\"de\">" + #CRLF$ +
         ~"<head><meta charset=\"UTF-8\">" + #CRLF$ +
         ~"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" + #CRLF$ +
         "<title>Alert mit JavaScript</title>" + #CRLF$ +
         "<script>" + #CRLF$ +
         "function f_showMessage(text) {" + #CRLF$ +
         "alert(text);" + #CRLF$ +
         "}" + #CRLF$ +
         "</script></head>" + #CRLF$ +
         "<body><h1>Messagebox with JavaScript</h1>" + #CRLF$ +
         "<p>Please click the button below, to open the Messagebox.</p>" + #CRLF$ +
         ~"<button onclick=\"f_showMessage('You have clicked the button. Please try it with Execute Javascript also')\">Test Messagebox</button>" + #CRLF$ +
         "</body></html>"



        SetGadgetItemText(#Gadget_Web, #PB_Web_HtmlCode, html)
    
    
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_SizeWindow
        Resize()
      ElseIf Event = #PB_Event_Gadget And EventGadget() = #Gadget_Button
        Command$ = GetGadgetText(#Gadget_Command)
        Arguments$ = GetGadgetText(#Gadget_Args)
        Result$ = ExecuteJavaScript(#Gadget_Web, Command$, Arguments$, ",")           
        SetGadgetText(#Gadget_Text, "Result: "+Result$)         
      EndIf
    Until Event = #PB_Event_CloseWindow
EndIf

End


"Daddy, I'll run faster, then it is not so far..."
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Executing Javascript on an external website

Post by JHPJHP »

Hi dige,

The PureBasic WebGadget already has the ability to do everything you're requesting without using ugly hacks, extensions, or third-party addons.
• Everything that was done using the old WebGadget, and everything you were hoping for from the new WebGadget is possible.

Here is a link to some demonstration videos that should support my previous statements.
My PureBasic Stuff • Forum • Chromium Framework • Demonstration Videos
Last edited by JHPJHP on Mon Oct 07, 2024 5:48 pm, edited 12 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Executing Javascript on an external website

Post by dige »

Thank you JHPJHP, did you receive my message via the forum?
"Daddy, I'll run faster, then it is not so far..."
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Executing Javascript on an external website

Post by ChrisR »

It's good to see you JHPJHP, welcome back, perhaps :)
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Executing Javascript on an external website

Post by normeus »

Nice to see a new post from you JHPJHP!
I think almost everyone in the forums would be interested, The videos look amazing.

Take care.
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Executing Javascript on an external website

Post by JHPJHP »

Hi dige,

I hope you received my email. Please don't hesitate to ask questions. It might be quicker if you posted directly to my forum.

----------------

Hi ChrisR,

Thank you for the kind words, and congratulations on your IceDesign GUI Designer.

----------------

Hi normeus,
normeus wrote:I think almost everyone in the forums would be interested
I would have thought so as well, but not long ago I emailed several forum members describing my progress. I did this to initiate a dialog and gauge interest, but never heard back. Next, I emailed a link to a forum member who posted a question that I had solved using the Framework, but the response wasn't much better. Lastly, I posted here offering anyone who contacted me a copy of the Framework, another waste of time.
Last edited by JHPJHP on Sun Sep 29, 2024 4:58 am, edited 6 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Executing Javascript on an external website

Post by Caronte3D »

Nice to see you again JHPJHP :wink:
I hope you follow contribute to this community, sometimes you can feel no one is interested, but it's not the case, for sure, simply not the momento to use it in our own projects or even, not understanding how to apply it.
I think like normeus about your codes, all are awesome, some are like magic to me :D
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Executing Javascript on an external website

Post by dige »

JHPJHP wrote: Thu Sep 19, 2024 1:19 am Hi dige,
I hope you received my email. Please don't hesitate to ask questions. It might be quicker if you posted directly to my forum.
Yes, I have received your email. Thank you very much.
I'm still marvelling and trying it out. :o
At the moment I'm just blown away by all the great possibilities.

I'm sure I'll have more questions and will be happy to get in touch via your forum.
"Daddy, I'll run faster, then it is not so far..."
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Executing Javascript on an external website

Post by ChrisR »

Caronte3D wrote: Thu Sep 19, 2024 8:47 am Nice to see you again JHPJHP :wink:
I hope you follow contribute to this community, sometimes you can feel no one is interested, but it's not the case, for sure, simply not the momento to use it in our own projects or even, not understanding how to apply it.
I think like normeus about your codes, all are awesome, some are like magic to me :D
Totally agree and it reminds me of something you kindly wrote on SweetyVD (I admit it's not always easy :wink:)
JHPJHP wrote: Thu Apr 06, 2017 6:09 pm NB*: Don't take the current lack of comments to mean the community isn't grateful for the work you've done.
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Executing Javascript on an external website

Post by JHPJHP »

Hi Caronte3D,

As always, it was a pleasure hearing from you. Thank you for your kind words.

------------------

Hi ChrisR,

Thank you for your post.

Since I removed downloading and donation options from my website, I'm no longer concerned about general feedback.
For me it came down to interest, and my Chromium Framework didn't generate much.
Last edited by JHPJHP on Tue Oct 01, 2024 12:21 am, edited 4 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Executing Javascript on an external website

Post by JHPJHP »

Additional Information:

If I take a broader approach to the post: "Executing JavaScript on an external website"…

As with the PureBasic WebGadget, most new browsers include within their architecture the Chrome DevTools Protocol:
Edge, Chrome, Opera, Vivaldi, Brave, Epic, Maxthon, Comodo and Firefox to name a few.

It’s just a matter of accessing the protocol commands to have complete control over not just a WebGadget, but any installed Chromium based browser.
For the sake of brevity, I won’t go into controlling a WebGadget or browser on a remote computer, but it’s possible.

It doesn't take a large program to do what you see in the video. Included with the Chromium Framework are templates (120-lines of code) that do the same thing. These same methods have been used to create DLLs with full control over a WebGadget or browser.
Last edited by JHPJHP on Tue Oct 01, 2024 12:20 am, edited 2 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Executing Javascript on an external website

Post by dige »

@JHPJHP: for me, communicating with the WebGadget via WebSocket is a big change at first. But I can see that it opens up a lot of great new possibilities and I'm going to dig into it.
Thanks again :)
"Daddy, I'll run faster, then it is not so far..."
BarryG
Addict
Addict
Posts: 4122
Joined: Thu Apr 18, 2019 8:17 am

Re: Executing Javascript on an external website

Post by BarryG »

Hi JHPJHP! Like the others said, it's good to see your new posts here. :)
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Executing Javascript on an external website

Post by JHPJHP »

Hi BarryG,

Thank you, it was nice to hear from you.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Post Reply