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

114 lines
3.3 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();
});
window.ytacCSS = {
2019-06-29 19:47:18 +00:00
"sidebar": "#related",
"comments": "#comments",
"endscreen": ".videowall-endscreen",
"invideo": ".ytp-ce-video",
"notification": "ytd-notification-topbar-button-renderer",
"other": "#newness-dot, .ytp-spinner, .ytp-next-button"
2019-06-29 19:47:18 +00:00
};
const INVISIBLE = " { display: none !important; }";
2019-06-29 19:47:18 +00:00
function addCSS(key) {
if (document.getElementById("ytac" + key) !== null) {
return;
}
let head = document.getElementsByTagName('head')[0];
2019-06-29 19:47:18 +00:00
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 = window.ytacCSS[key] + INVISIBLE;
2019-06-29 19:47:18 +00:00
} else {
e.styleSheet.cssText = window.ytacCSS[key] + INVISIBLE;
2019-06-29 19:47:18 +00:00
}
head.appendChild(e);
}
function addClickListeners(key) {
for (el of document.querySelectorAll(window.ytacCSS[key])) {
el.addEventListener("click", (e) => addCSS(key));
}
}
for (key of Object.keys(window.ytacCSS)) {
addCSS(key);
}
2019-06-29 19:47:18 +00:00
// 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");
2019-07-13 10:11:25 +00:00
document.getElementById("ytaccomments").remove();
2019-06-29 19:47:18 +00:00
} else if (message.command == "sidebar") {
console.log("[YTAC] showing video sidebar");
document.getElementById("ytacsidebar").remove();
addClickListeners("sidebar");
2019-06-29 19:47:18 +00:00
} else if (message.command == "invideo") {
console.log("[YTAC] showing in-video suggestions");
document.getElementById("ytacinvideo").remove();
2019-07-13 10:11:25 +00:00
addClickListeners("invideo");
2019-06-29 19:47:18 +00:00
} else if (message.command == "endscreen") {
console.log("[YTAC] showing endscreen");
document.getElementById("ytacendscreen").remove();
2019-07-13 10:11:25 +00:00
addClickListeners("endscreen");
2019-06-29 19:47:18 +00:00
} else if (message.command == "notification") {
console.log("[YTAC] showing notification bell");
document.getElementById("ytacnotification").remove();
2019-07-13 10:11:25 +00:00
addClickListeners("notification");
2019-06-29 19:47:18 +00:00
}
};
browser.runtime.onMessage.addListener(window.ytacListener);
console.log("[YTAC] added action listener");
function modifyThings() {
2019-07-23 15:19:22 +00:00
//let repeating = false;
2019-06-29 19:47:18 +00:00
// autoplay disabler
let autoplayButton = document.getElementById("toggle");
if (autoplayButton == null) {
console.warn("[YTAC] could not locate autoplay button, trying again in 10s ...");
2019-07-23 15:19:22 +00:00
setTimeout(modifyThings, 10000);
2019-06-29 19:47:18 +00:00
} else if (autoplayButton.getAttribute("aria-pressed") === "true") {
autoplayButton.click();
}
// description expander
2019-07-23 15:19:22 +00:00
/*
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
}
2019-07-23 15:19:22 +00:00
*/
2019-06-29 19:47:18 +00:00
}
2019-07-14 09:48:15 +00:00
modifyThings();
// attempt to hide the notification count in the title
function hideNotificationCount() {
let a = document.getElementsByTagName("title")[0];
if (a.innerText.match("^\\(\\d+\\).*")) {
a.innerText = a.innerText.substr(a.innerText.indexOf(")") + 2);
}
}
2019-07-23 15:19:22 +00:00
setInterval(hideNotificationCount, 1000);