Xeroday / Spotify-Ad-Blocker

EZBlocker - A Spotify Ad Blocker for Windows
https://www.ericzhang.me/projects/spotify-ad-blocker-ezblocker/
GNU General Public License v3.0
1.85k stars 193 forks source link

Include option to fully block ads from even playing #191

Closed ghost closed 5 years ago

ghost commented 5 years ago

This is the code i found when going to a reddit post on r/Piracy It is in batchfile format and requires saving the code in notepad as .cmd or .bat.

@echo off

:CheckAdminRights
set UAC="%temp%\GetAdmin.vbs"
fltmc >nul 2>&1 || (title Requesting Admin Privileges & (echo Set UAC=CreateObject^("Shell.Application"^):UAC.ShellExecute "%~f0","","","runas",1)>%UAC% & %UAC% & del /f /q %UAC% & exit)

:PreCheck
set "RegKey=Software\Microsoft\Windows Script Host\Settings"
for %%a in (HKLM,HKCU) do reg query "%%a\%RegKey%" /v "Enabled">nul 2>&1 && (reg delete "%%a\%RegKey%" /f /v "Enabled">nul 2>&1 & goto PreCheck)

:Payload
title Spotify AdBlock & color 2F
setlocal EnableDelayedExpansion

set "IP=127.0.0.1"
set "path=%windir%\system32\drivers\etc"

set "AdSite1=adclick.g.doublecklick.net,adeventtracker.spotify.com,ads-fa.spotify.com,analytics.spotify.com,audio2.spotify.com,b.scorecardresearch.com,bounceexchange.com"
set "AdSite2=bs.serving-sys.com,content.bitsontherun.com,core.insightexpressai.com,crashdump.spotify.com,d2gi7ultltnc2u.cloudfront.net,d3rt1990lpmkn.cloudfront.net,desktop.spotify.com"
set "AdSite3=doubleclick.net,ds.serving-sys.com,googleadservices.com,googleads.g.doubleclick.net,gtssl2-ocsp.geotrust.com,js.moatads.com,media-match.com,omaze.com,pagead46.l.doubleclick.net"
set "AdSite4=pagead2.googlesyndication.com,partner.googleadservices.com,pubads.g.doubleclick.net,redirector.gvt1.com,s0.2mdn.net,securepubads.g.doubleclick.net,spclient.wg.spotify.com,tpc.googlesyndication.com"
set "Adsite5=v.jwpcdn.com,video-ad-stats.googlesyndication.com,weblb-wg.gslb.spotify.com,www.googleadservices.com,www.googletagservices.com,www.omaze.com"

::find Spotify Adblock keyword "Present" in Hosts file, if found Spotify Adblock Implemented
echo Checking...
for %%a in ("Hosts files","hosts") do findstr /i "present2" "%windir%\system32\drivers\etc\%%~a">nul 2>&1 && (
    cd /d "%~dp0"
    echo Reblocking Spotify Adsites...
    call :Reblock "hosts file"
    call :Reblock "hosts"
    echo Spotify Ads has been reblocked
    goto end
)
for %%a in ("Hosts files","hosts") do findstr /i "present" "%windir%\system32\drivers\etc\%%~a">nul 2>&1 && (
    cd /d "%~dp0"
    echo Unblocking Spotify Adsites...
    call :Deblock "hosts file"
    call :Deblock "hosts"
    echo Spotify Ads has been unblocked
    goto end
)

cd /d "%~dp0"
echo Blocking Spotify Ads...
call :AdBlock "hosts file"
call :AdBlock "hosts"
echo Spotify Ads has been blocked
:end
cd /d "%~dp0"
echo.
echo Please restart your spotify if running
echo.
pause & exit

::Adds Network Rule to Redirect Known Spotify Ad Sites to Localhost IP
:AdBlock
echo. >> "%path%\%~1"
echo #Spotify Ad Sites, Present >> "%path%\%~1"
for %%a in (1,2,3,4,5) do (
    for %%b in (!AdSite%%a!) do echo %IP% %%b >> "%path%\%~1"
)
exit /b

::Search and deletes known network rule to reverse effect
:Deblock
set "tempdir=%path%\%~1.tmp"
set "filedir=%path%\%~1"
if exist "%tempdir%" (del /f /q "%tempdir%")
for /f "tokens=* delims=" %%a in (%filedir%) do (
    set "line=%%a"
    set "line=!line:#Spotify Ad Sites, Present=#Spotify Ad Sites, Present2!"
    for %%a in (1,2,3,4,5) do (
        for %%b in (!AdSite%%a!) do set "line=!line:%IP% %%b=#%IP% %%b!"
    )
    echo.!line!>> "%tempdir%"
)
del /f /q "%filedir%"
ren "%tempdir%" "%~1"
exit /b

::Similar to deblock but in reverse
:Reblock
set "tempdir=%path%\%~1.tmp"
set "filedir=%path%\%~1"
if exist "%tempdir%" (del /f /q "%tempdir%")
FireEmerald commented 5 years ago

@thepcwiz101 Take a look at https://help.github.com/articles/creating-and-highlighting-code-blocks/

ghost commented 5 years ago

@FireEmerald thanks

Xeroday commented 5 years ago

This no longer works. Spotify now delivers ads from the same servers as songs.

ghost commented 5 years ago

@Xeroday I still use it and i haven't got ads back and i have the latest spotify version installed. Also i found that when using a proxy server the ads return despite the hosts file blocking the addresses.

Xeroday commented 5 years ago

If that's the case, EZBlocker's "Block Banner Ads" blocks some of those hosts and also blocks ads. I don't want to risk adding more hosts b/c it tends to break random experiences on Spotify itself.

Xeroday commented 5 years ago

Also, some of those hosts are redundant. Eg omaze.com and www.omaze.com won't load if doubleclick is blocked, since doubleclick loads those other pages.

ghost commented 5 years ago

@Xeroday well it is known that this breaks certain features and i noticed that. It also breaks google chrome's spell checker updating when i use this hosts file.