mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-03 03:11:23 +01:00
fixed userscript
This commit is contained in:
1 parent
2a4b1b95d1
commit
ae1072ac67
1 file changed
+20
-63
@@ -19,40 +19,6 @@ function cLog(text, subtext = "") {
|
|||||||
console.log(`[Return Youtube Dislikes] ${text} ${subtext}`);
|
console.log(`[Return Youtube Dislikes] ${text} ${subtext}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function doXHR(opts) {
|
|
||||||
if (typeof GM_xmlhttpRequest === "function") {
|
|
||||||
return GM_xmlhttpRequest(opts);
|
|
||||||
}
|
|
||||||
if (typeof GM !== "undefined") {
|
|
||||||
/*This will prevent from throwing "Uncaught ReferenceError: GM is not defined"*/ if (
|
|
||||||
typeof GM.xmlHttpRequest === "function"
|
|
||||||
) {
|
|
||||||
return GM.xmlHttpRequest(opts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.warn(
|
|
||||||
"Unable to detect UserScript plugin, falling back to native XHR."
|
|
||||||
);
|
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.open(opts.method, opts.url, true);
|
|
||||||
if (opts.responseType === "text") {
|
|
||||||
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.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getButtons() {
|
function getButtons() {
|
||||||
if (document.getElementById("menu-container").offsetParent === null) {
|
if (document.getElementById("menu-container").offsetParent === null) {
|
||||||
return document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div");
|
return document.querySelector("ytd-menu-renderer.ytd-watch-metadata > div");
|
||||||
@@ -154,42 +120,33 @@ function createRateBar(likes, dislikes) {
|
|||||||
function setState() {
|
function setState() {
|
||||||
cLog("Fetching votes...");
|
cLog("Fetching votes...");
|
||||||
let statsSet = false;
|
let statsSet = false;
|
||||||
doXHR({
|
|
||||||
method: "GET",
|
fetch(`https://www.youtube.com/watch?v=${getVideoId()}`).then(response => {
|
||||||
responseType: "text",
|
response.text().then(text => {
|
||||||
url: `https://www.youtube.com/watch?v=${getVideoId()}`,
|
let result = getDislikesFromYoutubeResponse(text)
|
||||||
onload: function (xhr) {
|
if (result) {
|
||||||
if (xhr) {
|
cLog("response from youtube:");
|
||||||
let result = getDislikesFromYoutubeResponse(xhr.response);
|
cLog(JSON.stringify(result));
|
||||||
if (result) {
|
if (result.likes || result.dislikes) {
|
||||||
cLog("response from youtube:");
|
const formattedDislike = numberFormat(result.dislikes);
|
||||||
cLog(JSON.stringify(result));
|
setDislikes(formattedDislike);
|
||||||
if (result.likes || result.dislikes) {
|
createRateBar(result.likes, result.dislikes);
|
||||||
const formattedDislike = numberFormat(result.dislikes);
|
statsSet = true;
|
||||||
setDislikes(formattedDislike);
|
|
||||||
createRateBar(result.likes, result.dislikes);
|
|
||||||
statsSet = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
doXHR({
|
fetch(`https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=${getVideoId()}`).then(response => {
|
||||||
method: "GET",
|
response.json().then(json => {
|
||||||
responseType: "json",
|
if (json && !statsSet) {
|
||||||
url:
|
const { dislikes, likes } = json;
|
||||||
"https://return-youtube-dislike-api.azurewebsites.net/votes?videoId=" +
|
|
||||||
getVideoId(),
|
|
||||||
onload: function (xhr) {
|
|
||||||
if (xhr != undefined && !statsSet) {
|
|
||||||
const { dislikes, likes } = xhr.response;
|
|
||||||
cLog(`Received count: ${dislikes}`);
|
cLog(`Received count: ${dislikes}`);
|
||||||
setDislikes(numberFormat(dislikes));
|
setDislikes(numberFormat(dislikes));
|
||||||
createRateBar(likes, dislikes);
|
createRateBar(likes, dislikes);
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function likeClicked() {
|
function likeClicked() {
|
||||||
|
|||||||
Reference in new issue
Block a user