diff --git a/Moonlight/App/Configuration/ConfigV1.cs b/Moonlight/App/Configuration/ConfigV1.cs index 7818130..62aab2e 100644 --- a/Moonlight/App/Configuration/ConfigV1.cs +++ b/Moonlight/App/Configuration/ConfigV1.cs @@ -17,6 +17,14 @@ public class ConfigV1 [JsonProperty("Store")] public StoreData Store { get; set; } = new(); [JsonProperty("Theme")] public ThemeData Theme { get; set; } = new(); + [JsonProperty("Advertisement")] public AdvertisementData Advertisement { get; set; } = new(); + + public class AdvertisementData + { + [JsonProperty("PreventAdBlockers")] + [Description("This prevents users from using ad blockers while using moonlight. (Note: The detection might not always work)")] + public bool PreventAdBlockers { get; set; } + } public class ThemeData { [JsonProperty("EnableDefault")] public bool EnableDefault { get; set; } = true; diff --git a/Moonlight/App/Services/Interop/AdBlockService.cs b/Moonlight/App/Services/Interop/AdBlockService.cs new file mode 100644 index 0000000..074f807 --- /dev/null +++ b/Moonlight/App/Services/Interop/AdBlockService.cs @@ -0,0 +1,29 @@ +using Microsoft.JSInterop; +using Moonlight.App.Helpers; + +namespace Moonlight.App.Services.Interop; + +public class AdBlockService +{ + private readonly IJSRuntime JsRuntime; + + public AdBlockService(IJSRuntime jsRuntime) + { + JsRuntime = jsRuntime; + } + + public async Task Detect() + { + try + { + return await JsRuntime.InvokeAsync("moonlight.utils.vendo"); // lat. vendo = advertisement xd + } + catch (Exception e) + { + Logger.Warn("An unexpected error occured while trying to detect possible ad blockers"); + Logger.Warn(e); + + return false; + } + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 8a45501..94009dd 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -64,6 +64,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Services / Store builder.Services.AddScoped(); diff --git a/Moonlight/Shared/Components/Alerts/AdBlockAlert.razor b/Moonlight/Shared/Components/Alerts/AdBlockAlert.razor new file mode 100644 index 0000000..0032bca --- /dev/null +++ b/Moonlight/Shared/Components/Alerts/AdBlockAlert.razor @@ -0,0 +1,16 @@ +
+
+
+

+ AdBlocker detected +

+
+ We have detected that you are using an adblocker. Disable your adblocker or add this site as an exception and reload the page to proceed +
+
+ + +
+
\ No newline at end of file diff --git a/Moonlight/Shared/Components/Alerts/RestartAlert.razor b/Moonlight/Shared/Components/Alerts/RestartAlert.razor index b383513..5db8a7f 100644 --- a/Moonlight/Shared/Components/Alerts/RestartAlert.razor +++ b/Moonlight/Shared/Components/Alerts/RestartAlert.razor @@ -1,6 +1,4 @@ -@inject NavigationManager Navigation - -
+

diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index 993bdd0..cb58c9f 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -14,6 +14,7 @@ @inject SessionService SessionService @inject NavigationManager Navigation @inject ConnectionService ConnectionService +@inject AdBlockService AdBlockService @{ var url = new Uri(Navigation.Uri); @@ -41,6 +42,12 @@ } + else if (ConfigService.Get().Advertisement.PreventAdBlockers && AdBlockerDetected) + { + + + + } else { @@ -100,6 +107,7 @@ else { private bool Initialized = false; private bool RestartLock = false; + private bool AdBlockerDetected = false; private Session? MySession; @@ -147,6 +155,9 @@ else }; await SessionService.Register(MySession); + + if(ConfigService.Get().Advertisement.PreventAdBlockers) + AdBlockerDetected = await AdBlockService.Detect(); Initialized = true; diff --git a/Moonlight/wwwroot/js/moonlight.js b/Moonlight/wwwroot/js/moonlight.js index 29a5d14..4a7a659 100644 --- a/Moonlight/wwwroot/js/moonlight.js +++ b/Moonlight/wwwroot/js/moonlight.js @@ -120,6 +120,25 @@ window.moonlight = { anchorElement.click(); anchorElement.remove(); URL.revokeObjectURL(url); + }, + vendo: function () + { + try + { + var request = new XMLHttpRequest(); + + request.open("GET", "https://pagead2.googlesyndication.com/pagead/js/aidsbygoogle.js?client=ca-pub-1234567890123456", false); + request.send(); + + if(request.status === 404) + return false; + + return true; + } + catch (e) + { + return false; + } } }, textEditor: {