fix YoutubeAdblock not working for some users

This commit is contained in:
Vendicated 2024-08-03 15:18:35 +02:00
parent 8274b67597
commit d1996034b7
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18

View file

@ -19,7 +19,6 @@
* along with AdGuard's Block YouTube Ads. If not, see <http://www.gnu.org/licenses/>.
*/
const LOGO_ID = "block-youtube-ads-logo";
const hiddenCSS = [
"#__ffYoutube1",
"#__ffYoutube2",
@ -98,7 +97,7 @@ const hideElements = () => {
}
const rule = selectors.join(", ") + " { display: none!important; }";
const style = document.createElement("style");
style.innerHTML = rule;
style.textContent = rule;
document.head.appendChild(style);
};
/**
@ -193,68 +192,25 @@ const jsonOverride = (propertyName, overrideValue) => {
return obj;
};
// Override Response.prototype.json
const nativeResponseJson = Response.prototype.json;
Response.prototype.json = new Proxy(nativeResponseJson, {
apply(...args) {
Response.prototype.json = new Proxy(Response.prototype.json, {
async apply(...args) {
// Call the target function, get the original Promise
const promise = Reflect.apply(...args);
const result = await Reflect.apply(...args);
// Create a new one and override the JSON inside
return new Promise((resolve, reject) => {
promise.then(data => {
overrideObject(data, propertyName, overrideValue);
resolve(data);
}).catch(error => reject(error));
});
overrideObject(result, propertyName, overrideValue);
return result;
},
});
};
const addAdGuardLogoStyle = () => { };
const addAdGuardLogo = () => {
if (document.getElementById(LOGO_ID)) {
return;
}
const logo = document.createElement("span");
logo.innerHTML = "__logo_text__";
logo.setAttribute("id", LOGO_ID);
if (window.location.hostname === "m.youtube.com") {
const btn = document.querySelector("header.mobile-topbar-header > button");
if (btn) {
btn.parentNode?.insertBefore(logo, btn.nextSibling);
addAdGuardLogoStyle();
}
} else if (window.location.hostname === "www.youtube.com") {
const code = document.getElementById("country-code");
if (code) {
code.innerHTML = "";
code.appendChild(logo);
addAdGuardLogoStyle();
}
} else if (window.location.hostname === "music.youtube.com") {
const el = document.querySelector(".ytmusic-nav-bar#left-content");
if (el) {
el.appendChild(logo);
addAdGuardLogoStyle();
}
} else if (window.location.hostname === "www.youtube-nocookie.com") {
const code = document.querySelector("#yt-masthead #logo-container .content-region");
if (code) {
code.innerHTML = "";
code.appendChild(logo);
addAdGuardLogoStyle();
}
}
};
// Removes ads metadata from YouTube XHR requests
jsonOverride("adPlacements", []);
jsonOverride("playerAds", []);
// Applies CSS that hides YouTube ad elements
hideElements();
// Some changes should be re-evaluated on every page change
addAdGuardLogo();
hideDynamicAds();
autoSkipAds();
observeDomChanges(() => {
addAdGuardLogo();
hideDynamicAds();
autoSkipAds();
});