fixed userscript

This commit is contained in:
DARKDRAGON532 committed 2021-11-30 21:16:00 +05:30
1 parent 2a4b1b95d1
commit ae1072ac67
1 file changed
+12 -55
@@ -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,13 +120,10 @@ 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 (xhr) {
let result = getDislikesFromYoutubeResponse(xhr.response);
if (result) { if (result) {
cLog("response from youtube:"); cLog("response from youtube:");
cLog(JSON.stringify(result)); cLog(JSON.stringify(result));
@@ -171,25 +134,19 @@ function setState() {
statsSet = true; 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() {