Build Your First AdBlocker - Chrome Extension

DevSage
8 Dec 201912:25

Summary

TLDRIn this tutorial, Deb Sage teaches viewers how to create a Google Chrome ad blocker extension. She explains how ad blockers work by blocking requests to advertisers, guides through setting up a manifest file, and demonstrates adding a background script to filter and block specific URLs. The video also covers creating icons and testing the extension in Chrome.

Takeaways

  • 😀 Deb Sage is teaching how to create a Google Chrome ad blocker extension.
  • 🔍 Ad blockers work by monitoring network traffic and blocking requests to advertisers.
  • 📄 A manifest file is necessary for all Chrome extensions and contains important information about the plugin.
  • 📚 The manifest file includes properties like name, version, description, permissions, background scripts, and icons.
  • 🏗️ Permissions are needed to access certain Chrome APIs, such as the web request and web request blocking APIs.
  • 🤖 The background script is used to add event listeners for Chrome events, specifically to intercept and potentially block requests.
  • 🛠️ The `chrome.webRequest.onBeforeRequest` event listener is used to check and block requests to potential advertisers.
  • 🌐 A filter object is used to specify which sites the callback function should apply to, allowing selective blocking of requests.
  • 🖼️ Icons for the extension are specified in the manifest file and should be created in different sizes (16x16, 48x48, 128x128).
  • 🚀 To test the extension, enable developer mode in Chrome, load the unpacked extension, and test its functionality by visiting an advertiser's website.

Q & A

  • What is the main purpose of the video?

    -The main purpose of the video is to teach viewers how to create a Google Chrome ad blocker extension.

  • Why does the video creator dislike ads?

    -The video creator dislikes ads because they find them annoying and prefer not to watch them.

  • How does an ad blocker work in the context of the video?

    -An ad blocker works by monitoring network traffic and blocking requests to potential advertisers, thus preventing ads from being displayed on web pages.

  • What is the first file that needs to be created for a Chrome extension?

    -The first file that needs to be created is the 'manifest.json', which contains important information about the extension.

  • What properties should be added to the 'manifest.json' file?

    -The properties to be added include 'name', 'version', 'description', 'permissions', 'background', 'icons', and 'manifest_version'.

  • Which APIs are mentioned as needing permissions in the video?

    -The APIs mentioned as needing permissions are the 'web request' API and the 'web request blocking' API.

  • What is the purpose of the 'background.js' file in the extension?

    -The 'background.js' file contains scripts that run in the background, mainly for adding event listeners, which are used to monitor and potentially block requests to advertisers.

  • How can you block requests to specific advertisers in the extension?

    -You can block requests by using a filter object in the 'onBeforeRequest' event listener, specifying URLs or regular expressions that match the advertiser's domain.

  • What is the special permission called 'all URLs' for?

    -The 'all URLs' permission is used to grant the extension access to all URLs, allowing it to block requests across all websites.

  • How can you test the functionality of your ad blocker extension?

    -You can test the extension by loading it in Chrome's 'chrome://extensions' page, enabling developer mode, and then loading the unpacked extension from the ad blocker folder.

  • What is the final step mentioned in the video for using the ad blocker extension?

    -The final step is to visit a website like 'zito.com' to see if the requests are being blocked, which would prevent ads from being displayed.

Outlines

00:00

🛠️ Building a Chrome Ad Blocker Extension

In this video, Deb Sage introduces viewers to the process of creating a Google Chrome ad blocker extension. Deb explains her motivation for wanting to block ads, which she dislikes, and proceeds to guide the audience through the development of their own extension. The explanation begins with a basic understanding of how ad blockers work by intercepting network traffic and preventing the browser from sending requests to advertisers. The video then dives into the technical aspect, starting with the creation of a 'manifest.json' file, which is essential for all Chrome extensions. This file contains important information about the plugin, such as its name, version, description, permissions, and background scripts. Deb also discusses the need for permissions to access certain Chrome APIs, such as the web request API and the web request blocking API, and the inclusion of a special permission for 'all URLs'. The tutorial continues with the setup of background scripts and the configuration of icons for the extension.

05:00

🖼️ Creating Icons for the Extension

Continuing from the previous segment, Deb Sage discusses the creation of icons for the Chrome extension. She mentions the importance of having icons in different sizes, specifically 16x16, 48x48, and 128x128 pixels, and provides the paths for these icons in the 'manifest.json' file. Deb then guides viewers on how to create a new folder called 'icons' and suggests creating these icons later, providing a link to some pre-made icons in the repository for those who may not wish to create their own. The focus here is on the visual aspect of the extension, ensuring that it has a recognizable and consistent appearance across different interfaces.

10:04

🚀 Running the Ad Blocker Extension

In the final segment of the video, Deb Sage demonstrates how to run the newly created ad blocker extension. She instructs viewers to enable developer mode in Chrome, navigate to 'chrome://extensions', and load the unpacked extension from the ad blocker folder. Deb explains how to toggle the extension on and off and tests its functionality by visiting a website known for displaying ads. The video shows that with the extension enabled, ads are successfully blocked, demonstrating the effectiveness of the ad blocker. Deb also encourages viewers to do their own research on which sites to block and provides a list of potential advertising sites that can be filtered. The tutorial concludes with a brief overview of how the extension works and an invitation for viewers to like the video and leave comments if they have questions.

Mindmap

Keywords

💡Ad Blocker

An ad blocker is a software tool designed to prevent ads from being displayed on web pages. In the video, the creator discusses building a custom ad blocker extension for Google Chrome. This tool is crucial for blocking requests to advertisers, enhancing user experience by removing annoying ads from web pages.

💡Google Chrome

Google Chrome is a popular web browser developed by Google. The video focuses on creating an ad blocker extension specifically for this browser. It is significant as the platform where the ad blocker will function, highlighting the browser's extensibility and support for custom extensions.

💡Manifest JSON

Manifest JSON is a configuration file required for all Chrome extensions. It contains metadata about the extension, such as its name, version, and permissions. In the script, the creator explains how to set up this file, which is essential for defining the extension's properties and capabilities.

💡Permissions

Permissions in the context of Chrome extensions refer to the access rights granted to the extension to interact with Chrome APIs and perform certain actions. The video mentions needing permissions for APIs like 'webRequest' and 'webRequestBlocking', which are crucial for monitoring and blocking network requests to advertisers.

💡Background Scripts

Background scripts are scripts that run in the background of a Chrome extension, primarily for handling events and performing tasks without user interaction. The video script describes adding event listeners in these scripts to intercept and potentially block network requests, which is central to the ad blocker's functionality.

💡Web Request API

The Web Request API is a part of the Chrome Extensions API that allows extensions to observe and analyze web requests in real-time. The video discusses using this API to monitor requests and decide whether to block them based on their destination, which is key to the ad blocker's operation.

💡Web Request Blocking API

This API is used in conjunction with the Web Request API to block web requests. The video script explains how to utilize this API to prevent the browser from sending requests to certain URLs, effectively blocking ads served by those URLs.

💡Icons

Icons are graphical representations used in the Chrome extension to provide visual cues to the user. The video mentions creating icons of various sizes (16x16, 48x48, 128x128) for the extension, which are displayed in the browser's extension management interface and help users identify the extension.

💡Event Listeners

Event listeners are functions that run in response to specific browser events. In the context of the video, the creator adds an event listener to the 'onBeforeRequest' event of the Web Request API. This listener is used to check the destination of a request and decide whether to block it, which is a core mechanism of the ad blocker.

💡Zito.com

Zito.com is mentioned in the video as an example of an advertising server. The script discusses blocking requests to this domain using a regular expression, which is a practical example of how the ad blocker can be configured to filter out specific ad sources.

Highlights

Introduction to creating a Google Chrome ad blocker extension.

Explanation of how ads are displayed on websites like YouTube through additional requests to advertisers.

Purpose of ad blockers to monitor network traffic and prevent requests to advertisers.

Creation of a 'manifest.json' file necessary for all Chrome extensions.

Description of the 'manifest.json' file properties including 'name', 'version', 'description', and 'permissions'.

Inclusion of the 'webRequest' and 'webRequestBlocking' APIs in the permissions array.

Explanation of the 'background' property and its role in running scripts with Chrome event listeners.

Creation of 'background.js' file for handling Chrome API calls and event listeners.

Use of 'chrome.webRequest.onBeforeRequest' to intercept and potentially block requests.

Implementation of a callback function to cancel requests to specific advertisers.

Introduction of a filter object to specify which sites to apply the callback function to.

Blocking requests to 'Zito.com' using a regular expression pattern in the filter object.

Instructions on how to load the extension in Chrome and enable developer mode.

Demonstration of the extension blocking ads on 'Zito.com' when enabled.

Discussion on researching and compiling a list of sites to block for effective ad blocking.

Encouragement to create a personal list of sites to block for custom ad blocking.

Conclusion and invitation for feedback on the tutorial.

Transcripts

play00:00

[Music]

play00:06

what's up people

play00:07

Deb sage here and in this video I'm

play00:10

gonna be teaching you how to create a

play00:12

Google Chrome ad blocker extension so I

play00:18

absolutely hate having to watch ads

play00:21

they're one of my least favorite things

play00:23

so that's why I have an ad blocker

play00:25

extension and I thought it'd be neat to

play00:27

teach you how to build your own

play00:29

extension so let's get started how does

play00:32

an ad blocker work so let's say I want

play00:35

to watch a YouTube video when I click on

play00:38

a video Google Chrome my browser sends a

play00:41

request to YouTube saying I want this

play00:44

video give it to me

play00:46

YouTube returns the video it sounds

play00:49

simple but my browser isn't only sending

play00:51

requests to YouTube YouTube is telling

play00:54

my browser hey I want you to send some

play00:56

more requests to our advertisers

play00:59

advertiser a advertiser be an advertiser

play01:02

see and they'll give you back some ads

play01:05

and will display those ads on the web

play01:08

page great now I have to deal with these

play01:11

annoying ads it would be nice if I can

play01:14

just send a request to YouTube so I can

play01:16

get my video but block any other

play01:19

requests to these different advertisers

play01:21

and that's where ad blockers come in ad

play01:24

blockers are basically scripts that run

play01:27

alongside your browser that can monitor

play01:31

your network traffic and make sure your

play01:33

browser doesn't send any requests to

play01:35

potential advertisers so let's jump into

play01:39

the code so in our ad blocker folder

play01:42

here we're going to create a file called

play01:44

manifest Jason which is a file that's

play01:48

necessary for all Chrome extensions and

play01:51

it's going to contain all of the

play01:53

important information about our plug-in

play01:56

so this is going to be a JSON object so

play01:58

we're gonna create an empty object and

play02:00

we're going to add some properties to

play02:02

this object one of the properties we're

play02:04

going to add is called name this is just

play02:07

the name of our plug-in which we can

play02:10

just say

play02:11

Deb sage AdBlocker another property we

play02:16

can add is conversion and this is just

play02:19

gonna be version 1.0 we can also add a

play02:24

description this is just going to be I

play02:29

don't know dev sage AdBlocker tutorial

play02:36

our fourth property here is going to be

play02:41

permissions and this is going to be an

play02:45

array and this is going to be a list of

play02:49

all of the chrome api's that we want our

play02:53

plug-in to have access to so what do I

play02:58

mean by that so let's open up Google

play03:01

Chrome

play03:02

let's open up developer tools and let's

play03:05

go to the console let's type it in

play03:08

Chrome so this is the chrome object

play03:12

inside of the chrome object here are a

play03:15

few of its api's that we can use to

play03:18

manipulate chrome and to build

play03:20

extensions with there are actually a lot

play03:23

more of these api's that we can't see

play03:26

here because we need certain permissions

play03:28

to access them and we can't access all

play03:30

of them directly from the browser and

play03:33

those api's are what we're going to be

play03:36

putting in our permissions array so a

play03:40

few of the api's that we're going to

play03:43

need permissions for are the web request

play03:46

API and the web request blocking API we

play03:53

also need one more permission this is a

play03:55

special permission called all URLs and

play03:58

this is just saying I want to grant

play04:02

permissions for these api's for all URLs

play04:07

okay so another property we can add is

play04:11

our background property which is going

play04:13

to be an object this object is going to

play04:16

have a property called scripts which is

play04:20

going to be an array

play04:22

and this is going to be an array of all

play04:24

of our background scripts which

play04:27

essentially are just scripts that run in

play04:29

the background that mainly contain

play04:32

chrome event listeners so the name of

play04:35

our background script is going to be

play04:37

background j/s alright and another

play04:44

property we can add is the icons

play04:46

property which is going to be an object

play04:48

and this object is going to contain

play04:51

information about the icons that our

play04:54

extension is going to have so there are

play04:58

three main sizes you have to consider

play05:00

there's the 16 by 16 size and for right

play05:03

now I'm just gonna put the path we're

play05:05

gonna create these icons later so we're

play05:08

gonna basically give a path to the icon

play05:11

so we're gonna say icons slash logo 16

play05:16

by 16 dot PNG and the next size we need

play05:22

to consider is 48 by 48 this is going to

play05:26

be an icon slash logo 48 by 48 and 128

play05:35

pixels by 128 this is going to be icons

play05:39

logo 1 to 8 by 1 to 8 all right so let's

play05:48

actually create those icons so let's

play05:52

create our folder

play05:53

I wouldn't create a new folder called

play05:55

icons and I actually have some icons

play05:58

already made and I'm gonna link these in

play06:02

the repository just in case you don't

play06:03

feel like creating your own so here the

play06:07

icon spent a copy call my ad blocker

play06:11

folder paste those okay now we have our

play06:16

icons for our extension and the last

play06:19

property we're going to add is the

play06:21

manifest version this will need to be to

play06:28

ok so we're done with our manifest file

play06:31

so now we can move on to our background

play06:34

dot JS file this is the only other file

play06:37

we're gonna need so let's create that

play06:40

file so new file background is and in

play06:46

here we're going to be calling our

play06:50

chrome api's so remember how I said

play06:54

background scripts are mainly just for

play06:57

adding event listeners so we're going to

play07:00

add an event listener to listen for

play07:02

right before we want to send a request

play07:05

and with this event listener we're going

play07:08

to check to see where this request is

play07:10

going and if it's going to a potential

play07:12

advertiser we want to block it so let's

play07:16

do that so let's say chrome dot web

play07:19

request dot on before request request

play07:26

should be capitalised dot ad listener

play07:30

not at event listener I know it's a

play07:33

little different with Chrome it's just

play07:34

ad listener and this is going to take in

play07:37

three arguments the first argument is

play07:40

going to be a callback function so

play07:43

function it's going to take in details

play07:46

which is going to contain details about

play07:49

the request that we are about to send

play07:52

we're not actually going to use it we're

play07:55

just going to return an object that says

play07:58

cancel true this essentially just blocks

play08:03

whatever request we are about to send so

play08:08

this is cool but right now this will be

play08:11

blocking all of our requests to

play08:13

everywhere so what we want to do is we

play08:15

want to add another argument here which

play08:18

is going to be an object that represents

play08:20

our filter so which sites do we want to

play08:25

apply this callback function to which

play08:28

sites do we want to actually block so

play08:31

inside of this object we can pass in a

play08:33

property URLs and it's going to be an

play08:36

array and here we can list all of the

play08:40

URLs

play08:41

that we want to block requests to so

play08:45

there's this one website called Zito

play08:49

calm and apparently Zito is a really big

play08:54

advertising server so it's possible that

play08:58

when you see an ad that it's from Zito

play09:01

so let's for example block any requests

play09:04

that are with that would be going to

play09:06

Zito so let's go back to our file and

play09:12

let's block request from Zito so we want

play09:15

to say star we can use regular

play09:18

expressions here so star colon slash

play09:21

slash star Z TOCOM slash star and this

play09:28

is going to block any requests from HTTP

play09:32

HTTP whatever this is basically a

play09:37

pattern that's going to block any

play09:40

requests coming from anything that looks

play09:43

like this alright and there's this one

play09:47

more thing we have to pass in after our

play09:50

filters object and it's going to be an

play09:52

array and we're just gonna pass in the

play09:57

blocking option okay and our plugin is

play10:04

functional we can actually run this so

play10:07

how do we do that so let's open Chrome

play10:10

and let's go to Chrome colon slash slash

play10:15

extensions and you're going to want to

play10:19

make sure developer mode is on so I have

play10:21

it on right now so make sure it's on you

play10:25

want to go to load unpacked and you want

play10:28

to go to your ad blocker folder whatever

play10:32

folder you had it in so I'm gonna go to

play10:37

ad blocker I'm gonna go to select folder

play10:39

and it should load up right here and

play10:42

this button is to toggle your extension

play10:45

on off and let's try to go to zero com

play10:49

see what it does look at that zero com

play10:55

blocked we were just on it not too long

play10:58

ago but now that we've blocked requests

play11:00

to Z TOCOM we can't actually get any

play11:04

information back such as an ad so if I

play11:09

were to go back and disable this refresh

play11:13

the page works just fine so I guess the

play11:20

question is how do you know which sites

play11:22

the block well that's where you're gonna

play11:25

have to do your own research I do have a

play11:27

list of sites you can filter from that

play11:31

I've found on the internet and so let me

play11:34

just copy and paste that in here and you

play11:38

could just compile your own list of

play11:40

sites that you want to block whether it

play11:43

be advertising sites or whatever and you

play11:46

can just pass this in as default filters

play11:52

and any requests that go to any of these

play11:56

sites are gonna be blocked and you won't

play11:59

get any information back from them and

play12:02

that is a little bit about how to build

play12:06

an ad blocker Chrome extension if you

play12:10

liked this video leave a like if you got

play12:12

any questions leave a comment and yeah

play12:15

peace

play12:19

[Music]

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Ad BlockerChrome ExtensionCoding TutorialWeb DevelopmentBrowser ScriptsNetwork TrafficUser ExperienceContent FilteringDeveloper ToolsOnline Ads
Benötigen Sie eine Zusammenfassung auf Englisch?