2022-12-02 18:43:43 +01:00
Option Strict On
Imports System . IO
Imports System . ComponentModel
Imports System . Threading
Imports System . Net
Imports System . Net . WebUtility
Imports System . IO . Compression
Imports System . Text
Imports System . Security . Policy
Imports Microsoft . Web . WebView2 . Core
Imports MetroFramework . Drawing
2023-02-07 14:31:23 +01:00
Imports Newtonsoft . Json . Linq
2022-12-02 18:43:43 +01:00
Public Class Browser
Private Sub WebView2_CoreWebView2InitializationCompleted ( sender As Object , e As CoreWebView2InitializationCompletedEventArgs ) Handles WebView2 . CoreWebView2InitializationCompleted
2023-07-11 18:53:50 +02:00
Try
WebView2 . CoreWebView2 . AddWebResourceRequestedFilter ( " https://www.crunchyroll.com/* " , CoreWebView2WebResourceContext . All )
WebView2 . CoreWebView2 . AddWebResourceRequestedFilter ( " https://www.funimation.com/* " , CoreWebView2WebResourceContext . All )
2023-05-04 18:48:37 +02:00
2023-07-11 18:53:50 +02:00
'WebView2.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All)
AddHandler WebView2 . CoreWebView2 . WebResourceResponseReceived , AddressOf ObserveResponse
AddHandler WebView2 . CoreWebView2 . WebResourceRequested , AddressOf ObserveHttp
'WebView2.CoreWebView2.Settings.UserAgent = My.Settings.User_Agend.Replace(Chr(34), "").Replace("User-Agent: ", "")
'
'WebView2.CoreWebView2.Settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko"
My . Settings . User_Agend = Chr ( 34 ) + " User-Agent: " + WebView2 . CoreWebView2 . Settings . UserAgent + Chr ( 34 )
'MsgBox(My.Settings.User_Agend)
2023-06-03 15:57:13 +02:00
2023-07-11 18:53:50 +02:00
If WebView2 . CoreWebView2 . Source = " about:blank " Or WebView2 . CoreWebView2 . Source = Nothing Then
'TextBox1.Text = Main.Startseite
WebView2 . CoreWebView2 . Navigate ( Main . Startseite )
2022-12-02 18:43:43 +01:00
2023-06-03 15:57:13 +02:00
2023-07-11 18:53:50 +02:00
End If
Catch ex As Exception
2022-12-02 18:43:43 +01:00
2023-07-11 18:53:50 +02:00
End Try
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
End Sub
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
Private Sub WebView2_SourceChanged ( sender As Object , e As CoreWebView2SourceChangedEventArgs ) Handles WebView2 . SourceChanged
Try
TextBox1 . Text = WebView2 . CoreWebView2 . Source
Catch ex As Exception
End Try
End Sub
Private Sub WebView2_NavigationCompleted ( sender As Object , e As CoreWebView2NavigationCompletedEventArgs ) Handles WebView2 . NavigationCompleted
' Dim HTML As String = WebView2.CoreWebView2.
'TextBox1.Text = WebView2.CoreWebView2.Source
' Exit Sub
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
If e . HttpStatusCode = 200 Then
Dim DocumentTitle As String = WebView2 . CoreWebView2 . DocumentTitle
Debug . WriteLine ( " NavigationCompleted: " + Date . Now . ToString )
Main . WebbrowserURL = WebView2 . CoreWebView2 . Source
TextBox1 . Text = Main . WebbrowserURL
Main . WebbrowserTitle = DocumentTitle
Main . ProcessHTML ( " " , Main . WebbrowserURL , DocumentTitle )
GetCookies ( Main . WebbrowserURL )
2023-06-03 15:57:13 +02:00
Main . BowserWasOpen = True
If Application . OpenForms ( ) . OfType ( Of Anime_Add ) . Any = True Then
Anime_Add . btn_dl . Cursor = Cursors . Default
Anime_Add . btn_dl . BackgroundImage = My . Resources . main_button_download_default
End If
2023-07-11 18:53:50 +02:00
If Main . Startseite IsNot My . Settings . Startseite Then
Main . LoadBrowser ( Main . Startseite , 1 )
Main . Startseite = My . Settings . Startseite
End If
2022-12-18 20:28:02 +01:00
End If
2022-12-02 18:43:43 +01:00
End Sub
Public Async Sub GetCookies ( ByVal Uri As String )
2023-01-05 14:26:40 +01:00
Try
Main . CookieList = Await WebView2 . CoreWebView2 . CookieManager . GetCookiesAsync ( Uri )
Catch ex As Exception
End Try
2022-12-02 18:43:43 +01:00
End Sub
2022-12-05 18:16:07 +01:00
Private Sub Browser_Load ( sender As Object , e As EventArgs ) Handles Me . Load
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
Main . waveOutSetVolume ( 0 , 0 )
If Me . Width > My . Computer . Screen . Bounds . Width Then
Me . Width = My . Computer . Screen . Bounds . Width
Panel1 . Width = Me . Size . Width - 15 ', Me.Size.Height - 69)
Panel1 . Location = New Point ( 0 , 30 )
TextBox1 . Width = My . Computer . Screen . Bounds . Width - 435
End If
If Me . Size . Height > My . Computer . Screen . Bounds . Height Then
Me . Height = My . Computer . Screen . Bounds . Height
Panel1 . Height = Me . Size . Height - 69
Panel1 . Location = New Point ( 0 , 30 )
End If
Try
Me . Icon = My . Resources . icon
Catch ex As Exception
End Try
If Main . UserBowser = False Then
Me . Location = New Point ( - 10000 , 10000 )
2023-02-07 14:31:23 +01:00
Timer1 . Enabled = True
2022-12-02 18:43:43 +01:00
End If
WebView2 . Source = New Uri ( Main . Startseite )
End Sub
Private Sub Browser_FormClosing ( sender As Object , e As FormClosingEventArgs ) Handles Me . FormClosing
2023-06-03 15:57:13 +02:00
Main . BowserWasOpen = False
2022-12-02 18:43:43 +01:00
'Main.UserBowser = False
'Me.Location = New Point(-10000, -10000)
'Main.LoadingUrl = ""
'Debug.WriteLine("Collecting")
'Dim Collector As New TaskCookieVisitor
'Dim CM As ICookieManager = WebBrowser1.GetCookieManager
'CM.VisitAllCookies(Collector)
'Main.CookieList = Collector.Task.Result()
'Debug.WriteLine("Collecting-end")
''e.Cancel = True
End Sub
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
Private Sub Button1_Click ( sender As Object , e As EventArgs ) Handles Button1 . Click
'MsgBox(Main.CR_etp_rt)
'MsgBox(Main.CR_ajs_user_id)
'MsgBox(Main.CheckCRLogin.ToString)
2023-06-03 15:57:13 +02:00
2022-12-02 18:43:43 +01:00
Try
My . Computer . Clipboard . SetText ( WebView2 . CoreWebView2 . Source )
MsgBox ( " copied: " + Chr ( 34 ) + WebView2 . CoreWebView2 . Source + Chr ( 34 ) )
Catch ex As Exception
End Try
'My.Computer.Clipboard.SetText(WebBrowser1.Document.Body.InnerHtml)
End Sub
Private Sub TextBox1_KeyDown ( sender As Object , e As KeyEventArgs ) Handles TextBox1 . KeyDown
Try
If e . KeyCode = Keys . Return Then
e . SuppressKeyPress = True
Debug . WriteLine ( " Start loading: " + Date . Now . ToString )
WebView2 . CoreWebView2 . Navigate ( TextBox1 . Text )
End If
Catch ex As Exception
MsgBox ( " Error in URL " , MsgBoxStyle . Critical )
End Try
End Sub
2022-12-05 18:16:07 +01:00
Private Async Sub ObserveResponse ( ByVal sender As Object , ByVal e As CoreWebView2WebResourceResponseReceivedEventArgs )
2022-12-02 18:43:43 +01:00
2022-12-05 18:16:07 +01:00
If CBool ( InStr ( Main . LoadingUrl , " crunchyroll.com " ) ) Then
2022-12-02 18:43:43 +01:00
2023-02-07 14:31:23 +01:00
If CBool ( InStr ( e . Request . Uri , " crunchyroll.com/ " ) ) And CBool ( InStr ( e . Request . Uri , " v1/token " ) ) And Main . CR_v1Token = " Get " Then
Debug . WriteLine ( " Crunchyroll-v1_token: " + e . Request . Uri )
Dim Content As Stream = Await e . Response . GetContentAsync
Dim ContentString As String = Nothing
Dim reader As New StreamReader ( Content )
ContentString = reader . ReadToEnd
Dim Loc_CR_Cookies = " -H " + Chr ( 34 ) + Main . CR_Cookies + Chr ( 34 )
Dim Token ( ) As String = ContentString . Split ( New String ( ) { Chr ( 34 ) + " access_token " + Chr ( 34 ) + " : " + Chr ( 34 ) } , System . StringSplitOptions . RemoveEmptyEntries )
Dim Token2 ( ) As String = Token ( 1 ) . Split ( New String ( ) { Chr ( 34 ) + " , " + Chr ( 34 ) } , System . StringSplitOptions . RemoveEmptyEntries )
Dim Auth As String = " Bearer " + Token2 ( 0 )
Dim Auth2 As String = " -H " + Chr ( 34 ) + " Authorization: " + Auth + Chr ( 34 )
2023-06-03 15:57:13 +02:00
Main . ProcessLoading ( Main . LoadingUrl , Auth2 , Loc_CR_Cookies , 0 )
2023-02-07 14:31:23 +01:00
Main . CR_v1Token = " "
Exit Sub
2022-12-18 20:28:02 +01:00
2023-02-07 14:31:23 +01:00
ElseIf CBool ( InStr ( e . Request . Uri , " crunchyroll.com/ " ) ) And CBool ( InStr ( e . Request . Uri , " seasons?preferred_audio_language= " ) ) And CBool ( InStr ( Main . LoadingUrl , " /series/ " ) ) Then
Debug . WriteLine ( " Crunchyroll-Season: " + e . Request . Uri )
Main . LoadedUrls . Add ( e . Request )
2022-12-18 20:28:02 +01:00
2022-12-05 18:16:07 +01:00
Exit Sub
2023-06-03 15:57:13 +02:00
'ElseIf CBool(InStr(e.Request.Uri, "crunchyroll.com/")) And CBool(InStr(e.Request.Uri, "content/v2/cms/objects/")) Then
' Debug.WriteLine("Crunchyroll-objects added to cache: " + e.Request.Uri)
' Main.LoadedUrls.Add(e.Request)
' Exit Sub
'ElseIf CBool(InStr(e.Request.Uri, "crunchyroll.com/")) And CBool(InStr(e.Request.Uri, "seasons?preferred_audio_language=")) And CBool(InStr(Main.LoadingUrl, "/series/")) Then
' Debug.WriteLine("Crunchyroll-objects added to cache: " + e.Request.Uri)
' Main.LoadedUrls.Add(e.Request)
' Exit Sub
2022-12-27 17:04:25 +01:00
End If
2023-06-03 15:57:13 +02:00
2022-12-05 18:16:07 +01:00
ElseIf CBool ( InStr ( Main . LoadingUrl , " funimation.com " ) ) Then
If CBool ( InStr ( e . Request . Uri , " ?deviceType=web " ) ) Then
'Debug.WriteLine(e.Request.Uri)
Dim parms As String ( ) = e . Request . Uri . Split ( New String ( ) { " ?deviceType= " } , System . StringSplitOptions . RemoveEmptyEntries )
Main . FunimationDeviceRegion = " ?deviceType= " + parms ( 1 )
End If
If CBool ( InStr ( e . Request . Uri , " https://title-api.prd.funimationsvc.com " ) ) Then
Debug . WriteLine ( " Funimtaion: " + e . Request . Uri )
2022-12-02 18:43:43 +01:00
If ( Me . InvokeRequired ) Then
2022-12-18 20:28:02 +01:00
Me . Invoke ( Sub ( ) Main . LoadedUrls . Add ( e . Request ) )
2022-12-02 18:43:43 +01:00
Exit Sub
Else
2022-12-18 20:28:02 +01:00
Main . LoadedUrls . Add ( e . Request )
2022-12-02 18:43:43 +01:00
Exit Sub
End If
2022-12-05 18:16:07 +01:00
ElseIf CBool ( InStr ( e . Request . Uri , " /data/v2/shows/ " ) ) Then
Debug . WriteLine ( " Funimtaion: " + e . Request . Uri )
2022-12-02 18:43:43 +01:00
If ( Me . InvokeRequired ) Then
2022-12-18 20:28:02 +01:00
Me . Invoke ( Sub ( ) Main . LoadedUrls . Add ( e . Request ) )
2022-12-02 18:43:43 +01:00
Exit Sub
Else
2022-12-18 20:28:02 +01:00
Main . LoadedUrls . Add ( e . Request )
2022-12-02 18:43:43 +01:00
Exit Sub
End If
2022-12-05 18:16:07 +01:00
ElseIf CBool ( InStr ( e . Request . Uri , " /data/v1/episodes/ " ) ) Then
Debug . WriteLine ( " Funimtaion: " + e . Request . Uri )
2022-12-02 18:43:43 +01:00
If ( Me . InvokeRequired ) Then
2022-12-18 20:28:02 +01:00
Me . Invoke ( Sub ( ) Main . LoadedUrls . Add ( e . Request ) )
2022-12-02 18:43:43 +01:00
Exit Sub
Else
2022-12-18 20:28:02 +01:00
Main . LoadedUrls . Add ( e . Request )
2022-12-02 18:43:43 +01:00
Exit Sub
End If
End If
2022-12-05 18:16:07 +01:00
End If
End Sub
Private Sub ObserveHttp ( ByVal sender As Object , ByVal e As CoreWebView2WebResourceRequestedEventArgs ) 'Handles RequestResource.GetUrl
If CBool ( InStr ( e . Request . Uri , " crunchyroll.com " ) ) = True And Main . CrBetaBasic = Nothing Then
Dim Headers As New List ( Of KeyValuePair ( Of String , String ) )
Headers . AddRange ( e . Request . Headers . ToList )
For i As Integer = 0 To Headers . Count
If CBool ( InStr ( Headers . Item ( i ) . Value , " Basic " ) ) Then
Main . CrBetaBasic = Headers . Item ( i ) . Value
2022-12-18 20:28:02 +01:00
Debug . WriteLine ( " Auth-Basic: " + Main . CrBetaBasic )
ElseIf CBool ( InStr ( Headers . Item ( i ) . Value , " Bearer " ) ) Then
Main . CR_AuthToken = Headers . Item ( i ) . Value
Debug . WriteLine ( " Auth-Bearer: " + Main . CR_AuthToken )
End If
Next
End If
2023-06-03 15:57:13 +02:00
'If CBool(InStr(e.Request.Uri, "crunchyroll.com/")) And CBool(InStr(e.Request.Uri, "streams?")) Then
' Dim Headers As New List(Of KeyValuePair(Of String, String))
' Headers.AddRange(e.Request.Headers.ToList)
' For i As Integer = 0 To Headers.Count
' If CBool(InStr(Headers.Item(i).Value, "Bearer")) Then
' Main.CR_AuthToken = Headers.Item(i).Value
' Debug.WriteLine("Auth-Bearer: " + Main.CR_AuthToken)
' End If
' Next
'End If
2022-12-02 18:43:43 +01:00
2023-06-03 15:57:13 +02:00
'
'
If CBool ( InStr ( e . Request . Uri , " 9016.2bd48f1e07adf6596b2d.js " ) ) = True Then '
Debug . WriteLine ( e . Request . Uri )
'MsgBox("Found!")
e . Response = WebView2 . CoreWebView2 . Environment . CreateWebResourceResponse ( StringToStream ( File . ReadAllText ( Application . StartupPath + " \WebInterface\9016.2bd48f1e07adf6596b2d.js " ) , Encoding . UTF8 ) , 200 , " Not found " , " content-type: application/javascript " )
2022-12-02 18:43:43 +01:00
2023-06-03 15:57:13 +02:00
End If
2022-12-02 18:43:43 +01:00
2023-06-03 15:57:13 +02:00
End Sub
2022-12-02 18:43:43 +01:00
2023-06-03 15:57:13 +02:00
Public Function StringToStream ( input As String , enc As Encoding ) As Stream
Dim memoryStream = New MemoryStream ( )
Dim streamWriter = New StreamWriter ( memoryStream , enc )
streamWriter . Write ( input )
streamWriter . Flush ( )
memoryStream . Position = 0
Return memoryStream
End Function
2022-12-02 18:43:43 +01:00
2023-02-07 14:31:23 +01:00
Private Sub Timer1_Tick ( sender As Object , e As EventArgs ) Handles Timer1 . Tick
'If Main.UserBowser = False Then
' WebView2.Reload()
'Else
' Timer1.Enabled = False
'End If
2022-12-02 18:43:43 +01:00
2023-02-07 14:31:23 +01:00
End Sub
2022-12-02 18:43:43 +01:00
Private Sub Timer2_Tick ( sender As Object , e As EventArgs ) Handles Timer2 . Tick
Me . Close ( )
End Sub
End Class