Trying to code an adblocker for a specific website.
I already have the code which deletes the ads on the homepage, but it still works on, for example: Movie pages. And that causes info section, with poster and everything, to be deleted. So, I only want this specific piece of code to run on Homepage. which can be https://net.adjara.com/ or https://net.adjara.com/Home. This website is an AJAX one, it's loaded once and that's it.
This is the code which deletes the ads on the homepage :
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.message === 'Homepage') {
var timer = setInterval(deletor, 1);
function deletor() {
timer;
var slider = document.querySelector("#slider-con");
var bannerTop = document.querySelector("#MainContent > div:nth-child(2)")
var bannerMiddle = document.querySelector("#MainContent > iframe");
var bannerRandom = document.querySelector("#MainContent > div:nth-child(3)");
if (slider) {
slider.parentNode.removeChild(slider);
}
if (bannerTop) {
bannerTop.parentNode.removeChild(bannerTop);
}
if (bannerMiddle) {
bannerMiddle.parentNode.removeChild(bannerMiddle);
}
if (bannerRandom) {
bannerRandom.parentNode.removeChild(bannerRandom);
}
};
} else {
return false;
}
})
This is the bg script which sends the message:
chrome.tabs.onUpdated.addListener(function
(tabId, changeInfo, tab) {
// read changeInfo data and do something with it (like read the url)
if (changeInfo.url == "net.adjara.com" || "net.adjara.com/Home" || "net.adjara.com/Home/") {
chrome.tabs.sendMessage( tabId, {
message: "Homepage",
url: changeInfo.url
})
console.log("successful!");
} else if (changeInfo.url == "net.adjara.com/Movie/main*" || "net.adjara.come/Movie/*"){
console.log("Movie page");
} else {
return false;
}
}
);
I added a console.log and it seems like it works on every page load, but it also seems like the code is still running on other sub-domains.
I want this code to run only on homepage. For others I will write another custom script, which will apply only to movie sub-domains.
I think I'm missing something important.