HTML code streamed into WebGadget cannot run external JavaScript

Just starting out? Need help? Post your questions and find answers here.
User avatar
Techmas
User
User
Posts: 11
Joined: Sun Jan 26, 2020 2:06 pm

HTML code streamed into WebGadget cannot run external JavaScript

Post by Techmas »

It seems that external JavaScript cannot be executed from code that is streamed into the webgadget.

Example with external JavaScript in separate file (does not work):

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
	WebGadget(0, 0, 0, 600, 300, "", #PB_Web_Edge)
	
	html$="<button id=''demo'' type=''button'' onclick=''myFunction()''>Click Me</button><script src=''file:///"+GetCurrentDirectory()+"myScript.js''></script>"
	html$=ReplaceString(html$,"''",Chr(34))
	Debug html$
	
	SetGadgetItemText(0, #PB_Web_HtmlCode, html$)
	Repeat 
	Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
myScript.js (located in the same directory as the .pb file):

Code: Select all

function myFunction() {
  document.getElementById("demo").innerHTML = "Clicked";
}
It works, if the JavaScript is directly embedded in the html code

Example with embedded JavaScript (does work):

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
	WebGadget(0, 0, 0, 600, 300, "", #PB_Web_Edge)
	
	html$="<button id=''demo'' type=''button'' onclick=''myFunction()''>Click Me</button><script>function myFunction(){document.getElementById(''demo'').innerHTML = ''Clicked'';}</script>"
	html$=ReplaceString(html$,"''",Chr(34))
	Debug html$
	
	SetGadgetItemText(0, #PB_Web_HtmlCode, html$)
	
	Repeat 
	Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
It does also work if the test.html file is set as URL:

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
	WebGadget(0, 0, 0, 600, 300, "file:///"+GetCurrentDirectory()+"test.html", #PB_Web_Edge)

	Repeat 
	Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf

Thank you very much & Regards
Last edited by Techmas on Sat Apr 27, 2024 8:34 am, edited 2 times in total.
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: HTML code streamed into WebGadget cannot run external JavaScript

Post by Kiffi »

I suspect that this does not work for security reasons. If you use a WebView gadget with the #PB_WebView_Debug flag, the console will say "Not allowed to load local resource"
Hygge
User avatar
Techmas
User
User
Posts: 11
Joined: Sun Jan 26, 2020 2:06 pm

Re: HTML code streamed into WebGadget cannot run external JavaScript

Post by Techmas »

You are right. As you suspected it says 'Not allowed to load local resource'. It's probably a security thing... but i still find it kind of strange, because when i create a local html file with a local resource (same code that was streamed into the WebGadget or WebView), the problem does not occur. I thought it only occurs when the files are stored in mixed locations e.g. locally and on a server.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: HTML code streamed into WebGadget cannot run external JavaScript

Post by Justin »

You have to use virtual hosts:

Code: Select all

EnableExplicit

Define.s HTML
Define.ICoreWebView2 wv2
Define.ICoreWebView2_3 wv2_3
Define.ICoreWebView2Controller ctrl

#APP_VIRTUAL_HOST = "myapp.com"

HTML = "<html><head>" + 
~"<script type=\"text/javascript\" src=\"http://" + #APP_VIRTUAL_HOST + ~"/myScript.js\"></script>" +
"</head><body>" + 
~"<button id=\"demo\" onclick=\"myFunction()\">Click Me</button>" +
"</body></html>"

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
	WebGadget(0, 0, 0, 600, 300, "", #PB_Web_Edge)
	
	ctrl = GetGadgetAttribute(0, #PB_Web_ICoreController)
	ctrl\get_CoreWebView2(@wv2)
	wv2\QueryInterface(?IID_ICoreWebView2_3, @wv2_3)
	If wv2_3
		wv2_3\SetVirtualHostNameToFolderMapping(#APP_VIRTUAL_HOST, GetCurrentDirectory(), #COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW)
		wv2_3\Release()
		
	Else
		Debug "Virtual hosts not supported"
	EndIf
	wv2\Release()
	
	SetGadgetItemText(0, #PB_Web_HtmlCode, HTML)
	
	Repeat 
	Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf

DataSection
	IID_ICoreWebView2_3:
	Data.l $A0D6DF20
	Data.w $3B92, $416D
	Data.b $AA, $C, $43, $7A, $9C, $72, $78, $57
EndDataSection
User avatar
Techmas
User
User
Posts: 11
Joined: Sun Jan 26, 2020 2:06 pm

Re: HTML code streamed into WebGadget cannot run external JavaScript

Post by Techmas »

It works perfectly. Thank you so much!
Post Reply