0

I'm trying to create a Chrome extension that hides divs of a certain class, but unfortunately I can't get it to work:

My manifest.json:

{
  "manifest_version": 2,

  "name": "DivFilter",
  "description": "This extension allows the user to filter StackOverflow questions.",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab"
  ]
}

my popup.html:

<html>
  <head>
    <title>SO Filter</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <h1>SO Filter</h1>
    <button>Filter!</button>
  </body>
</html>

and my popup.js:

function filter() {
  var x = document.getElementsByClassName("question-summary narrow");
  console.log("Starting for-loop:");
  console.log(x);
  for (var i = 0; i < x.length; i++) {
    console.log(x[i]);
    var style = window.getComputedStyle(x[i]);

    if (style.display === "none") {
      x[i].style.display = "block";
    } else {
      x[i].style.display = "none";
    }
  }
}

document.addEventListener('DOMContentLoaded', function() {
  document.querySelector('button').addEventListener('click', filter);
});

when clicking the button on https://stackoverflow.com/ it logs the following:

enter image description here

I don't understand why length is 0, as when inspecting the page it clearly shows divs with the class I'm searching for:

enter image description here

I've read many other posts about this on SO, but so far I haven't been able to solve my problem...

vwos
  • 687
  • 9
  • 26
  • The problem is that I am not getting any matches at all, and I do not know why. Every time I log it to the console (using both functions) it says the list of elements has a length of 0. – vwos Jan 09 '18 at 10:11
  • 1
    This is a duplicate of [*How to access the webpage DOM rather than the extension page DOM?*](https://stackoverflow.com/questions/4532236/how-to-access-the-webpage-dom-rather-than-the-extension-page-dom). You're accessing your extension's DOM, not the page's. – T.J. Crowder Jan 09 '18 at 10:25

0 Answers0