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:
Manifest.json:
{
	"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
}

background.js:
  chrome.webRequest.onBeforeRequest.addListener(
  function(details) { return {cancel: true}; },
  { urls: ["*://*.doubleclick.net/*", "*://*.ads.google.com/*",]  },
  ["blocking"]
);

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.
Posted
Updated 16-Aug-20 21:12pm

1 solution

Quote:
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[^]
Example:
chrome.browserAction.onClicked.addListener(IconClicked);
function IconClicked(tab)
{
	let msg = {
		txt : "Icon Clicked!"
	}
	chrome.tabs.sendMessage(tab.id, msg);
}
   
Comments
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