forked from LiamHarrison25/Jambofy-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
142 lines (119 loc) · 4.79 KB
/
main.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
(() =>
{
const imageFilePath = "assets/images/";
const numImages = 170;
const flipExcludedCutoff = 168; //NOTE: this number represents the cutoff for where the non flippable images start
const flipRandomPercent = 2; //NOTE: the number represents how many numbers to randomly choose. bigger = less likely, smaller = more likely.
var isEnabled = true;
var opacityPercentage = 100;
chrome.storage.local.get(["opacity"], (result) =>
{
console.log("Opacity received on main")
const { opacity } = result;
if(opacity)
{
opacityPercentage = opacity;
}
})
//NOTE: The purpose of this function is to get all YouTube thumbnails on the page
function getThumbnails()
{
const thumbnailQuery = "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),.ytp-videowall-still-image:not([style*='extension:'])";
const thumbnail = document.querySelectorAll(thumbnailQuery);
thumbnail.forEach((image) =>
{
let counter = Math.random() > 0.001 ? 1 : 20;
let i = 0;
for(i = 0; i < counter; i++)
{
const index = getRandomImage();
let flip = getImageState(index);
let url = getImageURL(index);
applyThumbnails(image, url, flip);
}
}
)
}
//NOTE: The purpose of this function is to return the url of an image
function getImageURL(index)
{
return chrome.runtime.getURL(`${imageFilePath}${index}.png`);
}
//NOTE: The purpose of this function is to apply the thumbnail images to the thumbnails on YouTube.com
function applyThumbnails(image, imageUrl, flip = false)
{
if (image.nodeName == "IMG")
{``
const overlay = document.createElement("img");
overlay.src = imageUrl;
overlay.style.position = "absolute";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.width = "100%";
overlay.style.height = "100%";
overlay.style.zIndex = "0";
overlay.style.opacity = opacityPercentage / 100.0;
if(flip)
{
overlay.style.transform = "scaleX(-1)"; //flips the image
}
image.style.position = "relative";
image.parentElement.appendChild(overlay);
}
else if (image.nodeName == "DIV")
{
image.style.backgroundImage = `url("${imageUrl}"), ` + image.style.backgroundImage;
}
}
//NOTE: The purpose of this function is to take in a max number, and return a random number from 0 to that max number
function getRandomInt(max)
{
return Math.floor(Math.random() * max);
}
//NOTE: The purpose of this function is to get a random image to display
function getRandomImage()
{
//NOTE: percent is even across the board for any given image to be chosen
let random = 0;
random = getRandomInt(numImages + 1); //NOTE: +1 is because max is not inclusive
return random;
}
//NOTE: The purpose of this function is to randomly determine whether or not to flip the image or not
function getImageState(num)
{
//NOTE: percent to flip is default 50% when flipRandomPercent = 2
//Prevents flipping non-flippable images
if(num >= flipExcludedCutoff)
{
return false;
}
let random = 0;
random = getRandomInt(flipRandomPercent); //returns a random number from 0 to flipRandomPercent
if(random === 1)
{
return true; //STATE: flip image
}
else
{
return false; //STATE: do not flip image
}
}
//NOTE: The purpose of this function is to check if an image exists
async function doesImageExist(index)
{
const url = getImageURL(index);
return fetch(url).then(() =>
{
return true
}).catch(error =>
{
return false
})
}
//runs the functions
if(isEnabled) //checks if the user has disabled the plugin or not
{
setInterval(getThumbnails, 100);
}
}
)();