Added cookies pop-up that you have to accept

This commit is contained in:
BOT Alex 2023-05-29 01:47:43 +02:00
parent 6cb976fc11
commit 4e2158aea4
10 changed files with 156 additions and 13 deletions

View File

@ -5,7 +5,6 @@
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@inject NavigationManager NavManager
@inject ISnackbar Snackbar
@inject DataCollectorService DCollector
<MudLayout Class="overflow-hidden">
<MudAppBar Elevation="1">
@ -125,8 +124,7 @@
protected async Task DebugButton()
{
ipAddress = (await DCollector.GetBrowserInfo()).ToString();
Console.WriteLine(ipAddress);
}
#region Drawers and overlays
@ -206,6 +204,7 @@
async void Logout()
{
await localStorage.RemoveItemAsync("User");
await localStorage.RemoveItemAsync("AcceptedCookies");
RefreshPage();
}
@ -253,10 +252,36 @@
protected override async Task OnInitializedAsync()
{
StateHasChanged();
NavManager.LocationChanged += OnNavigated;
await CheckCookiesCheck();
CheckLoggedin();
}
async void OnNavigated(object sender, LocationChangedEventArgs e)
{
await CheckCookiesCheck();
}
async Task CheckCookiesCheck()
{
string relativeUri = NavManager.ToBaseRelativePath(NavManager.Uri);
relativeUri = (relativeUri == "") ? "Bypassss" : relativeUri;
if (!(new string[] { "Privacy", "Cookies", "TOS" }.Any(relativeUri.Contains)))
{
bool? haveAcceptedCookies = await localStorage.GetItemAsync<bool?>("AcceptedCookies");
if (haveAcceptedCookies == null || !(bool)haveAcceptedCookies)
{
NavManager.LocationChanged -= OnNavigated;
NavManager.NavigateTo("/Cookies");
NavManager.LocationChanged += OnNavigated;
await Task.Delay(500);
if (relativeUri != "Bypassss")
Snackbar.Add("Cookies not accepted, detected!", Severity.Warning);
}
}
}
async void CheckLoggedin()
{
string? name = await localStorage.GetItemAsStringAsync("User");
@ -275,7 +300,7 @@
private void RefreshPage()
{
NavManager.NavigateTo(NavManager.Uri, forceLoad: true);
NavManager.NavigateTo("/", forceLoad: true);
}
public class User

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazor-Analytics" Version="3.11.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.4" PrivateAssets="all" />

View File

@ -128,7 +128,7 @@
}
bool isLooping = false;
private async Task StartLoop()
private async void StartLoop()
{
if (isLooping)
return;
@ -140,7 +140,7 @@
messages = await messageService.GetMessages();
Console.WriteLine("Chat updated!");
await Task.Delay(10000);
await Task.Delay(20000);
}
}

View File

@ -0,0 +1,39 @@
@page "/Cookies"
@inject NavigationManager NavManager
@inject Blazored.LocalStorage.ILocalStorageService localStorage
<MudOverlay ZIndex="1400" DarkBackground="true" Visible=true Style="backdrop-filter:blur(5px)">
<MudCard>
<MudCardMedia Image="images/CookiesImage.jpg" Height="200" />
<MudCardContent>
<MudText Typo="Typo.h5">Cookies, local storage, and Privacy policy</MudText>
<MudText Typo="Typo.body1">This site <b>does not</b> use cookies!</MudText>
<MudText Typo="Typo.body2">This uses local storage, instead.</MudText>
<br/>
<MudText Typo="Typo.body2">Do you agree to our privacy policy and accept the use of local storage?</MudText>
</MudCardContent>
<MudCardActions Class="justify-end">
<MudButton Variant="Variant.Text" Color="Color.Primary" OnClick='()=>NavManager.NavigateTo("/Privacy")'>Privacy policy</MudButton>
<MudSpacer/>
<MudButton Variant="Variant.Text" Color="Color.Success" OnClick="AcceptedCookies">Accept</MudButton>
<MudButton Variant="Variant.Text" Color="Color.Error" OnClick='()=>NavManager.NavigateTo("https://www.youtube.com/watch?v=gokdrC4gQA4", replace: true)'>Decline</MudButton>
</MudCardActions>
</MudCard>
</MudOverlay>
@code{
protected override async Task OnInitializedAsync()
{
bool? haveAcceptedCookies = await localStorage.GetItemAsync<bool?>("AcceptedCookies");
if (haveAcceptedCookies == null) return;
if (!(bool)haveAcceptedCookies) return;
NavManager.NavigateTo("/");
}
async void AcceptedCookies()
{
await localStorage.SetItemAsync<bool>("AcceptedCookies", true);
NavManager.NavigateTo("/Posts");
}
}

View File

@ -1,11 +1,13 @@
@page "/"
@inject NavigationManager NavManager
@inject DataCollectorService DCollector
<h1>2nd loading... Please wait</h1>
@code {
protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
NavManager.NavigateTo("/Posts");
//await DCollector.PushDataToAnalytics();
}
}

View File

@ -8,6 +8,7 @@ using System.Security.Cryptography;
using System.Text;
using MudBlazor;
using Otakians.Services;
using Blazor.Analytics;
internal class Program
{
@ -25,6 +26,8 @@ internal class Program
builder.Services.AddBlazoredLocalStorage(config =>
config.JsonSerializerOptions.WriteIndented = true); // local storage
builder.Services.AddGoogleAnalytics("GT-TB78HCS");
builder.Services.AddScoped<MessageService>();
builder.Services.AddScoped<DataCollectorService>();

View File

@ -1,8 +1,11 @@
using DataStructs;
using Otakians.DataStructs;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Threading.Tasks;
using System;
using System.Drawing;
using System.Text.Json;
using System.Text;
namespace Otakians.Services
{
@ -10,11 +13,13 @@ namespace Otakians.Services
{
private readonly IJSRuntime jsRuntime;
private readonly NavigationManager navigationManager;
private readonly HttpClient httpClient;
public DataCollectorService(IJSRuntime jsRuntime, NavigationManager navigation)
public DataCollectorService(IJSRuntime jsRuntime, NavigationManager navigation, HttpClient httpClient)
{
this.jsRuntime = jsRuntime;
this.navigationManager = navigation;
this.httpClient = httpClient;
}
public async Task<string> GetIPAddress()
@ -27,10 +32,43 @@ namespace Otakians.Services
var browserInfo = await jsRuntime.InvokeAsync<BrowserInfo>("getBrowserInfo");
return browserInfo;
}
private async Task<Geolocation> GetGeolocation()
{
var position = await jsRuntime.InvokeAsync<Geolocation>("getCurrentPosition");
return position;
}
public async Task PushDataToAnalytics()
{
if (string.IsNullOrEmpty(Program.LoggedInName))
{
await Console.Out.WriteLineAsync("User not logged in. Canceling data push");
return;
}
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var analyticsUrl = "https://globalchat-4a89f-default-rtdb.europe-west1.firebasedatabase.app/Database/Analytics.json";
var dataObject = new
{
Username = Program.LoggedInName,
Time = currentTime,
Data = new
{
ipAddress = await GetIPAddress(),
browserInfo = await GetBrowserInfo()
}
};
var jsonContent = new StringContent(JsonSerializer.Serialize(dataObject), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(analyticsUrl, jsonContent);
}
}
}
namespace DataStructs
namespace Otakians.DataStructs
{
public class BrowserInfo
{
@ -56,4 +94,11 @@ namespace DataStructs
}
}
public class Geolocation
{
public double? latitude { get; set; }
public double? longitude { get; set; }
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -30,6 +30,18 @@
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<script src="/jsFunctions.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-W0L355ZGK7"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-W0L355ZGK7');
</script>
</body>
</html>

View File

@ -19,3 +19,19 @@ function getBrowserInfo() {
userAgent: navigator.userAgent
};
}
window.getCurrentPosition = () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
position => {
resolve({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
},
error => {
reject(error);
}
);
});
};