So I wanted to make a simple script that would replace images on a website (with kitten pics of course). Thing is, I want the script to keep replacing images as I scroll through a site.
E.g. on Google images it will only load kitten images for the immediately loaded pictures, not the other ones that load after.
How can I make it so that the images will replace the newly loaded images? I've thought about maybe detecting content loads but have no idea on how to approach the issue since I'm new to everything web.
Here's my content.js code:
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', replace);
} else {
replace();
}
function replace() {
let filenames = [
"0.jpg",
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"5.jpg",
"6.jpg",
"7.jpg",
"8.jpg",
"9.jpg",
"10.jpg"
];
let imgs = document.getElementsByTagName('img');
for (image of imgs) {
let ran = Math.floor(Math.random() * 10);
console.log(ran);
let file = 'cat/' + filenames[ran];
let url = chrome.extension.getURL(file);
image.src = url;
}
}