Looking to create an adblocker specifically for a certain website.
I have successfully implemented code that removes ads on the homepage, but it is still affecting other pages such as movie pages. This results in the deletion of the information section, including posters and other content. I want this piece of code to only target the Homepage at or . The website uses AJAX and loads only once.
Here is the code responsible for removing 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 background script sends the message:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
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;
}
});
After adding a console.log statement, it seems like the code runs on every page load and even on other sub-domains. I intend for this code to operate solely on the homepage, while creating a separate custom script for movie sub-domains.
It feels like I might be overlooking something crucial.