Skip to content

Commit

Permalink
Add user education, ad hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeroday committed May 20, 2016
1 parent dfab23d commit e65a875
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion EZBlocker/EZBlocker/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
<userSettings>
<EZBlocker.Properties.Settings>
<setting name="UID" serializeAs="String">
<value/>
<value />
</setting>
<setting name="UpdateSettings" serializeAs="String">
<value>True</value>
</setting>
<setting name="SpotifyMute" serializeAs="String">
<value>True</value>
</setting>
<setting name="UserEducated" serializeAs="String">
<value>False</value>
</setting>
</EZBlocker.Properties.Settings>
</userSettings>
</configuration>
6 changes: 3 additions & 3 deletions EZBlocker/EZBlocker/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 35 additions & 13 deletions EZBlocker/EZBlocker/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public partial class Main : Form
private string volumeMixerPath = Environment.GetEnvironmentVariable("WINDIR") + @"\System32\SndVol.exe";
private string hostsPath = Environment.GetEnvironmentVariable("WINDIR") + @"\System32\drivers\etc\hosts";

private string[] adHosts = { "pubads.g.doubleclick.net", "securepubads.g.doubleclick.net", "www.googletagservices.com", "gads.pubmatic.com"};
private string[] adHosts = { "pubads.g.doubleclick.net", "securepubads.g.doubleclick.net", "www.googletagservices.com", "gads.pubmatic.com", "ads.pubmatic.com"};

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("shell32.dll")]
public static extern bool IsUserAnAdmin();
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string text);

// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646275%28v=vs.85%29.aspx
private const int WM_APPCOMMAND = 0x319;
Expand Down Expand Up @@ -72,7 +74,8 @@ public Main()
private void MainTimer_Tick(object sender, EventArgs e)
{
try {
if (Process.GetProcessesByName("spotify").Length < 1)
IntPtr handle = GetHandle();
if (handle == IntPtr.Zero)
{
File.AppendAllText(logPath, "Spotify process not found\r\n");
Notify("Exiting EZBlocker.");
Expand Down Expand Up @@ -276,6 +279,7 @@ private void CheckUpdate()
if (File.Exists(coreaudioPath)) File.Delete(coreaudioPath);
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.UserEducated = false;
Properties.Settings.Default.Save();
}
catch (Exception ex)
Expand Down Expand Up @@ -378,6 +382,7 @@ protected override void WndProc(ref Message m)
{
this.Activate();
}
Notify("EZBlocker is already open.");
}
base.WndProc(ref m);
}
Expand Down Expand Up @@ -414,7 +419,6 @@ private void SpotifyMuteCheckBox_CheckedChanged(object sender, EventArgs e)
{
spotifyMute = SpotifyMuteCheckbox.Checked;
if (visitorId == null) return; // Still setting up UI
if (!spotifyMute) MessageBox.Show("You may need to restart Spotify for this to take effect.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Information);
LogAction("/settings/spotifyMute/" + spotifyMute.ToString());
Properties.Settings.Default.SpotifyMute = spotifyMute;
Properties.Settings.Default.Save();
Expand All @@ -425,7 +429,7 @@ private void SkipAdsCheckbox_Click(object sender, EventArgs e)
if (visitorId == null) return; // Still setting up UI
if (!IsUserAnAdmin())
{
MessageBox.Show("Enabling/Disabling this option requires Administrator privilages.\n\nPlease reopen EZBlocker with \"Run as Administrator\".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Enabling/Disabling this option requires Administrator privileges.\n\nPlease reopen EZBlocker with \"Run as Administrator\".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
BlockBannersCheckbox.Checked = !BlockBannersCheckbox.Checked;
return;
}
Expand All @@ -435,6 +439,11 @@ private void SkipAdsCheckbox_Click(object sender, EventArgs e)
}
try
{
// Always clear hosts
string[] text = File.ReadAllLines(hostsPath);
text = text.Where(line => !adHosts.Contains(line.Replace("0.0.0.0 ", "")) && line.Length > 0).ToArray();
File.WriteAllLines(hostsPath, text);

if (BlockBannersCheckbox.Checked)
{
using (StreamWriter sw = File.AppendText(hostsPath))
Expand All @@ -446,12 +455,6 @@ private void SkipAdsCheckbox_Click(object sender, EventArgs e)
}
}
}
else
{
string[] text = File.ReadAllLines(hostsPath);
text = text.Where(line => !adHosts.Contains(line.Replace("0.0.0.0 ", ""))).ToArray();
File.WriteAllLines(hostsPath, text);
}
MessageBox.Show("You may need to restart Spotify or your computer for this setting to take effect.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Information);
LogAction("/settings/blockBanners/" + BlockBannersCheckbox.Checked.ToString());
}
Expand Down Expand Up @@ -481,6 +484,7 @@ private void MuteButton_Click(object sender, EventArgs e)

private void WebsiteLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("Please leave a comment describing one of these problems:\r\n\r\n1. Audio ads are not muted\r\n2. Audio ads are not blocked but muted\r\n3. Banner ads are not blocked\r\n\r\nNot using one of these will cause your comment to be deleted.\r\n\r\nPlease note that #2 and #3 are experimental features and not guaranteed to work.", "EZBlocker");
Process.Start(website);
LogAction("/button/website");
}
Expand All @@ -502,10 +506,12 @@ private void Main_Load(object sender, EventArgs e)
}
}

CheckUpdate();

// Start Spotify and give EZBlocker higher priority
try
{
if (File.Exists(spotifyPath))
if (File.Exists(spotifyPath) && GetHandle() == IntPtr.Zero)
{
Process.Start(spotifyPath);
}
Expand Down Expand Up @@ -541,7 +547,7 @@ private void Main_Load(object sender, EventArgs e)
if (File.Exists(hostsPath))
{
string hostsFile = File.ReadAllText(hostsPath);
BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains(host));
BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains("0.0.0.0 " + host));
}

// Google Analytics
Expand Down Expand Up @@ -574,8 +580,24 @@ private void Main_Load(object sender, EventArgs e)
MainTimer.Enabled = true;

LogAction("/launch");
}

CheckUpdate();
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!Properties.Settings.Default.UserEducated)
{
var result = MessageBox.Show("Spotify ads will not be blocked if EZBlocker is not running. Are you sure you want to exit?", "EZBlocker",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);

e.Cancel = (result == DialogResult.No);

if (result == DialogResult.Yes)
{
Properties.Settings.Default.UserEducated = true;
Properties.Settings.Default.Save();
}
}
}
}
}
4 changes: 2 additions & 2 deletions EZBlocker/EZBlocker/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,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.6.5.0")]
[assembly: AssemblyFileVersion("1.6.5.0")]
[assembly: AssemblyVersion("1.6.6.0")]
[assembly: AssemblyFileVersion("1.6.6.0")]
12 changes: 12 additions & 0 deletions EZBlocker/EZBlocker/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions EZBlocker/EZBlocker/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<Setting Name="SpotifyMute" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="UserEducated" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>

0 comments on commit e65a875

Please sign in to comment.