forked from MagicJinn/MrBeastify-Youtube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpapapalttify.js
198 lines (160 loc) · 5.72 KB
/
papapalttify.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const imagesPath = "images/";
const images = [];
var isEnabled = false;
var facecamElement = null;
// Apply the overlay
function applyOverlay(thumbnailElement, overlayImageUrl, flip) {
// Create a new img element for the overlay
const overlayImage = document.createElement("img");
overlayImage.src = overlayImageUrl;
overlayImage.style.position = "absolute";
overlayImage.style.top = "0";
overlayImage.style.left = "0";
overlayImage.style.width = "100%";
overlayImage.style.height = "100%";
overlayImage.style.zIndex = "0"; // Ensure overlay is on top
if (flip) {
overlayImage.style.transform = "scaleX(-1)"; // Flip the image horizontally
}
// Style the thumbnailElement to handle absolute positioning
thumbnailElement.style.position = "relative";
// Append the overlay to the parent of the thumbnail
thumbnailElement.parentElement.appendChild(overlayImage);
}
// Looks for all thumbnails and applies overlay
function applyOverlayToThumbnails() {
if (!isEnabled) {
return;
}
// Query all YouTube video thumbnails on the page that haven't been processed yet
// (ignores shorts thumbnails)
const elementQuery =
"ytd-thumbnail:not(.ytd-video-preview, .ytd-rich-grid-slim-media) a > yt-image > img.yt-core-image:only-child:not(.yt-core-attributed-string__image-element)";
const thumbnailElements = document.querySelectorAll(elementQuery);
// Apply overlay to each thumbnail
thumbnailElements.forEach((thumbnailElement) => {
// Apply overlay and add to processed thumbnails
let loops = Math.random() > 0.001 ? 1 : 20; // Easter egg
for (let i = 0; i < loops; i++) {
// Get overlay image URL from your directory
const overlayImageUrl = getRandomImageFromDirectory();
const flip = Math.random() < 0.25; // 25% chance to flip the image
applyOverlay(thumbnailElement, overlayImageUrl, flip);
}
});
}
// Get a random image URL from a directory
function getRandomImageFromDirectory() {
const randomIndex = Math.floor(Math.random() * images.length);
return images[randomIndex];
}
// Checks for all images in the images folder instead of using a preset array, making the extension infinitely scalable
function checkImageExistence(index = 1) {
const testedURL = chrome.runtime.getURL(`${imagesPath}${index}.png`);
fetch(testedURL)
.then((response) => {
// Image exists, add it to the images array
images.push(testedURL);
// Check the next image in the directory
checkImageExistence(index + 1);
})
.catch((error) => { // The function encountered a missing image. Start applying overlays
setInterval(applyOverlayToThumbnails, 100);
console.log(
"Papaplattify Loaded Successfully, " + (index - 1) + " images detected."
);
});
}
// replace various elements
function replaceElements() {
if (isEnabled) {
replaceChannelNames();
replaceTitles();
replaceAvatars();
replaceVideoTitle();
}
}
function replaceChannelNames() {
const elementQuery = "ytd-channel-name a";
const channelNameElements = document.querySelectorAll(elementQuery);
channelNameElements.forEach((channelNameElement) => {
channelNameElement.innerHTML = 'Domo';
})
}
function replaceTitles() {
const elementQuery = "#video-title";
const titleElements = document.querySelectorAll(elementQuery);
titleElements.forEach((titleElement) => {
if (titleElement.getAttribute("aria-label") === null) {
titleElement.setAttribute("aria-label", titleElement.innerHTML)
}
titleElement.innerHTML = 'Papaplatte reagiert auf "' + titleElement.getAttribute("aria-label") + '"';
})
}
function replaceAvatars() {
const elementQuery = "#channel-thumbnail img, #avatar > img";
const avatarElements = document.querySelectorAll(elementQuery);
avatarElements.forEach((avatarElement) => {
avatarElement.src = 'https://yt3.googleusercontent.com/ytc/AOPolaQka2LVeiBI_JbXJi0TjlY82pkLJiPAGFtemPF0=s176-c-k-c0x00ffffff-no-rj';
avatarElement.style.visibility = 'visible';
})
}
function addFacecam() {
if (!isEnabled) {
return;
}
const elementQuery = ".html5-video-container:not(:has(img))";
const videoElement = document.querySelector(elementQuery);
if (videoElement == null) {
return;
}
videoElement.style.height = "100%";
facecamElement = document.createElement("img");
facecamElement.src = chrome.runtime.getURL('facecam.png');
facecamElement.style.position = "absolute";
facecamElement.style.width = "20%";
facecamElement.style.zIndex = "0"; // Ensure overlay is on top
positionFacecam();
videoElement.style.position = "relative";
videoElement.appendChild(facecamElement);
}
function positionFacecam() {
if (!isEnabled) {
return;
}
if (Math.random() >= 0.5) {
facecamElement.style.left = "0";
facecamElement.style.right = "auto";
} else {
facecamElement.style.right = "0";
facecamElement.style.left = "auto";
}
if (Math.random() >= 0.5) {
facecamElement.style.top = "0";
facecamElement.style.bottom = "auto";
} else {
facecamElement.style.bottom = "0";
facecamElement.style.top = "auto";
}
}
function replaceVideoTitle(){
const elementQuery = "#title h1:not(:has(p))";
const titleElement = document.querySelector(elementQuery);
if(titleElement == null){
return;
}
titleElement.style.display = 'flex'
const front = document.createElement("p")
front.innerHTML = 'Papaplatte reagiert auf "';
titleElement.prepend(front)
const last = document.createElement("p")
last.innerHTML = '"';
titleElement.appendChild(last)
}
replaceVideoTitle()
checkImageExistence();
setInterval(replaceElements, 100);
setInterval(addFacecam, 100);
chrome.runtime.sendMessage({ action: 'getPapaplattify' }, function (response) {
this.isEnabled = response.value;
});