Vagif
Vagif

Reputation: 244

Chrome extension does not work

please do not mark this as duplicate because my question is different. I am trying to make a chrome extension that blocks ads. But it doesn't seem to do anything. Here is my code:

manifest.json

{
"manifest_version": 2,

 "name": "Ad Killer",
 "description": "A Basic ad remover",
 "version": "1.0",

 "browser_action": {
 "default_icon": "ad128.png",
 "default_popup": "popup.html"
 },
 "permissions": [
   "activeTab",
   "https://ajax.googleapis.com/"
 ]
}

Here is my popup.html:

<html>
<body>

  <script type="text/javascript">
    var elems = document.getElementsByTagName("iframe"); 
    for(var i = 0, max = elems.length; i < max; i++) {
                 elems[i].hidden=true;
    } 
  </script>
  <h1> Ads Killed </h1>

</body>
</html>

The problem is that it shows the 'ads killed' message but does not do anything. My question is that is there any way to make it work? I have very little experience in javascript and any help will be very appreciated.

Upvotes: 1

Views: 61

Answers (1)

Soviut
Soviut

Reputation: 91714

Your script is only running inside popup.html which is what shows in the popup when you click the extension icon. It has no access to the pages in your browser tabs.

If you want this script to run on the pages in your browser tabs, you need to create a content script which gets injected into every tab.

If you want to communicate between your popup and the content scripts in your tabs, you need to use message passing to send messages between all the different pages. Each page would then listen for those messages and react accordingly using a syntax similar to event handlers.

Assigning content scripts is done from the manifest.json files. In order to limit which sites get your content script applied to them you must provide an array of URL matching patterns. You can then supply which javascript or css files get applied to that page.

For example, if you wanted to apply some javascript to all public web pages you might do something like:

"content_scripts": [
  {
    "matches": ["http://*", "https://*"],
    "js": ["myscript.js"]
  }
],

Upvotes: 2

Related Questions