WebView with links and target="_blank"

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

WebView with links and target="_blank"

Post by Kukulkan »

Hi,

The following code illustrates my problem (I'm on Linux with PB6.21):

Code: Select all

OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
WebViewGadget(0, 0, 0, 400, 400)
SetGadgetItemText(0, #PB_WebView_HtmlCode, 
                  ~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
                  ~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")

Repeat 
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
If I click the link with _self, it works while the link with _blank just does nothing. I like to have a chance to handle this event so I can handle this by myself (eg open system webbrowser) but without editing or changing or injecting to html.

I know that this is likely for safety, but on Android and Linux with WebViews (Kotlin and Swift) such links open up in system webbrowser. I like to have similar behavior on Windows/Linux/MacOS.

Is there a way I can handle or enable this without having to intercept the link on HTML side to run a bound function?
normeus
Enthusiast
Enthusiast
Posts: 484
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: WebView with links and target="_blank"

Post by normeus »

On windows PB 6.12x64 and 6.21x64, webviewgadget() opens up a window with an instance of webview2, not the default browser.
I have not used the webviewgadget() so I have no idea if opening a new window like a regular browser would be a desired behavior.

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebView with links and target="_blank"

Post by Kukulkan »

Thanks for testing. I'm basically looking for a way to intercept this and handle it by myself. I already have cross platform functions for correctly handling URIs and I like to use this if someone clicks such a link.
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebView with links and target="_blank"

Post by Kukulkan »

So, no one has a solution for this? Links with target="_blank" are not supported and do not work in WebViewGadget or do undefined things like opening another WebView instance?

If I have control over the website, I may be able to intercept these links and run a local bound function call. But if the content is not by me (eg some external website), it is not fully functional :-(
Justin
Addict
Addict
Posts: 961
Joined: Sat Apr 26, 2003 2:49 pm

Re: WebView with links and target="_blank"

Post by Justin »

For linux:

Code: Select all

EnableExplicit 

ImportC ""
  webkit_policy_decision_use(decision.i)
  webkit_policy_decision_ignore(decision.i)
  webkit_policy_decision_download(decision.i)
  webkit_navigation_policy_decision_get_navigation_action.i(decision.i)
  webkit_navigation_action_get_request.i(action.i)
  webkit_uri_request_get_uri.i(req.i)
EndImport

Enumeration
	#WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
	#WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
	#WEBKIT_POLICY_DECISION_TYPE_RESPONSE
EndEnumeration
  
ProcedureC.l decide_policy_cb(webview.i, decision.i, type.l, gdt.i)
	Protected.i action, req, uri
	Protected.s suri
		
	If type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
		action = webkit_navigation_policy_decision_get_navigation_action(decision)
		req = webkit_navigation_action_get_request(action)
		uri = webkit_uri_request_get_uri(req)
		
		If uri 
			suri = PeekS(uri, -1, #PB_UTF8)
		EndIf
		Debug suri
	
	ElseIf type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
		Debug "_blank"
	EndIf	
	
	webkit_policy_decision_use(decision)
	;webkit_policy_decision_ignore(decision) ;Block
EndProcedure

Procedure webview_init(gdt.i)
	Protected.i webview
	
	webview = gtk_bin_get_child_(GadgetID(gdt))
	g_signal_connect_(webview, "decide-policy", @decide_policy_cb(), gdt)
EndProcedure

Procedure main()
	Protected.i Event, wv
	
	OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
	wv = WebViewGadget(#PB_Any, 0, 0, 400, 400)
	webview_init(wv)
	SetGadgetItemText(wv, #PB_WebView_HtmlCode, 
	                  ~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
	                  ~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")
	
	Repeat 
	  Event = WaitWindowEvent()
	Until Event = #PB_Event_CloseWindow
EndProcedure

main()
Justin
Addict
Addict
Posts: 961
Joined: Sat Apr 26, 2003 2:49 pm

Re: WebView with links and target="_blank"

Post by Justin »

Or better like this to get also the uri of the new window:

Code: Select all

EnableExplicit 

ImportC ""
	webkit_policy_decision_use(decision.i)
  webkit_policy_decision_ignore(decision.i)
  webkit_policy_decision_download(decision.i)
  webkit_navigation_policy_decision_get_navigation_action.i(decision.i)
  webkit_navigation_action_get_request.i(action.i)
  webkit_uri_request_get_uri.i(req.i)  
EndImport

Enumeration
	#WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
	#WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
	#WEBKIT_POLICY_DECISION_TYPE_RESPONSE
EndEnumeration
  
ProcedureC.l decide_policy_cb(webview.i, decision.i, type.l, gdt.i)
	Protected.i action, req, uri
	Protected.s suri
		
	If type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION Or type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
		action = webkit_navigation_policy_decision_get_navigation_action(decision)
		
		req = webkit_navigation_action_get_request(action)
		uri = webkit_uri_request_get_uri(req)
		
		If uri 
			suri = PeekS(uri, -1, #PB_UTF8)
		EndIf
		Debug suri
		
		If type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
			Debug "_blank"
			
		ElseIf type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
			Debug "_self"
		EndIf	
	EndIf
	
	webkit_policy_decision_use(decision)
	;webkit_policy_decision_ignore(decision) ;Block
EndProcedure

Procedure webview_init(gdt.i)
	Protected.i webview
	
	webview = gtk_bin_get_child_(GadgetID(gdt))
	g_signal_connect_(webview, "decide-policy", @decide_policy_cb(), gdt)
EndProcedure

Procedure main()
	Protected.i Event, wv
	
	OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
	wv = WebViewGadget(#PB_Any, 0, 0, 400, 400)
	webview_init(wv)
	SetGadgetItemText(wv, #PB_WebView_HtmlCode, 
	                  ~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
	                  ~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")
	
	Repeat 
	  Event = WaitWindowEvent()
	Until Event = #PB_Event_CloseWindow
EndProcedure
main()
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebView with links and target="_blank"

Post by Kukulkan »

Thank you Justin. I yesterday solved it by injecting script to catch links on a[target="_blank"] and call a bound local function. Not elegant, but working for my needs.

Not bad to have your solution on the forum here, so maybe other will profit from that, too.
Post Reply