Click here to Skip to main content
14,695,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a chrome extension. Here is the code:
	"name": "My name!",
	"version": "2.0",
	"description": "Blocks ads",
	"permissions": ["webRequest", "webRequestBlocking", "<all_urls>"],
	"browser_action": {
		"default_icon": "shield.ico",
		"default_popup": "popup.html",
		"default_title": "Ad be gone"
	"background": {
		"scripts": ["background.js"]
	"manifest_version": 2

  function(details) { return {cancel: true}; },
  { urls: ["*://**", "*://**",]  },

How would I make a button in popup.html that turns on and off the background.js and alerts the user that it is doing so? I basically want to make a button that turns on and off the ad blocker.

What I have tried:

Help would be really appreciated.
Updated 16-Aug-20 21:12pm

1 solution

Effective background scripts stay dormant until an event they are listening for fires, react with specified instructions, then unload.

Reference: Manage Events with Background Scripts - Google Chrome[^]

You need to make use of: chrome.browserAction.onClicked.addListener(function callback)
More details here: chrome.browserAction - Google Chrome[^]
function IconClicked(tab)
	let msg = {
		txt : "Icon Clicked!"
	chrome.tabs.sendMessage(, msg);
landocss 17-Aug-20 15:26pm
Can I have the exact code please?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900