Resolved : Adblock for electron app


#1

Hi,

My app is using webview to load lot of websites.
I want block ads/trackers from those website.
Since electron is not supporting chrome addons, Is there any way to block those ads and trackers?

Thanks
Jk


Is there any ad-block module for Electron?
#2

I think you’ll have to write your own adblocker, but Brave already did that, so you might be able to find out what they did by looking at the source code.


#3

Hi,

Thanks for your suggestion. I checked that but i guess it will take some time for me to understand what is exactly happening in brave, in the meantime i found these links
https://github.com/brave/ad-block.
https://github.com/minbrowser/min [This also uses custom adblock]
Currently I dont have any idea on these links, I will give a try and update this thread if that works for me

Thanks
Jk


#4

Well, that’s good. It looks like brave/ad-block is something you can just drop into a project once you figure out precisely how to interact with it.


#5

Hi,

I tried to used brave/ad-block unfortunately it dont have clear documentation.

URL I want to check if it is ad/tracker : https://www.google.ie/gen_204?atyp=i&ct=slh&cad=&ei=OdWtWLPnI4mmsgHd4ILgBQ&s=2&v=2&pv=0.6461194853086566&me=10:
domain I loaded/current domain in url bar : www.google.ie

I cant understand their sample can any one help me understand this

Which URL I have to give in client.parse in below sample
Why is slashdot.org given inclient.matches(‘http://www.brianbondy.com/public/ad/some-ad’, FilterOptions.script, ‘slashdot.org’)

Sample given by brave adblock

const {AdBlockClient, FilterOptions} = require(‘ad-block’)
const client = new AdBlockClient()

client.parse(’/public/ad/$domain=slashdot.org’)
client.parse(’/public/ad3/
$script’)

var b1 = client.matches(‘http://www.brianbondy.com/public/ad/some-ad’, FilterOptions.script, ‘slashdot.org’)
var b2 = client.matches(‘http://www.brianbondy.com/public/ad/some-ad’, FilterOptions.script, ‘digg.com’)

console.log('public/ad/* should match b1. Actual: ', b1)
console.log('public/ad/* should not match b2. Actual: ', b2)


#6

How about this:

https://www.npmjs.com/package/abp-filter-parser

It seems really easy to use. It’s a NodeJs module so just require it. All you do is give it an URL and it will return boolean to say whether you should block it or not.

It contains a copy of the AdBlock Plus block list, but according to the header that one is quite old, so go to the specified website to download a fresh copy (your code will ideally need to update the file automatically, perhaps once per week?).

[Adblock Plus 2.0]
! Checksum: 0rf0saEyajXNmZMk2fxPAQ
! Version: 201506241930
! Title: EasyList
! Last modified: 24 Jun 2015 19:30 UTC
! Expires: 4 days (update frequency)
! Homepage: https://easylist.adblockplus.org/
! Licence: https://easylist-downloads.adblockplus.org/COPYING
!
! Please report any unblocked adverts or problems
! in the forums (https://forums.lanik.us/)
! or via e-mail (easylist.subscription@gmail.com).

You can also define your own filters if you need to:

https://adblockplus.org/en/filters


#7

@erikwallace-lewdewe Thanks for you suggestion.
I tried that solution It is working but very slow, I am not sure why it is so slow in my system.
Finally I end up with my own version

  session.defaultSession.webRequest.onBeforeRequest(['*://*./*'], function(details, callback) {
        
        var test_url = details.url;
        var check_block_list =/\.(gr|hk||fm|eu|it|es|is|net|ke|me||tz|za|zm|uk|us|in|com|de|fr|zw|tv|sk|se|php|pk|pl)\/ads?[\-_./\?]|(stats?|rankings?|tracks?|trigg|webtrends?|webtrekk|statistiche|visibl|searchenginejournal|visit|webstat|survey|spring).*.(com|net|de|fr|co|it|se)|cloudflare|\/statistics\/|torrent|[\-_./]ga[\-_./]|[\-_./]counter[\-_./\?]|ad\.admitad\.|\/widgets?[\-_./]?ads?|\/videos?[\-_./]?ads?|\/valueclick|userad|track[\-_./]?ads?|\/top[\-_./]?ads?|\/sponsor[\-_./]?ads?|smartadserver|\/sidebar[\-_]?ads?|popunder|\/includes\/ads?|\/iframe[-_]?ads?|\/header[-_]?ads?|\/framead|\/get[-_]?ads?|\/files\/ad*|exoclick|displayad|\ajax\/ad|adzone|\/assets\/ad*|advertisement|\/adv\/*\.|ad-frame|\.com\/bads\/|follow-us|connect-|-social-|googleplus.|linkedin|footer-social.|social-media|gmail|commission|adserv\.|omniture|netflix|huffingtonpost|dlpageping|log204|geoip\.|baidu|reporting\.|paypal|maxmind|geo\.|api\.bit|hits|predict|cdn-cgi|record_|\.ve$|radar|\.pop|\.tinybar\.|\.ranking|.cash|\.banner\.|adzerk|gweb|alliance|adf\.ly|monitor|urchin_post|imrworldwide|gen204|twitter|naukri|hulu.com|baidu|seotools|roi-|revenue|tracking.js|\/tracking[\-_./]?|elitics|demandmedia|bizrate|click-|click\.|bidsystem|affiliates?\.|beacon|hit\.|googleadservices|metrix|googleanal|dailymotion|ga.js|survey|trekk|visit_|arcadebanners?|visitor\.|ielsen|cts\.|link_|ga-track|FacebookTracking|quantc|traffic|evenuescien|roitra|pixelt|pagetra|metrics|[-_/.]?stats?[.-_/]?|common_|accounts\.|contentad|iqadtile|boxad|audsci.js|ebtrekk|seotrack|clickalyzer|youtube|\/tracker\/|ekomi|clicky|[-_/.]?click?[.-_/]?|[-_/.]?tracking?[.-_/]?|[-_/.]?track?[.-_/]?|ghostery|hscrm|watchvideo|clicks4ads|mkt[0-9]|createsend|analytix|shoppingshadow|clicktracks|admeld|google-analytics|-analytic|googletagservices|googletagmanager|tracking\.|thirdparty|track\.|pflexads|smaato|medialytics|doubleclick|cloudfront|-static|-static-|static-|sponsored-banner|static_|_static_|_static|sponsored_link|sponsored_ad|googleadword|analytics\.|googletakes|adsbygoogle|analytics-|-analytic|analytic-|googlesyndication|google_adsense2|googleAdIndexTop|\/ads\/|google-ad-|google-ad?|google-adsense-|google-adsense.|google-adverts-|google-adwords|google-afc-|google-afc.|google\/ad\?|google\/adv\.|google160.|google728.|_adv|google_afc.|google_afc_|google_afs.|google_afs_widget|google_caf.js|google_lander2.js|google_radlinks_|googlead|googleafc.|googleafs.|googleafvadrenderer.|googlecontextualads.|googleheadad.|googleleader.|googleleads.|googlempu.|ads_|_ads_|_ads|easyads|easyads|easyadstrack|ebayads|[.\-_/\?](ads?|clicks?|tracks?|tracking|logs?)[.\-_/]?(banners?|mid|trends|pathmedia|tech|units?|vert*|fox|area|loc|nxs|format|call|script|final|systems?|show|tag\.?|collect*|slot|right|space|taily|vids?|supply|true|targeting|counts?|nectar|net|onion|parlor|2srv|searcher|fundi|nimation|context|stats?|vertising|class|infuse|includes?|spacers?|code|images?|vers|texts?|work*|tail|track|streams?|ability||world*|zone|position|vertisers?|servers?|view|partner|data)[.\-_/]?/gi
        var check_white_list =/status|premoa.*.jpg|rakuten|nitori-net|search\?tbs\=sbi\:|google.*\/search|ebay.*static.*g|\/shopping\/product|aclk?|translate.googleapis.com|encrypted-|product|www.googleadservices.com\/pagead\/aclk|target.com|.css/gi;
        var block_me = check_block_list.test(test_url);
        var release_me = check_white_list.test(test_url);

        if(release_me){
            callback({cancel: false})
        }else if(block_me){
            callback({cancel: true});

        }else{
            callback({cancel: false})
        }

});

I tried to create a generic filter to block everything.
I have very limited sites to work with so if any site is broken I can add them in whitelist.
Currently this is working fine, In future I will try to integrate with proper parser.

Thanks.


Electron Intercept HTTP request, response on BrowserWindow
#8

Hmm, the AdBlock rules file is about 1.5 to 1.8 MB so maybe that’s just too much to process to respond quickly.


#9

I also thinks the same due to huge filter size it is working slowly, but the solution I gave is working fine I have to optimize that


#10

Hi,

I implemented my own version of ad-block, If any one needs this you can use it and help me to improve this.

Main.js - If anything breaks add that pattern in whitelist

session.defaultSession.webRequest.onBeforeRequest(['*://*./*'], function(details, callback) {

    var test_url = details.url;
    var check_block_list =/mathtag.com|pulsemgr.com|advertising.com|bluecava.com|estara\.com|\/site_ads|\.omtrdc\.net|thebrighttag\.com|coremetrics\.com|\.(gr|hk|fm|eu|it|es|is|net|ke|me||tz|za|zm|uk|us|in|com|de|fr|zw|tv|sk|se|php|pk|pl)\/ads?[\-_./\?]|(stats?|rankings?|tracks?|trigg|webtrends?|webtrekk|statistiche|visibl|searchenginejournal|visit|webstat|survey|spring).*.(com|net|de|fr|co|it|se|jp|es)|cloudflare|\/statistics\/|torrent|[\-_./]ga[\-_./]|[\-_./]counter[\-_./\?]|ad\.admitad\.|\/widgets?[\-_./]?ads?|\/videos?[\-_./]?ads?|\/valueclick|userad|track[\-_./]?ads?|\/top[\-_./]?ads?|\/sponsor[\-_./]?ads?|smartadserver|\/sidebar[\-_]?ads?|popunder|\/includes\/ads?|\/iframe[-_]?ads?|\/header[-_]?ads?|\/framead|\/get[-_]?ads?|\/files\/ad*|exoclick|displayad|\ajax\/ad|adzone|\/assets\/ad*|advertisement|\/adv\/*\.|ad-frame|\.com\/bads\/|follow-us|connect-|-social-|googleplus.|linkedin|footer-social.|social-media|gmail|commission|adserv\.|omniture|netflix|huffingtonpost|dlpageping|log204|geoip\.|baidu|reporting\.|paypal|maxmind|geo\.|api\.bit|hits|predict|cdn-cgi|record_|\.ve$|radar|\.pop|\.tinybar\.|\.ranking|.cash|\.banner\.|adzerk|gweb|alliance|adf\.ly|monitor|urchin_post|imrworldwide|gen204|twitter|naukri|hulu.com|baidu|seotools|roi-|revenue|tracking.js|\/tracking[\-_./]?|elitics|demandmedia|bizrate|click-|bidsystem|affiliates?\.|beacon|hit\.|googleadservices|metrix|googleanal|abmr.net|dailymotion|ga.js|survey|trekk|tellapart.com|arcadebanners?|ielsen|cts\.|link_|ga-track|FacebookTracking|quantc|traffic|evenuescien|csi\.gstatic|gen_204|roitra|pixelt|pagetra|metrics|[-_/.]?stats?[.-_/]?|common_|accounts\.|contentad|iqadtile|boxad|audsci.js|ebtrekk|seotrack|clickalyzer|youtube|\/tracker\/|ekomi|clicky|[-_/.]?tracking?[.-_/]?|[-_/.]?track?[.-_/]?|ghostery|hscrm|watchvideo|clicks4ads|mkt[0-9]|createsend|analytix|shoppingshadow|clicktracks|admeld|google-analytics|-analytic|googletagservices|googletagmanager|tracking\.|thirdparty|track\.|pflexads|smaato|medialytics|doubleclick.net|doubleclick.com|\-static|\-static\-|static\-|sponsored\-banner|static\_|\_static\_|tagtray.com|tracking2.channeladvisor.com|js-agent.newrelic.com|agkn.com|twitter.com\/oct\.js|\_static|duckduckgo\.com\/t\/|sponsored_link|sponsored_ad|googleadword|analytics\.|googletakes|adsbygoogle|analytics\-|\-analytic|analytic\-|googlesyndication|\.net\/p\.gif\?|adsense|googleAdIndexTop|\/ads\/|google-ad-|google-ad?|google-adsense-|google-adsense.|google-adverts-|google-adwords|google-afc-|google-afc.|google\/ad\?|google\/adv\.|google160.|google728.|_adv|google_afc.|google_afc_|google_afs.|google_afs_widget|google_caf.js|google_lander2.js|google_radlinks_|googlead|googleafc.|googleafs.|googleafvadrenderer.|googlecontextualads.|googleheadad.|googleleader.|googleleads.|googlempu.|ads\_|\_ads\_|\_ads|dynaTraceMonitor|advertise.js|\_promo\_id\=|bazaarvoice.com|\/b\/ss\/\*\&aqe\=|analytics-static.ugc.bazaarvoice.com|iesnare.com|easyads|easyads|easyadstrack|ebayads|[.\-_/\?](ads?|clicks?|tracks?|tracking|logs?)[.\-_/]?(banners?|mid|trends|pathmedia|tech|units?|vert*|fox|area|loc|nxs|format|call|script|final|systems?|show|tag\.?|collect*|slot|right|space|taily|vids?|supply|true|targeting|counts?|nectar|net|onion|parlor|2srv|searcher|fundi|nimation|context|stats?|vertising|class|infuse|includes?|spacers?|code|images?|vers|texts?|work*|tail|track|streams?|ability||world*|zone|position|vertisers?|servers?|view|partner|data)[.\-_/]?/gi
    var check_white_list =/seller|min.js|logos?|jquery|catalog|status|premoa.*.jpg|rakuten|nitori-net|search\?tbs\=sbi\:|google.*\/search|\/shopping\/product|aclk?|translate.googleapis.com|encrypted-|product|www.googleadservices.com\/pagead\/aclk|statue|target.com|.css|lib.js|tealeaf.js/gi;
    var block_me = check_block_list.test(test_url);
    var release_me = check_white_list.test(test_url);

    if(release_me){
        callback({cancel: false})
    }else if(block_me){
        callback({cancel: true});

    }else{
        callback({cancel: false})
    }

});

preload.js

document.addEventListener("DOMNodeInserted", function (get_id_and_class) {
    var element_id = get_id_and_class.target.id;
    var element_class = get_id_and_class.target.className;

    if(element_class !='' && class_hashmap[element_class] !=undefined){
        hide_elements_class(element_class);
    }

    if(element_id !='' && id_hashmap[element_id] !=undefined){
        hide_elements_id(element_id);
    }
});


document.addEventListener("DOMSubtreeModified", function (get_id_and_class) {
    var element_id = get_id_and_class.target.id;
    var element_class = get_id_and_class.target.className;

    if(element_class !='' && class_hashmap[element_class] !=undefined){
        hide_elements_class(element_class);
    }

    if(element_id !='' && id_hashmap[element_id] !=undefined){
        hide_elements_id(element_id);
    }

});


function hide_elements_class(element_class) {
    if (element_class) {
        var appBanners = document.getElementsByClassName(element_class);
        [].forEach.call(appBanners, function (appBanner) {
            appBanner.style.display = 'none';
            console.log('hided the class' +element_class )
        });
    }
}



function hide_elements_id(element_id){
    document.getElementById(element_id).style.display = 'none';
    console.log('hided the id'  +element_id)
}

document.addEventListener("DOMContentLoaded", function () {
    var allElements = document.getElementsByTagName('*');
    for(var i = 0; i < allElements.length; i++) {

        if (allElements[i].id !='' && id_hashmap[allElements[i].id] != undefined) {
            hide_elements_id(allElements[i].id);
        }

        if (allElements[i].className !='' && class_hashmap[allElements[i].className] != undefined) {
            hide_elements_class(allElements[i].className);

        }
    }
});

var class_hashmap={'ad1':'ad1','ad2':'ad2'}
var id_hashmap ={'adid1':'adid1','adid2':adid2}

get id and class of ads from https://easylist-downloads.adblockplus.org/easylist.txt


#11

The issue with above method is few class and id are not coming in both
DOMcontentloaded and DomNodeinserted so i cant block those classes/ids.

Example:
google.com display ads in ‘ads-ad class’ but I wont get that class in both DOMcontentloaded and DomNodeinserted.

Is there any way to get all class and id names while loading or after loading

Thanks
Jk


#12

I had the same issue, so I used the brave-ad-block for a simple ad-blocker for electron.
This might be a solution for you problem? It caches the ad-block-clients, so it’s fast. :upside_down_face:


#13

Hi,
@Jense5
Thanks for the update.
I am facing issues in making it work

I did these

$ npm install --save electron-rebuild
$ ./node_modules/.bin/electron-rebuild -f -w ad-block

Rebuild is successful, if i try to npm start

import blockWindowAds from 'electron-ad-blocker';
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at loadApplicationPackage (/Users/jeyakuma/Desktop/backup/currentproj/UmtElectronClient/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:283:12)
    at Object.<anonymous> (/Users/jeyakuma/Desktop/backup/currentproj/UmtElectronClient/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:325:5)
    at Object.<anonymous> (/Users/jeyakuma/Desktop/backup/currentproj/UmtElectronClient/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:361:3)
    at Module._compile (module.js:571:32)

Did you also faced that issue or only me


#14

I suppose you are not using babel? Try replacing the import line with the code below. This should do the trick. :slightly_smiling_face:

const blockWindowAds = require('electron-ad-blocker').default;

#15

my bad :frowning:
I didn’t read that commented code .

Where have you been these many days :slight_smile:
Awesome work.It works fine :slight_smile:

npm install --save electron-rebuild ./node_modules/.bin/electron-rebuild -f -w ad-block - I removed that (.), because for me it didn’t worked with that .

I tried following commands when I get error while installing this.May help some one

npm uninstall
npm install
export npm_config_abi=50
npm install node-gyp 
npm install rebuild
npm install --save electron-rebuild
./node_modules/.bin/electron-rebuild -f -w ad-block

Thanks
Jk


#16

Hi, why I can’t install your modules. I have put the log here.
Thanks