yt-addiction-control/youtube-addiction-control-extension-firefox.js

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-06-29 19:47:18 +00:00
// 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, trying again in 5s");
setTimeout(modifyThings, 5000);
repeating = true;
} else if (autoplayButton.getAttribute("aria-pressed") === "true") {
autoplayButton.click();
}
// description expander
let showMoreButton = document.getElementById("more");
if (showMoreButton == null) {
console.warn("[YTAC] could not locate description expander, trying again in 5s");
if (!repeating) {
setTimeout(modifyThings, 5000);
}
} else {
showMoreButton.click();
}
}
modifyThings();