return-youtube-dislike.background.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const apiUrl = "https://returnyoutubedislikeapi.com";
  2. // Security token causes issues if extension is reloaded/updated while several tabs are open
  3. // const securityToken = Math.random().toString(36).substring(2, 15);
  4. //
  5. // chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  6. // sendResponse(securityToken);
  7. // });
  8. chrome.runtime.onMessageExternal.addListener(
  9. (request, sender, sendResponse) => {
  10. if (request.message === "get_auth_token") {
  11. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  12. // console.log(token);
  13. // chrome.identity.getProfileUserInfo(function (userInfo) {
  14. // console.log(JSON.stringify(userInfo));
  15. // });
  16. // });
  17. } else if (request.message === "log_off") {
  18. // console.log("logging off");
  19. // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
  20. } else if (request.message == "set_state") {
  21. // console.log(request);
  22. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  23. let token = "";
  24. fetch(`${apiUrl}/votes?videoId=${request.videoId}`, {
  25. method: "GET",
  26. headers: {
  27. Accept: "application/json",
  28. Authorization: "Bearer " + token,
  29. },
  30. })
  31. .then((response) => response.json())
  32. .then((response) => {
  33. sendResponse(response);
  34. })
  35. .catch();
  36. //});
  37. return true;
  38. } else if (request.message == "send_links") {
  39. toSend = toSend.concat(request.videoIds.filter((x) => !sentIds.has(x)));
  40. if (toSend.length >= 20) {
  41. fetch(`${apiUrl}/votes`, {
  42. method: "POST",
  43. headers: {
  44. "Content-Type": "application/json",
  45. },
  46. body: JSON.stringify(toSend),
  47. });
  48. for (const toSendUrl of toSend) {
  49. sentIds.add(toSendUrl);
  50. }
  51. toSend = [];
  52. }
  53. } else if (request.message == "fetch_from_youtube") {
  54. fetch(`https://www.youtube.com/watch?v=${request.videoId}`, {
  55. method: "GET",
  56. })
  57. .then((response) => response.text())
  58. .then((text) => {
  59. var result = getDislikesFromYoutubeResponse(text);
  60. sendUserSubmittedStatisticsToApi({
  61. ...result,
  62. videoId: request.videoId,
  63. });
  64. sendResponse(result);
  65. });
  66. }
  67. }
  68. );
  69. const sentIds = new Set();
  70. let toSend = [];
  71. function getDislikesFromYoutubeResponse(htmlResponse) {
  72. let start =
  73. htmlResponse.indexOf('"videoDetails":') + '"videoDetails":'.length;
  74. let end =
  75. htmlResponse.indexOf('"isLiveContent":false}', start) +
  76. '"isLiveContent":false}'.length;
  77. if (end < start) {
  78. end =
  79. htmlResponse.indexOf('"isLiveContent":true}', start) +
  80. '"isLiveContent":true}'.length;
  81. }
  82. let jsonStr = htmlResponse.substring(start, end);
  83. let jsonResult = JSON.parse(jsonStr);
  84. let rating = jsonResult.averageRating;
  85. start = htmlResponse.indexOf('"topLevelButtons":[', end);
  86. start =
  87. htmlResponse.indexOf('"accessibilityData":', start) +
  88. '"accessibilityData":'.length;
  89. end = htmlResponse.indexOf("}", start);
  90. let likes = +htmlResponse.substring(start, end).replace(/\D/g, "");
  91. let dislikes = (likes * (5 - rating)) / (rating - 1);
  92. let result = {
  93. likes,
  94. dislikes: Math.round(dislikes),
  95. rating,
  96. viewCount: +jsonResult.viewCount,
  97. };
  98. return result;
  99. }
  100. function sendUserSubmittedStatisticsToApi(statistics) {
  101. fetch(`${apiUrl}/votes/user-submitted`, {
  102. method: "POST",
  103. headers: {
  104. "Content-Type": "application/json",
  105. },
  106. body: JSON.stringify(statistics),
  107. });
  108. }