Skip to content

Commit

Permalink
More elements, bug fixes, and console logs
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzpw authored Nov 6, 2023
1 parent f1c24ef commit 87e88f7
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 69 deletions.
20 changes: 18 additions & 2 deletions youtube-ad-skipper-chrome/background.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
try{
importScripts("/remove-ads.js")
} catch (_ex) { console.log(_ex) }
chrome.contextMenus.create({
id: "remove-ads",
title: "Remove Remaining Ads",
});

chrome.contextMenus.onClicked.addListener((info, tab)=>{
if (info.menuItemId === "remove-ads"){
chrome.tabs.executeScript({
file: "remove-ads.js",
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["remove-ads.js"],
});
}
});

chrome.tabs.onUpdated.addListener((id, change, tab) => {
if (change.status === "complete" && tab.url.includes("https://www.youtube.com")){
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["remove-ads.js"],
})
}
// chrome.scripting.executeScript({
// target: { tabId: tab.id },
// files: ["remove-ads.js"],
// });
});
101 changes: 74 additions & 27 deletions youtube-ad-skipper-chrome/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ adElements = [".ytd-companion-slot-renderer",
".ytd-action-companion-ad-renderer",
".ytd-watch-next-secondary-results-renderer.sparkles-light-cta",
".ytd-unlimited-offer-module-renderer",
".ytp-ad-overlay-image", ".ytp-ad-text-overlay",
".ytp-ad-overlay-image",
".ytp-ad-text-overlay",
"div#root.style-scope.ytd-display-ad-renderer.yt-simple-endpoint",
"div#sparkles-container.style-scope.ytd-promoted-sparkles-web-renderer",
".ytd-display-ad-renderer",
Expand All @@ -13,50 +14,96 @@ adElements = [".ytd-companion-slot-renderer",
".ytd-banner-promo-renderer",
".ytd-video-masthead-ad-v3-renderer",
".ytd-primetime-promo-renderer",
".ytd-in-feed-ad-layout-renderer",
".ytd-ad-slot-renderer"
".ytd-ad-slot-renderer",
];

unwantedElements = [
".ytd-promoted-video-renderer",
".ytd-brand-video-singleton-renderer",
".ytd-statement-banner-renderer",
".ytd-popup-container",
".ytd-rich-section-renderer",
".ytd-promoted-sparkles-web-renderer",
];

function removeAdElements(){
adElements.forEach((ad)=>{
document.querySelector(ad) ? document.querySelector(ad).hidden = true : null;
let n = 0;
adElements.forEach((ad) => {
let currentAd = document.querySelector(ad);
if (document.querySelectorAll(ad).length > 1) {
try{
document.querySelectorAll(ad).forEach((adSlot) => {
if (!adSlot.hidden){
adSlot.hidden = true;
++n;
}
});
} catch (_ex) {
console.log(_ex, _ex.lineNumber);
}
} else if (currentAd) {
if (!currentAd.hidden){
currentAd.hidden = true;
++n;
}
}
});
unwantedElements.forEach((ad) => {
let currentAd = document.querySelector(ad);
if (document.querySelectorAll(ad).length > 1){
document.querySelectorAll(ad).forEach((adSlot) => {
try{
document.querySelectorAll(ad).forEach((adSlot) => {
if (!adSlot.hidden){
adSlot.hidden = true;
++n;
}
})
} catch (_ex) {
console.log(_ex, _ex.lineNumber);
}
})
} else if (currentAd){
if (!currentAd.hidden){
currentAd.hidden = true;
++n
}
}
})
let logMessage = `Startup: %cRemoved ${n} ads`;
n >= 1 ? console.log(logMessage, "color: #28a745") : console.log(logMessage, "color: #ffc107");
}

// wait for 2 seconds before attempting to remove ads
if (document.readyState === "complete"){
setTimeout(()=>{
removeAdElements();
}, 2000);
} else {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(()=>{
removeAdElements();
}, 2000);
});
}
removeAdElements();

function main(){
let adInterval = setInterval(()=>{
let videoStream = document.getElementsByClassName("video-stream");
let moviePlayer = document.getElementById("movie_player");
let isAd = moviePlayer.classList?.contains("ad-showing") || moviePlayer.classList?.contains("ad-interrupting");
let isAd = moviePlayer?.classList?.contains("ad-showing") || moviePlayer?.classList?.contains("ad-interrupting");
if (isAd){
videoStream[0].currentTime = videoStream[0].duration - 0.1;
if (videoStream[0].currentTime <= videoStream[0].duration - 0.1){
videoStream[0].currentTime = videoStream[0].duration - 0.1;
}
if (videoStream[0].currentTime >= videoStream[0].duration - 0.1 && videoStream[0].paused){
videoStream[0].play();
}
try {
let skipButton = document.getElementsByClassName("ytp-ad-skip-button")[0];
skipButton.click();
console.log("Clicked skip")
} catch {
try {
let skipButtonModern = document.getElementsByClassName("ytp-ad-skip-button-modern")[0];
if (skipButton){
skipButton.click();
console.log("Clicked skip");
}
let skipButtonModern = document.getElementsByClassName("ytp-ad-skip-button-modern")[0];
if (skipButtonModern){
skipButtonModern.click();
console.log("Clicked skip")
} catch {
console.log("No skip button");
console.log("Clicked skip");
}
} catch {
console.log("No skip button");
}
}
}, 150);
}

main();
8 changes: 5 additions & 3 deletions youtube-ad-skipper-chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"manifest_version": 2,
"manifest_version": 3,
"version": "1.0",
"author": "xyz",
"name": "Youtube Ad Skipper",
"icons": {
"16": "/img/icon.png",
"48": "/img/icon.png",
"128": "/img/icon.png"
},
"permissions": ["activeTab", "contextMenus"],
"permissions": ["activeTab", "contextMenus", "scripting"],
"host_permissions": ["https://www.youtube.com/*"],
"background": {
"scripts": ["background.js", "remove-ads.js"]
"service_worker": "background.js"
},
"content_scripts": [
{
Expand Down
59 changes: 55 additions & 4 deletions youtube-ad-skipper-chrome/remove-ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,63 @@ adElements = [".ytd-companion-slot-renderer",
".ytd-video-masthead-ad-v3-renderer",
".ytd-primetime-promo-renderer",
".ytd-in-feed-ad-layout-renderer",
".ytd-ad-slot-renderer"
".ytd-ad-slot-renderer",
];

unwantedElements = [
".ytd-promoted-video-renderer",
".ytd-brand-video-singleton-renderer",
".ytd-brand-video-shelf-renderer",
".ytd-popup-container",
".ytd-rich-section-renderer",
".ytd-promoted-sparkles-web-renderer",
]

function removeRemainingAds(){
adElements.forEach((ad)=>{
document.querySelector(ad) ? document.querySelector(ad).hidden = true : null;
let n = 0;
adElements.forEach((ad) => {
let currentAd = document.querySelector(ad);
if (document.querySelectorAll(ad).length > 1) {
try{
document.querySelectorAll(ad).forEach((adSlot) => {
if (!adSlot.hidden){
adSlot.hidden = true;
++n;
}
});
} catch (_ex){
console.log(_ex, _ex.lineNumber);
}
} else if (currentAd) {
if (!currentAd.hidden){
currentAd.hidden = true;
++n;
}
}
});
console.log("Removed all ads in element list");
unwantedElements.forEach((ad) => {
let currentAd = document.querySelector(ad);
if (document.querySelectorAll(ad).length > 1){
try{
document.querySelectorAll(ad).forEach((adSlot) => {
if (!adSlot.hidden){
adSlot.hidden = true;
++n;
}
});
} catch (_ex){
console.log(_ex, _ex.lineNumber);
}
}
if (currentAd){
if (!currentAd.hidden){
currentAd.hidden = true;
++n;
}
}
})
let logMessage = `%cRemoved ${n} ads`;
n >= 1 ? console.log(logMessage, "color: #28a745") : console.log(logMessage, "color: #ffc107");
}

removeRemainingAds();
10 changes: 10 additions & 0 deletions youtube-ad-skipper-firefox/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ browser.contextMenus.onClicked.addListener((info, tab)=>{
}
});

browser.tabs.onUpdated.addListener((id, change, tab) => {
if (change.status === "complete" && tab.url.includes("https://www.youtube.com")){
browser.tabs.executeScript({
file: "remove-ads.js",
});
}
// browser.tabs.executeScript({
// file: "remove-ads.js",
// });
})
Loading

0 comments on commit 87e88f7

Please sign in to comment.