0

I am trying to create a Chrome Extension that will replace all ads on a page with pictures of a dog just for fun. I know I could use adblocker. I have been using document.images document.getelementsbytagname and document.queryselectorall and these don't find me all of the img tags on the page for some reason. Even When I used document.queryselectorall("*") and then filter by tagname = "IMG" it still finds just the original tags from before but not the ads. On this webpage it only finds the img tags for the users pics but doesn't find the img tags for the ads. Why is this the case? Shouldn't querySelectorAll find all the elements on the page and then find all the tags with the IMG tagName property and put those into the list?

var all = document.querySelectorAll('*');
var complist = [];
for(i=0;i<all.length;i++){
if(all[i].tagName == "IMG"){
    complist.push(all[i]);
};
};
DataisKING
  • 11
  • 2
  • 4
    Sometimes ads are loaded in nested ` – Pointy Jun 13 '18 at 14:25
  • What Pointy said, but also, they may be loaded via XHR, which will not be seen by `document.querySelectorAll()`. You could, however, use AdBlock Plus's services and write a custom service to replace the ads with dogs. They take care of all the hard stuff (it's Open Source). You can find it here: https://github.com/adblockplus/ – Sam Jun 13 '18 at 14:26
  • If I just search for – DataisKING Jun 13 '18 at 14:29
  • @DataisKING depends on the site. If they rely heavily on iFrame you may not be able to see certain content. – Sam Jun 13 '18 at 14:31
  • https://stackoverflow.com/questions/26630519/queryselector-for-web-elements-inside-iframe – DataisKING Jun 13 '18 at 14:37

0 Answers0