/** * Attempts to check if the current song is an ad **/ private bool IsAd(string artist) { try { int WebHelperResult = WebHelperHook.isAd(); Console.WriteLine("WebHelperResult " + WebHelperResult.ToString()); if (WebHelperResult > -1) { if (WebHelperResult == 0) { return(false); } else { return(true); } } else { return(isAdSpotify(artist) && IsAdiTunes(artist)); } } catch (Exception e) { Notify("Error occurred trying to connect to ad-detection servers."); try // Try again { return(isAdSpotify(artist) && IsAdiTunes(artist)); } catch { } return(false); } }
/** * Checks if the current installation is the latest version. Prompts user if not. **/ private void CheckUpdate() { try { int latest = Convert.ToInt32(WebHelperHook.GetPage("https://raw.githubusercontent.com/MatrixDJ96/EZBlocker/master/Version.txt")); int current = Convert.ToInt32(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace(".", "")); if (latest <= current) { return; } if (MessageBox.Show("There is a newer version of EZBlocker available. Would you like to download?", "EZBlocker", MessageBoxButtons.YesNo) == DialogResult.Yes) { Process.Start(update_website); Application.Exit(); } } catch {} }
public Main() { InitializeComponent(); socket.Open(); socket.On("pause", (data) => { WebHelperResult whr = WebHelperHook.GetStatus(); if (whr.isPlaying) { whr.isPlaying = false; BeginInvoke(new Action(this.Resume)); } }); socket.On("play", (data) => { WebHelperResult whr = WebHelperHook.GetStatus(); if (!whr.isPlaying) { whr.isPlaying = true; BeginInvoke(new Action(this.Resume)); } }); }
/** * Contains the logic for when to mute Spotify **/ private void MainTimer_Tick(object sender, EventArgs e) { try { if (Process.GetProcessesByName("spotify").Length < 1) { File.AppendAllText(logPath, "Spotify process not found\r\n"); Notify("Exiting EZBlocker."); Application.Exit(); } WebHelperResult whr = WebHelperHook.GetStatus(); if (whr.isAd) // Track is ad { if (whr.isPlaying) { Debug.WriteLine("Ad is playing"); if (lastArtistName != whr.artistName) { if (!muted) { Mute(1); } StatusLabel.Text = "Muting ad"; lastArtistName = whr.artistName; LogAction("/mute/" + whr.artistName); Debug.WriteLine("Blocked " + whr.artistName); } } else // Ad is paused { Debug.WriteLine("Ad is paused"); Resume(); } } else if (whr.isPrivateSession) { if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: *Private Session*"; lastArtistName = whr.artistName; MessageBox.Show("Please disable 'Private Session' on Spotify for EZBlocker to function properly.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); } } else if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; //Notify("Error connecting to Spotify. Retrying..."); File.AppendAllText(logPath, "Not running.\r\n"); MainTimer.Interval = 5000; /* * MainTimer.Enabled = false; * MessageBox.Show("Spotify is not running. Please restart EZBlocker after starting Spotify.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); * StatusLabel.Text = "Spotify is not running"; * Application.Exit(); */ } else if (!whr.isPlaying) { StatusLabel.Text = "Spotify is paused"; lastArtistName = ""; } else // Song is playing { if (muted) { Mute(0); } if (MainTimer.Interval > 1000) { MainTimer.Interval = 1000; } if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: " + ShortenName(whr.artistName); lastArtistName = whr.artistName; } } } catch (Exception ex) { Debug.WriteLine(ex); File.AppendAllText(logPath, ex.Message); } }
private void Main_Load(object sender, EventArgs e) { // Enable web helper if (File.Exists(spotifyPrefsPath)) { String[] lines = File.ReadAllLines(spotifyPrefsPath); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("webhelper.enabled") && lines[i].Contains("false")) { lines[i] = "webhelper.enabled=true"; File.WriteAllLines(spotifyPrefsPath, lines); break; } } } CheckUpdate(); // Start Spotify and give EZBlocker higher priority try { if (File.Exists(spotifyPath) && Process.GetProcessesByName("spotify").Length < 1) { Process.Start(spotifyPath); } Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Windows throttles down when minimized to task tray, so make sure EZBlocker runs smoothly // Check for open.spotify.com in hosts String hostsContent = File.ReadAllText(hostsPath); if (hostsContent.Contains("open.spotify.com")) { if (IsUserAnAdmin()) { File.WriteAllText(hostsPath, hostsContent.Replace("open.spotify.com", "localhost")); MessageBox.Show("An EZBlocker patch has been applied to your hosts file. If EZBlocker is stuck at 'Loading', please restart your computer.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("EZBlocker has detected an error in your hosts file.\r\n\r\nPlease re-run EZBlocker as Administrator or remove 'open.spotify.com' from your hosts file.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } } catch (Exception ex) { Debug.WriteLine(ex); } // Extract dependencies try { if (!File.Exists(nircmdPath)) { File.WriteAllBytes(nircmdPath, Properties.Resources.nircmd32); } if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } if (!File.Exists(coreaudioPath)) { File.WriteAllBytes(coreaudioPath, Properties.Resources.CoreAudio); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading EZBlocker dependencies. Please run EZBlocker as administrator or put EZBlocker in a user folder."); } // Set up UI SpotifyMuteCheckbox.Checked = Properties.Settings.Default.SpotifyMute; if (File.Exists(hostsPath)) { string hostsFile = File.ReadAllText(hostsPath); BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains("0.0.0.0 " + host)); } RegistryKey startupKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (startupKey.GetValue("EZBlocker") != null) { if (startupKey.GetValue("EZBlocker").ToString() == "\"" + Application.ExecutablePath + "\"") { StartupCheckbox.Checked = true; this.WindowState = FormWindowState.Minimized; } else // Reg value exists, but not in right path { startupKey.DeleteValue("EZBlocker"); } } // Google Analytics rnd = new Random(Environment.TickCount); starttime = DateTime.Now.Ticks; if (String.IsNullOrEmpty(Properties.Settings.Default.UID)) { Properties.Settings.Default.UID = rnd.Next(100000000, 999999999).ToString(); // Build unique visitorId; Properties.Settings.Default.Save(); } visitorId = Properties.Settings.Default.UID; File.AppendAllText(logPath, "-----------\r\n"); bool unsafeHeaders = WebHelperHook.SetAllowUnsafeHeaderParsing20(); Debug.WriteLine("Unsafe Headers: " + unsafeHeaders); if (!hasNet45()) { if (MessageBox.Show("You do not have .NET Framework 4.5. Download now?", "EZBlocker Error", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=30653"); } else { MessageBox.Show("EZBlocker may not function properly without .NET Framework 4.5 or above."); } } Mute(0); MainTimer.Enabled = true; LogAction("/launch"); }
private void Main_Load(object sender, EventArgs e) { // Enable web helper if (File.Exists(spotifyPrefsPath)) { String[] lines = File.ReadAllLines(spotifyPrefsPath); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("webhelper.enabled") && lines[i].Contains("false")) { lines[i] = "webhelper.enabled=true"; File.WriteAllLines(spotifyPrefsPath, lines); break; } } } CheckUpdate(); // Start Spotify and give EZBlocker higher priority try { if (File.Exists(spotifyPath) && GetHandle() == IntPtr.Zero) { Process.Start(spotifyPath); } Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Windows throttles down when minimized to task tray, so make sure EZBlocker runs smoothly } catch (Exception ex) { Debug.WriteLine(ex); } // Extract dependencies try { if (!File.Exists(nircmdPath)) { File.WriteAllBytes(nircmdPath, Properties.Resources.nircmd32); } if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } if (!File.Exists(coreaudioPath)) { File.WriteAllBytes(coreaudioPath, Properties.Resources.CoreAudio); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading EZBlocker dependencies. Please run EZBlocker as administrator or put EZBlocker in a user folder."); } // Set up UI SpotifyMuteCheckbox.Checked = Properties.Settings.Default.SpotifyMute; if (File.Exists(hostsPath)) { string hostsFile = File.ReadAllText(hostsPath); BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains("0.0.0.0 " + host)); } // Google Analytics rnd = new Random(Environment.TickCount); starttime = DateTime.Now.Ticks; if (String.IsNullOrEmpty(Properties.Settings.Default.UID)) { Properties.Settings.Default.UID = rnd.Next(100000000, 999999999).ToString(); // Build unique visitorId; Properties.Settings.Default.Save(); } visitorId = Properties.Settings.Default.UID; File.AppendAllText(logPath, "-----------\r\n"); bool unsafeHeaders = WebHelperHook.SetAllowUnsafeHeaderParsing20(); Debug.WriteLine("Unsafe Headers: " + unsafeHeaders); if (!hasNet45()) { if (MessageBox.Show("You do not have .NET Framework 4.5. Download now?", "EZBlocker Error", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=30653"); } else { MessageBox.Show("EZBlocker may not function properly without .NET Framework 4.5 or above."); } } Mute(0); MainTimer.Enabled = true; LogAction("/launch"); }
/** * Contains the logic for when to mute Spotify **/ private void TimerMain_Tick(object sender, EventArgs e) { try { if (Process.GetProcessesByName("spotify").Length < 1) { NotifyBalloon("Exiting EZBlocker...", 10000); noErrors = false; Application.Exit(); } WebHelperResult whr = WebHelperHook.GetStatus(); if (whr.isAd) { if (whr.isPlaying) { if (lastSongName != whr.songName) { if (!muted) { Mute(1); } lastSongName = ""; LabelMessage("Playing: Muted ad!", ""); } } else { lastSongName = ""; LabelMessage("Ad is paused!? Why?", "Resume it now please"); } } else if (whr.isPrivateSession) { if (lastSongName != whr.songName) { lastSongName = whr.songName; LabelMessage("Playing: *Private Session*", "Please disable 'Private Session' on Spotify for EZBlocker to function properly."); } } else if (!whr.isRunning) { lastSongName = ""; LabelMessage("Spotify is not playing", ""); } else if (!whr.isPlaying) { lastSongName = ""; LabelMessage("Spotify is paused", ""); } else // Song is playing { if (muted) { Mute(0); } if (lastSongName != whr.songName) { lastSongName = whr.songName; LabelMessage("Playing: " + ShortenName(whr.songName), "Artist: " + ShortenName(whr.artistName)); } } } catch { LabelMessage("Is Internet availble?", ""); } }
/** * Contains the logic for when to mute Spotify **/ private void MainTimer_Tick(object sender, EventArgs e) { try { WebHelperResult whr = WebHelperHook.GetStatus(); //Console.WriteLine(whr.isAd); if (whr.isAd) // Track is ad { if (whr.isPlaying) { if (lastArtistName != whr.artistName) { if (!muted) { Mute(1); } StatusLabel.Text = "Muting ad: " + ShortenName(whr.artistName); lastArtistName = whr.artistName; LogAction("/mute/" + whr.artistName); Console.WriteLine("Blocked " + whr.artistName); } } else // Ad is paused { Resume(); StatusLabel.Text = "Muting: " + ShortenName(whr.artistName); } } /*else if (whr.length - whr.position < 1.5) // Song is within last 1.5 seconds * { * if (skipAds) * NextTrack(); * }*/ else if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; lastArtistName = "N/A"; } else if (!whr.isPlaying) { StatusLabel.Text = "Spotify is paused"; lastArtistName = "N/A"; } else // Song is playing { if (muted) { Mute(0); } if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: " + ShortenName(whr.artistName); lastArtistName = whr.artistName; } } } catch (Exception except) { StatusLabel.Text = "Connection Error"; WebHelperHook.CheckWebHelper(); Console.WriteLine(except); File.WriteAllText(logPath, except.ToString()); } }
private void Main_Load(object sender, EventArgs e) { LogAction("/launch"); CheckUpdate(); // Enable web helper if (File.Exists(spotifyPrefsPath)) { String[] lines = File.ReadAllLines(spotifyPrefsPath); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("webhelper.enabled") && lines[i].Contains("false")) { lines[i] = "webhelper.enabled=true"; File.WriteAllLines(spotifyPrefsPath, lines); break; } } } // Start Spotify and give EZBlocker higher priority try { if (File.Exists(spotifyPath)) { Process.Start(spotifyPath); } Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Windows throttles down when minimized to task tray, so make sure EZBlocker runs smoothly } catch (Exception ex) { Debug.WriteLine(ex); } // Extract dependencies try { if (!File.Exists(nircmdPath)) { File.WriteAllBytes(nircmdPath, Properties.Resources.nircmd32); } if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } if (!File.Exists(coreaudioPath)) { File.WriteAllBytes(coreaudioPath, Properties.Resources.CoreAudio); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading EZBlocker dependencies. Please run EZBlocker as administrator or put EZBlocker in a user folder."); } // Set up UI SpotifyMuteCheckbox.Checked = Properties.Settings.Default.SpotifyMute; if (File.Exists(hostsPath)) { BlockBannersCheckbox.Checked = File.ReadAllText(hostsPath).Contains("doubleclick.net"); } // Google Analytics rnd = new Random(Environment.TickCount); starttime = DateTime.Now.Ticks; if (String.IsNullOrEmpty(Properties.Settings.Default.UID)) { Properties.Settings.Default.UID = rnd.Next(100000000, 999999999).ToString(); // Build unique visitorId; Properties.Settings.Default.Save(); } visitorId = Properties.Settings.Default.UID; File.AppendAllText(logPath, "-----------\r\n"); bool unsafeHeaders = WebHelperHook.SetAllowUnsafeHeaderParsing20(); Debug.WriteLine("Unsafe Headers: " + unsafeHeaders); Mute(0); MainTimer.Enabled = true; }