Page 11 of 13

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Mon Dec 18, 2023 12:25 am
by vmars316
infratec wrote: Sat Dec 16, 2023 6:38 pm You can look at the NavigationCallback().
Thanks for your reply ;
OK , I'll check t out...

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Mon Dec 18, 2023 12:29 am
by vmars316
Justin wrote: Sun Dec 17, 2023 10:02 am Doesn't the NavigationStarting event do what you want?
https://learn.microsoft.com/en-us/micro ... navigating
Justin
I notice a difference in behavior between how
Chrome and MsEdge handles youtube.com vs how
Ohm handles 'clicking' on an 'on page link' .

With Chrome and MsEdge , clicking on a link
results in a change to the 'address bar' ,
showing the updated/requested URL of the 'clicked on link' .

WIth Ohm , it doesn't show the updated URL of the 'clicked on link' .
With my changes to Ohm , I need to check this requested link against a file of 'safe links' to determine if it is a 'safe link' .

I checked Ohm against youtube alternatives , and they all update the 'address bar' .

youtube is doing something to keep user on their site .

Is there a way to catch a 'requested link' for youtube.com .

Actually it might be better to capture
clicking on an 'on page link' (for any page) and
then check if it is a 'safe site' and
don't even start Navigaton if site is unsafe .
I know how to do that in javascript , but how can I do that in PB ?

I'll take another look at [url]
https://learn.microsoft.com/en-us/micro ... tures-apis?
[url]

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Mon Dec 18, 2023 9:37 am
by Justin
There is no onclick event in the webview api, you have to do it with javascript, calling a PB function in the js eventhandler with the url, check it in the PB function and depending on the result use preventDefault() on the js side.
But links can be also navigated with key presses don't know if will fire onclick.
You can also look at the beforeunload js event.

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Mon Dec 18, 2023 8:33 pm
by vmars316
infratec wrote: Sat Dec 16, 2023 6:38 pm You can look at the NavigationCallback().
Sorry to be so stupid, but I can't find a 'NavigationCallback()' .
Pls Help...

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Thu Dec 21, 2023 6:27 pm
by vmars316
TIA ,
I have modified Ohm.pb to make it Kidsafe ,
by adding TrustFile.tx (to hold Trusted Sites) .
For this I catch a Requested-Site in core_NavigationStaring .
The only Snag is youtube.com .

With Ohm Browser On youtube.com .
Youtube doesn't update Ohm's Adress-Bar ,
so it always just shows the 1st link URL on entry to youtube .

What I want to do is to capture the URL for each on-Page click .
So what I have done to (understand Ohm processing)
is to put a Debug statement into each Procedure .
So for each Procedure there is a Debug "1 procedure..."
at entry to each Procedure
and a Debug "2 Procedure...." at end of each procedure .
So now I can see program flow .

Below is the program flow for a left-click for on-page link .
"1 Procedure core_HistoryChanged" happens quite quickly .
So I am hoping to capture Requested-Link at this point .
But How can I do that ?
If impossible to do in core_HistoryChanged ,
where else can I captue Requested-Link ?
I have Posted Ohm-ONLY.pb here for viewing
https://vmars.us/freeware/Ohm-ONLY.pb

So again here's what I want to do:
Capture Requested-Link for on-page link click .
How can I capture Requested-URL ?
Thanks for your help...

Below is program-flow for Ohm.pb "click on-page link" :

1 Procedure toolBar_Proc
1 Procedure toolBar_On_Default
1 Procedure url_Edit_Proc
1 Procedure url_Edit_OnDefault
1 Procedure core_HistoryChanged
1 Procedure tab_GetCurrentBrowser
1 Procedure toolBar_UpdateNavButtons
1 Procedure btnGoBack_CallBack
1 Procedure btn_DrawBackground
2 Procedure btn_DrawBackground
1 Procedure btn_GetForeColor
2 Procedure btn_GetForeColor
2 Procedure btnGoBack_CallBack
1 Procedure btnGoForward_CallBack
1 Procedure btn_DrawBackground
2 Procedure btn_DrawBackground
1 Procedure btn_GetForeColor
2 Procedure btn_GetForeColor
Procedure
2 Procedure btnGoForward_CallBack
2 Procedure toolBar_UpdateNavButtons
2 Procedure core_HistoryChanged

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Sat Dec 23, 2023 10:11 am
by HanPBF
Hello Justin,

as new WebViewGadget with WebView2 is available in 6.10b1, is there an interface behind GadgetId(MyWebViewGadget)?
Maybe ICoreWebView2? Did not try it and I don't know if it is the intention to simple use Your code.

So, some functions yet can be used like opening the developer tools?

best regards

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Sat Dec 23, 2023 6:32 pm
by Justin
Hi,
Previously you could do GetWindowLong_(GadgetID(MyWebGadget), #GWL_USERDATA) to get the browser interface, but it does not work with the new webview engine, they will probably add some way to get it.

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Wed Dec 27, 2023 6:49 pm
by vmars316
Hi Justin ,
On youtube , every time an on-page link is clicked ,
one of the 1st things Ohm.pb does , is to go to
Procedure core_HistoryChanged(this.IWV2EventHandler, sender.ICoreWebView2, args.IUnknown)
And then to: toolBar_UpdateNavButtons
See trace below:

1 Procedure toolBar_Proc
1 Procedure toolBar_On_Default
1 Procedure url_Edit_Proc
1 Procedure url_Edit_OnDefault
1 Procedure core_HistoryChanged
args.IUnknown = 0
sender.ICoreWebView2 = 76725298905088
this.IWV2EventHandler = 81595344
1 Procedure tab_GetCurrentBrowser
1 Procedure toolBar_UpdateNavButtons

So it occurs to me that one of these Procedures
would be a good place to capture requested-link :

Code: Select all

Procedure core_HistoryChanged(this.IWV2EventHandler, sender.ICoreWebView2, args.IUnknown)
	Protected.BROWSER *browser
	Debug "1 Procedure core_HistoryChanged  " +  urlGlobal
	Debug "args.IUnknown = " + args.IUnknown
	Debug "sender.ICoreWebView2 = " + sender.ICoreWebView2
	Debug "this.IWV2EventHandler = " + this.IWV2EventHandler
	this\LockMutex()
	*browser = this\GetContext()
	;Nav buttons
	If *browser = tab_GetCurrentBrowser()
		toolBar_UpdateNavButtons(*browser)
	EndIf 
	this\UnlockMutex()
	Debug "2 Procedure core_HistoryChanged  " +  urlGlobal
EndProcedure

Code: Select all

Procedure toolBar_UpdateNavButtons(*browser.BROWSER)
	Protected.l canGoBack, canGoForward
Debug "1 Procedure toolBar_UpdateNavButtons  " +  urlGlobal
	
	If *browser And *browser\core
		*browser\core\get_CanGoBack(@canGoBack)
		gadget_Enable(app\btnGoBack, canGoBack)
		btn_Draw(app\btnGoBack)
		
		*browser\core\get_CanGoForward(@canGoForward)
		gadget_Enable(app\btnGoForward, canGoForward)
		btn_Draw(app\btnGoForward)
	EndIf 
Debug "2 Procedure toolBar_UpdateNavButtons  " +  urlGlobal
EndProcedure
I am not a Pro programmer
and I need help to capture the requested-url
from where ever it is exposed or passed around ,
possibly in here: args.IUnknown .

Thanks for your help...

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Wed Dec 27, 2023 11:33 pm
by Justin
Navigation events are not fired in youtube, i think is because it uses frames.
There is an api for that
https://learn.microsoft.com/en-us/micro ... .0.2210.55
i'll try it later

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Thu Dec 28, 2023 3:35 am
by vmars316
Thanks

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Thu Dec 28, 2023 12:22 pm
by Justin
I did some tests and the frame navigation events don't fire for all youtube links, just some of them so it's not reliable.

If you simply want to update the address bar with the youtube video clicked you have to subscribe to the SourceChanged event and then use get_Source() on ICoreWebView2, that gives you the youtube link, but that happens when the video has loaded and you can't block it.
https://learn.microsoft.com/en-us/micro ... rcechanged

If you want to block some videos you have to subscribe to the WebResourceRequested event, inspect the headers and return a 404 response for example.
https://learn.microsoft.com/en-us/micro ... erequested

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Fri Dec 29, 2023 10:31 pm
by vmars316
Justin ,
Of those two options , I think the 1st one
would work best ,
IF
It is possible to grab SourceChanged and then
check that URL against a List of approved URL's then
if not approved ,
StartNavigation to http://xxxxxxx.xxx/NotApproved.html ?

Is that possible ?
Thanks

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Posted: Sat Dec 30, 2023 2:15 pm
by infratec
It is up to you to set a new url :wink:

Re: Webview2 control - Chromium browser for Purebasic (Windows) [Jan 1, 2024]

Posted: Mon Jan 01, 2024 9:53 am
by Justin
Headers updated to last SDK and added custom scheme example, see first post.

vmars316,
The updated Ohm.pb uses SourceChanged event to update the url address wich is better.
To escape from unwanted videos you can use something like this as the event handler to escape all videos:

Code: Select all

Procedure core_SourceChanged(this.IWV2EventHandler, sender.ICoreWebView2, *args.ICoreWebView2SourceChangedEventArgs)
	Protected.BROWSER *browser
	Protected.i uri
	Protected.s suri
	
	this\LockMutex()
	sender\get_Source(@uri)
	If uri
		suri = PeekS(uri)
		str_FreeCoMemString(uri)
		If GetURLPart(suri, #PB_URL_Site) = "www.youtube.com" And FindString(suri, "watch?")
			sender\ExecuteScript(~"location.replace(\"http://xxxxxxx.xxx/NotApproved.html\")", 0)
		EndIf 
		
		url_Edit_SetText(app\url, suri)
	EndIf 
	this\UnlockMutex()
EndProcedure
You have to use location.replace to remove the unwanted link from the browser history.

Re: Webview2 control - Chromium browser for Purebasic (Windows) [Jan 1, 2024]

Posted: Thu Jan 04, 2024 2:23 am
by vmars316
Thanks Justin :
I am almost there ,
just need to know how to get this working :

Code: Select all

 *browser\core\Navigate("http://kidsafebrowser.us/Warning-UnSafe-Site.html")  
in here :

Code: Select all

Procedure IfThisLinkOk(Passed_suri$)  ; vm
  Debug "1 LINK Procedure IfThisLinkOk = " + Passed_suri$  ; vm"
  ThisLinkItemOk = #False 
  ResetList(ThisLinkList())
  While NextElement(ThisLinkList())
    CurrentElementContent$ = ThisLinkList() 
;    Debug "IfThisLinkOk  CurrentElementContent$" + CurrentElementContent$
    Position = FindString(Passed_suri$, CurrentElementContent$ , 0 ) 
    If Position > 0     
      Debug "ThisLinkItemOk = #True  " + Passed_suri$ 
      ThisLinkItemOk = #True 
    EndIf  ;  Greater than 0 == Yes its a Hit 
  Wend
  
    If ThisLinkItemOk = #False 
; GO TO:  https://kidsafebrowser.us/Warning-UnSafe-Site.html
      
      *browser\core\Navigate("http://kidsafebrowser.us/Warning-UnSafe-Site.html")  ; 

    EndIf 
EndProcedure ; IfThisLinkOk()  
I'm getting this error :

Code: Select all

With 'EnableExplicit' , variables have to be declared: *browser. 
How do I fix that ?
Thanks