Skip to content

Commit

Permalink
Added a basic ad blocker
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Oct 6, 2020
1 parent 217e4c9 commit 65f4fad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
27 changes: 26 additions & 1 deletion QuickDictionary/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using System.Drawing;
using System.Windows.Media.Animation;
using Squirrel;
using CefSharp.Handler;
using CefSharp;

namespace QuickDictionary
{
Expand Down Expand Up @@ -62,6 +64,8 @@ public MainWindow()

Title = "Quick Dictionary v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
title = Title;

browser.RequestHandler = new AdBlockRequestHandler();
}

List<OCRWord> OCRWords = new List<OCRWord>();
Expand Down Expand Up @@ -184,6 +188,7 @@ private async void Overlay_WordSelected(object sender, System.Windows.Point posi
public event PropertyChangedEventHandler PropertyChanged;

private ObservableCollection<Dictionary> dictionaries = new ObservableCollection<Dictionary>();
public static List<string> Adhosts = new List<string>();

private async void Window_SourceInitialized(object sender, EventArgs e)
{
Expand All @@ -193,9 +198,19 @@ private async void Window_SourceInitialized(object sender, EventArgs e)
var windowClipboardManager = new ClipboardManager(this);
windowClipboardManager.ClipboardChanged += ClipboardChanged;

WebClient client = new WebClient();
Stream stream = client.OpenRead("https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt");
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
var matches = Regex.Matches(content, @"0\.0\.0\.0 (.+)");
foreach (Match match in matches)
{
Adhosts.Add(match.Groups[1].Value);
}

using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/Henry-YSLin/QuickDictionary"))
{
await mgr.UpdateApp((progress) => Dispatcher.Invoke(() => Title = title + (progress > 95 ? "" : $" - Updating {progress}%")));
await mgr.UpdateApp((progress) => Dispatcher.Invoke(() => Title = title + (progress >= 99 ? "" : $" - Updating {progress}%")));
}
}

Expand Down Expand Up @@ -266,6 +281,16 @@ private void btnOCR_Click(object sender, RoutedEventArgs e)
{
startOCR();
}

public class AdBlockRequestHandler : RequestHandler
{
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
bool block = Adhosts.Exists(x => request.Url.Contains(x));
if (block) Console.WriteLine("BLOCKED: " + request.Url);
return block;
}
}
}

public class OCRWord
Expand Down
4 changes: 2 additions & 2 deletions QuickDictionary/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
2 changes: 1 addition & 1 deletion QuickDictionary/QuickDictionary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.1.0</ApplicationVersion>
<ApplicationVersion>1.0.2.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down

0 comments on commit 65f4fad

Please sign in to comment.