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

83 lines
2.5 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",
2019-07-10 09:59:04 +00:00
"other": "#newness-dot, .ytp-spinner"
2019-06-29 19:47:18 +00:00
};
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) {
2019-07-10 09:59:04 +00:00
console.warn("[YTAC] could not locate autoplay button");
2019-06-29 19:47:18 +00:00
} else if (autoplayButton.getAttribute("aria-pressed") === "true") {
autoplayButton.click();
}
// description expander
2019-07-10 09:59:04 +00:00
let showMoreButton = document.getElementsByClassName("more-button");
if (showMoreButton.length == 0) {
2019-06-29 19:47:18 +00:00
console.warn("[YTAC] could not locate description expander, trying again in 5s");
2019-07-10 09:59:04 +00:00
setTimeout(modifyThings, 5000);
2019-06-29 19:47:18 +00:00
} else {
2019-07-10 09:59:04 +00:00
showMoreButton[0].click();
2019-06-29 19:47:18 +00:00
}
}
modifyThings();