yt-addiction-control/youtube-addiction-control-extension-firefox.js
2019-07-10 11:59:04 +02:00

83 lines
2.5 KiB
JavaScript

// inject CSS
const STYLE_CLASS = "youtube-ac-styling";
Array.from(document.getElementsByClassName(STYLE_CLASS)).forEach((e) => {
e.remove();
});
let ytacCSS = {
"sidebar": "#related",
"comments": "#comments",
"endscreen": ".videowall-endscreen",
"invideo": ".ytp-ce-video",
"notification": "ytd-notification-topbar-button-renderer",
"other": "#newness-dot, .ytp-spinner"
};
const INVISIBLE = " { display: none !important; }"
let head = document.getElementsByTagName('head')[0];
for (key of Object.keys(ytacCSS)) {
let e = document.createElement('style');
e.setAttribute('type', 'text/css');
e.setAttribute("class", STYLE_CLASS);
e.setAttribute("id", "ytac" + key);
if ('textContent' in e) {
e.textContent = ytacCSS[key] + INVISIBLE;
} else {
e.styleSheet.cssText = ytacCSS[key] + INVISIBLE;
}
head.appendChild(e);
}
// browser action listener
// Listen for messages from the background script.
if (window.ytacListener != null) {
browser.runtime.onMessage.removeListener(window.ytacListener);
}
window.ytacListener = (message) => {
console.log("[YTAC] received browser action " + message.command);
if (message.command == "comments") {
console.log("[YTAC] showing comments");
document.getElementById("ytaccomments").remove();
} else if (message.command == "sidebar") {
console.log("[YTAC] showing video sidebar");
document.getElementById("ytacsidebar").remove();
} else if (message.command == "invideo") {
console.log("[YTAC] showing in-video suggestions");
document.getElementById("ytacinvideo").remove();
} else if (message.command == "endscreen") {
console.log("[YTAC] showing endscreen");
document.getElementById("ytacendscreen").remove();
} else if (message.command == "notification") {
console.log("[YTAC] showing notification bell");
document.getElementById("ytacnotification").remove();
}
};
browser.runtime.onMessage.addListener(window.ytacListener);
console.log("[YTAC] added action listener");
function modifyThings() {
let repeating = false;
// autoplay disabler
let autoplayButton = document.getElementById("toggle");
if (autoplayButton == null) {
console.warn("[YTAC] could not locate autoplay button");
} else if (autoplayButton.getAttribute("aria-pressed") === "true") {
autoplayButton.click();
}
// description expander
let showMoreButton = document.getElementsByClassName("more-button");
if (showMoreButton.length == 0) {
console.warn("[YTAC] could not locate description expander, trying again in 5s");
setTimeout(modifyThings, 5000);
} else {
showMoreButton[0].click();
}
}
modifyThings();