This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
script.js
204 lines (182 loc) · 6.69 KB
/
script.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
198
199
200
201
202
203
204
// This is a modified script from the original one by aquelemiguel.
chrome.storage.sync.get("primary", ({ primary }) => {
chrome.storage.sync.get("secondary", ({ secondary }) => {
(function () {
const BASE_ENDPOINT = "https://returnyoutubedislikeapi.com/votes";
const video_id = new URLSearchParams(window.location.search).get("v");
async function run() {
const info = await fetchInfo(video_id);
const percentage_like = likePercentage(
parseInt(info.likes),
parseInt(info.dislikes)
);
addBar(info.likes, info.dislikes, percentage_like);
editDislikes(info.dislikes);
console.catch((err) => console.error(err));
}
function numberToAbbreviatedString(number) {
let result = "";
let num = number;
let unit = "";
let numStr = num.toString();
if (numStr.length > 3) {
num = num / 1000;
unit = "K";
}
if (numStr.length > 6) {
num = num / 1000;
unit = "M";
}
if (numStr.length > 9) {
num = num / 1000;
unit = "B";
}
result =
numStr.length <= 3 && unit === ""
? num.toFixed(0) + unit
: num.toFixed(1) + unit;
return result;
}
async function fetchInfo(videoId) {
if (!videoId) {
videoId = new URLSearchParams(window.location.search).get("v");
}
const endpoint = `${BASE_ENDPOINT}?videoId=${videoId}`;
return fetch(endpoint)
.then((r) => r.json())
.then(
(r) =>
(values = {
dislikes: parseInt(r.dislikes),
likes: parseInt(r.likes),
})
);
}
function editDislikes(dislikeNo) {
let selector;
// Fetch the dislike label
// checks for new UI of youtube or Old one
const selectorOldUi =
"ytd-menu-renderer.ytd-video-primary-info-renderer > div > :nth-child(2) yt-formatted-string";
const selectorNewUi =
"ytd-menu-renderer.ytd-watch-metadata > div > :nth-child(2) yt-formatted-string";
const checkForNewUI = document.getElementById(
"description-and-actions"
);
if (checkForNewUI) {
selector = selectorNewUi;
} else {
selector = selectorOldUi;
}
const dislikeLabel = document.querySelector(selector);
// Update the label with the new dislike count
const formattedDislikes = numberToAbbreviatedString(dislikeNo);
if (formattedDislikes === "NaN") {
run();
let progressBar = document.getElementById("custom-progress");
progressBar.parentElement.removeChild(progressBar);
} else if (formattedDislikes !== "NaN") {
dislikeLabel.textContent = formattedDislikes;
}
}
function likePercentage(likeCount, dislikeCount) {
return (100 * likeCount) / (likeCount + dislikeCount);
}
function addBar(likes, dislikes, likePercentage) {
// checks for new UI of youtube or Old
const selectorOldUi = document.getElementById("menu-container");
const selectorNewUi = document.getElementById("actions-inner");
if (selectorOldUi || selectorNewUi) {
let selector;
if (selectorNewUi) {
selector = selectorNewUi;
} else {
selector = selectorOldUi;
}
const prgroess = document.getElementById("custom-progress");
let clipButton = document.querySelector('[aria-label="Clip"]');
let ThanksButton = document.querySelector('[aria-label="Thanks"]');
if (prgroess) {
return;
}
const progress = document.createElement("div");
const tooltip = document.createElement("div");
const color = document.createElement("div");
// Fix for Dark youtube Mode and Light youtube mode
let colorBackground;
let progressBackround;
let darkMode = document
.getElementsByTagName("html")[0]
.getAttribute("dark");
const reg = /^#[0-9A-F]{6}$/i;
if (
!primary ||
!secondary ||
!reg.test(primary) ||
!reg.test(secondary)
) {
if (darkMode) {
progressBackround = "grey";
colorBackground = "white";
} else {
colorBackground = "black";
progressBackround = "grey";
}
} else {
colorBackground = primary;
progressBackround = secondary;
}
progress.className = "progress";
progress.style.position = "relative";
progress.style.height = "3px";
progress.style.width = "40%";
progress.style.background = `${progressBackround}`;
progress.style.marginright = "20px";
progress.setAttribute("id", "custom-progress");
progress.style.marginTop = "3px";
color.className = "color";
color.style.position = "absolute";
color.style.background = `${colorBackground}`;
color.style.width = `${likePercentage}%`;
color.style.height = "3px";
color.setAttribute("id", "color");
progress.addEventListener("mouseover", () => {
tooltip.innerHTML = `
<!--<tp-yt-paper-tooltip position="top" class="" role="tooltip" tabindex="-1" style="left: 25.6833px; bottom: -64px;"><!--css-build:shady-->
<div id="tooltip" class="style-scope tp-yt-paper-tooltip visible" style="background:#616161; max-width:110px; Position:Absolute; Z-Index: 4">
${likes} / ${dislikes}
</tp-yt-paper-tooltip>
`;
selector.appendChild(tooltip);
});
progress.addEventListener("mouseout", () => {
tooltip.parentNode.removeChild(tooltip);
});
if (clipButton) {
progress.style.width = "32.5%";
} else if (ThanksButton) {
progress.style.width = "30.5%";
}
if (clipButton && ThanksButton) {
progress.style.width = "25.5%";
}
progress.appendChild(color);
selector.appendChild(progress);
}
}
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
// listen for messages sent from background.js
if (request.message === "progressbar") {
let progressBar = document.getElementById("custom-progress");
progressBar.parentElement.removeChild(progressBar);
}
// run();
});
run();
})();
});
});