-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.user.js
executable file
·120 lines (104 loc) · 5.72 KB
/
main.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// ==UserScript==
// @name Change notification wording
// @namespace https://github.com/tomasz13nocon
// @version 1.2.1
// @description Replace sentence-like wording from yt notifications with predictable, readable-at-a-glance text.
// @author Tomasz Nocoń
// @match https://www.youtube.com/*
// @grant none
// @homepageURL https://github.com/tomasz13nocon/youtube-change-notification-wording
// @license MIT
// ==/UserScript==
(function() {
'use strict';
const langs = {
// The span stuff in the regexes prevents a bug where the notification text would get replaced recursively
// The key must correspond to the lang attribute on the html tag
"pl-PL": {
notifTitleText: "Powiadomienia",
video: /(?:.*<\/span>)?Na kanał (?<channel>.*?) został przesłany film (?<title>.*)/,
live: /(?:.*<\/span>)?(?<channel>.*?) nadaje: (?<title>.*)/,
premiere: /(?:.*<\/span>)?Na kanale (?<channel>.*?) trwa premiera filmu: (?<title>.*)/,
commentReply: /(?:.*<\/span>)?(?<user>.*?) odpowiedział\(a\): (?<comment>.*)/,
commentLike: /(?:.*<\/span>)?👍 Ktoś polubił Twój komentarz: (?<comment>.*)/,
},
"en-GB": {
notifTitleText: "Notifications",
video: /(?:.*<\/span>)?(?<channel>.*?) uploaded: (?<title>.*)/,
live: /(?:.*<\/span>)?(?<channel>.*?) is live: (?<title>.*)/,
premiere: /(?:.*<\/span>)?(?<channel>.*?) premiering now: (?<title>.*)/,
commentReply: /(?:.*<\/span>)?(?<user>.*?) replied: (?<comment>.*)/,
commentLike: /(?:.*<\/span>)?👍 Someone liked your comment: (?<comment>.*)/,
},
};
let siteLang = document.getElementsByTagName("html")[0].getAttribute("lang");
if (!(siteLang in langs)) {
console.error(`Language ${siteLang} is not supported.`);
return;
}
document.head.insertAdjacentHTML("beforeend", `<style>
.notif-wording_channel-name {
color: #c0cbf7; /* var(--yt-spec-text-primary); */
}
html[dark="true"] .notif-wording_channel-name {
color: #99a;
}
</style>`);
function main(list) {
let weMutatedDom = false;
let styleObserverActive = false;
new MutationObserver((mutations, observer) => {
// If the popup is not a notification popup, abort
if (!list.innerText.startsWith(langs[siteLang].notifTitleText)) return;
if (weMutatedDom) {
weMutatedDom = false;
return;
}
let elList = list.querySelectorAll(".message.ytd-notification-renderer");
// The changes we make to notification wording mess up youtube's proccessing of them
// that happens when the notification drop down gets reopened, scrolled down or a new notification gets added.
// To remedy this we need to watch for when the drop down gets hidden and revert all our changes.
if (!styleObserverActive) {
// let listHtml = list.querySelector("#items").innerHTML;
let listWrapper = list.querySelector("tp-yt-iron-dropdown");
new MutationObserver((mutations, observer) => {
if (listWrapper.style.display === "none") {
// Surprisingly removing the HTML works. JK it doesn't
list.querySelector("#items").innerHTML = "";
styleObserverActive = false;
observer.disconnect();
}
}).observe(listWrapper, { attributes: true, attributeFilter: ["style"] });
styleObserverActive = true;
}
for (let el of elList) {
weMutatedDom = true;
el.innerHTML = el.innerHTML.replace(langs[siteLang].video, `<span class="notif-wording_wrapper"><strong class="notif-wording_channel-name">$<channel></strong>: $<title></span>`);
el.innerHTML = el.innerHTML.replace(langs[siteLang].live, `<span class="notif-wording_wrapper">🔴 <strong class="notif-wording_channel-name">$<channel></strong>: $<title></span>`);
el.innerHTML = el.innerHTML.replace(langs[siteLang].premiere, `<span class="notif-wording_wrapper">🎦 <strong class="notif-wording_channel-name">$<channel></strong>: $<title></span>`); // Alternative emojis: 🗓️📹
el.innerHTML = el.innerHTML.replace(langs[siteLang].commentReply, `<span class="notif-wording_wrapper">💬 <strong class="notif-wording_channel-name">$<user></strong>: $<comment></span>`);
el.innerHTML = el.innerHTML.replace(langs[siteLang].commentLike, `<span class="notif-wording_wrapper">💬👍 $<comment></span>`);
// Youtube goes crazy when the notification text gets changed, and adds text from different notifications to existing notification elements. This works around that.
if (el.children.length > 1) {
el.firstElementChild.remove();
}
}
}).observe(list, { childList: true, subtree: true });
}
let list = document.querySelector("ytd-popup-container");
// Most of the time this element exists at the time this script executes, so we just call the main function
if (list) {
main(list);
}
// If it doesn't though, observe body for it to be added
else {
let observer = new MutationObserver(() => {
list = document.querySelector("ytd-popup-container");
if (list) {
observer.disconnect();
main(list);
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
})();