mirror of
https://github.com/Alvin-Zilverstand/return-youtube-dislike.git
synced 2026-12-01 18:31:24 +01:00
0.9 for chrome and FF, Icons
This commit is contained in:
1 parent
e60a0921dc
commit
129328fc64
19 files changed
+100
-83
No files matched your search
@@ -0,0 +1,124 @@
|
||||
const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
|
||||
|
||||
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.message === "get_auth_token") {
|
||||
// chrome.identity.getAuthToken({ interactive: true }, function (token) {
|
||||
// console.log(token);
|
||||
// chrome.identity.getProfileUserInfo(function (userInfo) {
|
||||
// console.log(JSON.stringify(userInfo));
|
||||
// });
|
||||
// });
|
||||
} else if (request.message === "log_off") {
|
||||
// console.log("logging off");
|
||||
// chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
|
||||
} else if (request.message == "set_state") {
|
||||
console.log(request);
|
||||
// chrome.identity.getAuthToken({ interactive: true }, function (token) {
|
||||
let token = "";
|
||||
fetch(`${apiUrl}/votes?videoId=${request.videoId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: "Bearer " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((response) => {
|
||||
console.log(response);
|
||||
sendResponse(response);
|
||||
})
|
||||
.catch();
|
||||
//});
|
||||
return true;
|
||||
} else if (request.message == "send_links") {
|
||||
toSend = toSend.concat(request.videoIds.filter((x) => !sentIds.has(x)));
|
||||
if (toSend.length >= 20) {
|
||||
fetch(`${apiUrl}/votes`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(toSend),
|
||||
});
|
||||
for (const toSendUrl of toSend) {
|
||||
sentIds.add(toSendUrl);
|
||||
}
|
||||
toSend = [];
|
||||
}
|
||||
} else if (request.message == "fetch_from_youtube") {
|
||||
fetch(`https://www.youtube.com/watch?v=${request.videoId}`, {
|
||||
method: "GET",
|
||||
})
|
||||
.then((response) => response.text())
|
||||
.then((text) => {
|
||||
let result = getDislikesFromYoutubeResponse(text);
|
||||
sendResponse(result);
|
||||
try {
|
||||
sendUserSubmittedStatisticsToApi({
|
||||
...result,
|
||||
videoId: request.videoId,
|
||||
});
|
||||
} catch {}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
const sentIds = new Set();
|
||||
let toSend = [];
|
||||
let lastCalled = new Date();
|
||||
|
||||
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
||||
if (changeInfo.status == "complete" && new Date() - lastCalled > 100) {
|
||||
lastCalled = new Date();
|
||||
console.log("Tab update complete");
|
||||
|
||||
if (tab.url && tab.url.indexOf("youtube.") < 0) return;
|
||||
browser.tabs.get(tabId, (tab) => {
|
||||
browser.tabs.executeScript(tab.id, {
|
||||
file: "return-youtube-dislike.script.js",
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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 sendUserSubmittedStatisticsToApi(statistics) {
|
||||
fetch(`${apiUrl}/votes/user-submitted`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(statistics),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user