Sunday, January 4, 2009

AdBlock for WebKit/Android




Since my port of AdBlockPlus to WebKit is applicable to far more than just the HTC vogue (or even just android), here is the code. Unfortunately, WebKit doesn't have content-policy plugins like FireFox, and since my goal was to just get this working in the quickest, dirtiest way possible, this is implemented as a very very sloppy hack to the WebCore code. Here is the patch for android. This only includes element-blocking, since that suffices to block everything on sites I visit on android, and my first shot at element hiding wasn't too efficient.

Here is the patch. To use it,
  1. Apply patch
  2. Place filter list (you can find a good one here) in /sdcard/block.txt
  3. Run browser app. In settings, "Load Images" is now "Block ads" (see comment in patch for reasoning behind this; here is a patch to change text)
If you have any suggestions for how to move this into a plugin, please let me know.

12 comments:

  1. Could you possibly post a compiled .apk of the browser with this? I'm running Android on my T-Mobile G1 and it'd be highly convenient or me and other users if we could have an easy-to-install method of doing this.

    Thanks!

    ~Unk

    ReplyDelete
  2. Agreed, please post a .apk app

    ReplyDelete
  3. Just to let you know, this patch no longer works properly on 1.5. It still applies cleanly, however the following errors occur during compile time:

    external/webkit/WebCore/loader/AdBlock.cpp: In constructor 'WebCore::Pattern::Pattern()':
    external/webkit/WebCore/loader/AdBlock.cpp:47: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression()'
    external/webkit/WebCore/platform/text/RegularExpression.h:38: note: candidates are: WebCore::RegularExpression::RegularExpression(const WebCore::RegularExpression&)
    external/webkit/WebCore/platform/text/RegularExpression.h:35: note: WebCore::RegularExpression::RegularExpression(const WebCore::String&, WebCore::TextCaseSensitivity)
    external/webkit/WebCore/loader/AdBlock.cpp: In member function 'bool WebCore::Pattern::matches(const WebCore::String&, int)':
    external/webkit/WebCore/loader/AdBlock.cpp:51: error: 'class WebCore::RegularExpression' has no member named 'search'
    external/webkit/WebCore/loader/AdBlock.cpp: In member function 'void WebCore::PatternMatcher::addPattern(const WebCore::String&)':
    external/webkit/WebCore/loader/AdBlock.cpp:127: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression(WebCore::String&)'
    external/webkit/WebCore/platform/text/RegularExpression.h:38: note: candidates are: WebCore::RegularExpression::RegularExpression(const WebCore::RegularExpression&)
    external/webkit/WebCore/platform/text/RegularExpression.h:35: note: WebCore::RegularExpression::RegularExpression(const WebCore::String&, WebCore::TextCaseSensitivity)
    external/webkit/WebCore/loader/AdBlock.cpp:131: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression(const char [3])'
    external/webkit/WebCore/platform/text/RegularExpression.h:38: note: candidates are: WebCore::RegularExpression::RegularExpression(const WebCore::RegularExpression&)
    external/webkit/WebCore/platform/text/RegularExpression.h:35: note: WebCore::RegularExpression::RegularExpression(const WebCore::String&, WebCore::TextCaseSensitivity)
    external/webkit/WebCore/loader/AdBlock.cpp:132: error: 'class WebCore::RegularExpression' has no member named 'search'
    external/webkit/WebCore/loader/AdBlock.cpp:136: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression(const char [5])'
    external/webkit/WebCore/platform/text/RegularExpression.h:38: note: candidates are: WebCore::RegularExpression::RegularExpression(const WebCore::RegularExpression&)
    external/webkit/WebCore/platform/text/RegularExpression.h:35: note: WebCore::RegularExpression::RegularExpression(const WebCore::String&, WebCore::TextCaseSensitivity)
    external/webkit/WebCore/loader/AdBlock.cpp:137: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression(const char [6])'
    external/webkit/WebCore/platform/text/RegularExpression.h:38: note: candidates are: WebCore::RegularExpression::RegularExpression(const WebCore::RegularExpression&)
    external/webkit/WebCore/platform/text/RegularExpression.h:35: note: WebCore::RegularExpression::RegularExpression(const WebCore::String&, WebCore::TextCaseSensitivity)
    external/webkit/WebCore/loader/AdBlock.cpp:138: error: no matching function for call to 'WebCore::RegularExpression::RegularExpression(const char [6])'

    ReplyDelete
  4. what version of webkit does this compile against?
    =r

    ReplyDelete
  5. just heard there are WebPolicyDelegates in webkit--wonder if that's enough.



    =r

    ReplyDelete
  6. Hi !
    I've updated the patch to 2.3 gingerbread, see
    http://jahrome.free.fr/index.php/block-ads-android-adblock-browser-webkit?lang=en

    ReplyDelete
  7. Searching for your slacks I want to refer you mensusa(dot)com the most popular in google in terms of suits.

    ReplyDelete
  8. I lately came beyond your web-site and accept been account affluence of posts of yours. I aloof anticipation I'd add a quick animadversion and let you apperceive that you've got a actively nice weblog. I'll watch out for updates from you!

    Android app developer

    ReplyDelete
  9. This is one of the wonderful and good post.I like your blog technique.Nice post.
    Android app developers

    ReplyDelete