-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserscript.js
More file actions
227 lines (196 loc) · 8.52 KB
/
Copy pathuserscript.js
File metadata and controls
227 lines (196 loc) · 8.52 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// ==UserScript==
// @name Return YouTube Subscribers
// @namespace ryts.mgcounts.com
// @version 4.2
// @description Returns the full unabbreviated subscriber count to channels, videos, and search results on YouTube.
// @author @ok_aj
// @match *://*.youtube.com/*
// @icon https://raw.githubusercontent.com/returnyoutubesubscribers/returnyoutubesubscribers.github.io/main/assets/icon.png
// @run-at document-start
// @grant none
// ==/UserScript==
let currentURL = window.location.href;
const apiLink = "https://backend.mixerno.space/api/youtube/estv3/";
const apiPath = "items[0].statistics.subscriberCount";
const strType = "en-US";
let lastFetchedSubscriberCount = null;
const possibleSubCounters = [
"#owner-sub-count",
"#page-header > yt-page-header-renderer > yt-page-header-view-model > div > div.ytPageHeaderViewModelHeadline > div > yt-content-metadata-view-model > div:nth-child(3) > span:nth-child(1)"
];
function getValueFromJson(json, path) {
const keys = path.replace(/\[(\d+)\]/g, '.$1').split('.'); // Converts [0] to .0 and splits on .
let result = json;
for (let key of keys) {
if (result && result[key] !== undefined) {
result = result[key];
} else {
return undefined;
}
}
return result;
}
function check() {
let url = window.location.href;
if (url.includes("/results?search_query=")) {
setTimeout(function () { searchResults(); }, 1000);
} else {
const subscriberCount = document.querySelector(possibleSubCounters[1]);
const ownerSubCount = document.querySelector(possibleSubCounters[0]);
if (subscriberCount) {
stats();
} else if (ownerSubCount) {
stats2();
} else {
setTimeout(check, 100);
}
}
}
async function fetchSubscriberData(channelId, targetSelectorIndex, element) {
try {
const response = await fetch(apiLink + channelId);
const data = await response.json();
if (data) {
let subscriberText = getValueFromJson(data, apiPath);
console.log(subscriberText)
if (element) {
element.textContent = parseInt(subscriberText).toLocaleString(strType) + " subscribers";
} else {
const targetElement = document.querySelector(possibleSubCounters[targetSelectorIndex]);
if (!targetElement) return;
if (subscriberText !== undefined && subscriberText !== null) {
if (targetElement.getAttribute("loaded") === "true") return;
subscriberText = String(subscriberText).trim();
const formattedCount = parseInt(subscriberText).toLocaleString(strType);
targetElement.textContent = formattedCount + " subscribers";
targetElement.setAttribute("loaded", "true");
targetElement.removeAttribute("is-empty");
subs = parseInt(subscriberText);
lastFetchedSubscriberCount = formattedCount;
const ownerSubCount = document.querySelector("#owner-sub-count");
if (ownerSubCount && ownerSubCount.childNodes.length > 1) {
for (let i = ownerSubCount.childNodes.length - 1; i >= 0; i--) {
const node = ownerSubCount.childNodes[i];
if (node.nodeType === Node.TEXT_NODE && node.textContent.match(/^\d+/)) {
ownerSubCount.removeChild(node);
}
}
}
const updateAdditionalInfo = () => {
const additionalInfoElement = document.querySelector("#additional-info-container > table > tbody > tr:nth-child(6) > td:nth-child(2)");
if (additionalInfoElement) {
additionalInfoElement.textContent = formattedCount + " subscribers";
clearInterval(infoCheckInterval);
}
};
updateAdditionalInfo();
const infoCheckInterval = setInterval(updateAdditionalInfo, 500);
}
}
} else {
console.error("[RYTS] Error fetching subscriber data:", data);
}
} catch (error) {
console.error("[RYTS] Error fetching subscriber data:", error);
}
}
function stats() {
const req = new XMLHttpRequest();
req.open("GET", currentURL, false);
req.send(null);
if (req.status === 200) {
const res = req.responseText;
const channelId = extractChannelId(res);
if (channelId) {
fetchSubscriberData(channelId, 1);
}
}
}
function stats2() {
const req = new XMLHttpRequest();
req.open("GET", currentURL, false);
req.send(null);
if (req.status === 200) {
const res = req.responseText;
const channelId = extractChannelId(res);
if (channelId) {
fetchSubscriberData(channelId, 0);
}
}
}
function extractChannelId(responseText) {
const browseIdMatch = responseText.match(/"browse_id":"(.*?)"/);
const channelIdMatch = responseText.match(/"channelId":"(.*?)"/);
const externalIdMatch = responseText.match(/"externalId":"(.*?)"/);
console.log("[RYTS] Browse ID:", browseIdMatch?.[1]);
console.log("[RYTS] Channel ID:", channelIdMatch?.[1]);
console.log("[RYTS] External ID:", externalIdMatch?.[1]);
return externalIdMatch?.[1] || browseIdMatch?.[1] || channelIdMatch?.[1] || null;
}
function clearLoadedState() {
for (let selector of possibleSubCounters) {
const element = document.querySelector(selector);
if (element) {
element.removeAttribute("loaded");
console.log("[RYTS] Cleared loaded state for selector:", selector);
}
}
}
setInterval(() => {
const url = window.location.href;
if (currentURL !== url) {
currentURL = url;
console.log("[RYTS] URL changed:", url);
clearLoadedState();
if (url.includes("/results?search_query=")) {
setTimeout(searchResults, 1000);
} else if (/\/channel\/|\/c\/|\/user\/|\/watch\?v=|\/@/.test(url)) {
const subscriberCount = document.querySelector(possibleSubCounters[1]);
const isWatchPage = url.includes("/watch?v=");
console.log("[RYTS]", isWatchPage ? "Watch page" : "Channel page");
if (isWatchPage) {
stats2();
} else {
const updateStats = () => {
if (subscriberCount && subscriberCount.getAttribute("is-empty") === null) {
stats();
} else {
setTimeout(updateStats, 500);
}
};
updateStats();
}
}
}
}, 500);
async function searchResults() {
console.log('[RYTS] Search Page')
try {
let channelRenderers = document.querySelectorAll("ytd-channel-renderer");
for (const channelRenderer of channelRenderers) {
try {
let link = channelRenderer.querySelector("#main-link").href;
console.log(link)
if (link) {
if (link.includes("@")) {
fetch("https://www.youtube.com/@" + link.split("@")[1])
.then((response) => response.text())
.then(async (data) => {
if (data && data.includes('externalId')) {
let channelId = data.split('"externalId":"')[1].split('"')[0];
fetchSubscriberData(channelId, 0, channelRenderer.querySelector("#video-count"));
}
});
} else {
}
}
} catch (error) {
console.error("[RYTS] Error fetching search results:", error);
}
}
} catch (error) {
console.error("[RYTS] Error fetching search results:", error);
setTimeout(searchResults, 1000);
}
}
check();