mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-01 18:31:24 +01:00
Added fetch from youtube for userscript
This commit is contained in:
1 parent
f44f22eaae
commit
8ce61c8742
1 file changed
+63
-4
@@ -34,9 +34,15 @@ function doXHR(opts) {
|
|||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
xhr.open(opts.method, opts.url, true);
|
xhr.open(opts.method, opts.url, true);
|
||||||
xhr.onload = () => opts.onload({
|
if (opts.responseType === 'text') {
|
||||||
response: JSON.parse(xhr.responseText),
|
xhr.onload = () => opts.onload({
|
||||||
});
|
response: xhr.responseText,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
xhr.onload = () => opts.onload({
|
||||||
|
response: JSON.parse(xhr.responseText),
|
||||||
|
});
|
||||||
|
}
|
||||||
xhr.onerror = err => console.error('XHR Failed', err);
|
xhr.onerror = err => console.error('XHR Failed', err);
|
||||||
xhr.send();
|
xhr.send();
|
||||||
}
|
}
|
||||||
@@ -145,6 +151,28 @@ function createRateBar(likes, dislikes) {
|
|||||||
|
|
||||||
function setState() {
|
function setState() {
|
||||||
cLog('Fetching votes...');
|
cLog('Fetching votes...');
|
||||||
|
let statsSet = false;
|
||||||
|
doXHR({
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'text',
|
||||||
|
url: `https://www.youtube.com/watch?v=${getVideoId()}`,
|
||||||
|
onload: function (xhr) {
|
||||||
|
if (xhr) {
|
||||||
|
let result = getDislikesFromYoutubeResponse(xhr.response);
|
||||||
|
if (result) {
|
||||||
|
cLog("response from youtube:");
|
||||||
|
cLog(JSON.stringify(result));
|
||||||
|
if (result.likes || result.dislikes) {
|
||||||
|
const formattedDislike = numberFormat(result.dislikes);
|
||||||
|
setDislikes(formattedDislike);
|
||||||
|
createRateBar(result.likes, result.dislikes);
|
||||||
|
statsSet = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
doXHR({
|
doXHR({
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -153,7 +181,7 @@ function setState() {
|
|||||||
"https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
|
"https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
|
||||||
getVideoId(),
|
getVideoId(),
|
||||||
onload: function (xhr) {
|
onload: function (xhr) {
|
||||||
if (xhr != undefined) {
|
if (xhr != undefined && !statsSet) {
|
||||||
const { dislikes, likes } = xhr.response;
|
const { dislikes, likes } = xhr.response;
|
||||||
cLog(`Received count: ${dislikes}`);
|
cLog(`Received count: ${dislikes}`);
|
||||||
setDislikes(numberFormat(dislikes));
|
setDislikes(numberFormat(dislikes));
|
||||||
@@ -212,6 +240,37 @@ function numberFormat(numberState) {
|
|||||||
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
|
return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDislikesFromYoutubeResponse(htmlResponse) {
|
||||||
|
let start =
|
||||||
|
htmlResponse.indexOf('"videoDetails":') + '"videoDetails":'.length;
|
||||||
|
let end =
|
||||||
|
htmlResponse.indexOf('"isLiveContent":false}', start) +
|
||||||
|
'"isLiveContent":false}'.length;
|
||||||
|
if (end < start) {
|
||||||
|
end =
|
||||||
|
htmlResponse.indexOf('"isLiveContent":true}', start) +
|
||||||
|
'"isLiveContent":true}'.length;
|
||||||
|
}
|
||||||
|
let jsonStr = htmlResponse.substring(start, end);
|
||||||
|
let jsonResult = JSON.parse(jsonStr);
|
||||||
|
let rating = jsonResult.averageRating;
|
||||||
|
|
||||||
|
start = htmlResponse.indexOf('"topLevelButtons":[', end);
|
||||||
|
start =
|
||||||
|
htmlResponse.indexOf('"accessibilityData":', start) +
|
||||||
|
'"accessibilityData":'.length;
|
||||||
|
end = htmlResponse.indexOf("}", start);
|
||||||
|
let likes = +htmlResponse.substring(start, end).replace(/\D/g, "");
|
||||||
|
let dislikes = (likes * (5 - rating)) / (rating - 1);
|
||||||
|
let result = {
|
||||||
|
likes,
|
||||||
|
dislikes: Math.round(dislikes),
|
||||||
|
rating,
|
||||||
|
viewCount: +jsonResult.viewCount,
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
function setEventListeners(evt) {
|
function setEventListeners(evt) {
|
||||||
function checkForJS_Finish() {
|
function checkForJS_Finish() {
|
||||||
if (getButtons()?.offsetParent && isVideoLoaded()) {
|
if (getButtons()?.offsetParent && isVideoLoaded()) {
|
||||||
|
|||||||
Reference in new issue
Block a user