background.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
  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({...result, videoId: request.videoId});
  61. sendResponse(result);
  62. });
  63. }
  64. }
  65. );
  66. const sentIds = new Set();
  67. let toSend = [];
  68. function getDislikesFromYoutubeResponse(htmlResponse) {
  69. let start =
  70. htmlResponse.indexOf('"videoDetails":') + '"videoDetails":'.length;
  71. let end =
  72. htmlResponse.indexOf('"isLiveContent":false}', start) +
  73. '"isLiveContent":false}'.length;
  74. if (end < start) {
  75. end =
  76. htmlResponse.indexOf('"isLiveContent":true}', start) +
  77. '"isLiveContent":true}'.length;
  78. }
  79. let jsonStr = htmlResponse.substring(start, end);
  80. let jsonResult = JSON.parse(jsonStr);
  81. let rating = jsonResult.averageRating;
  82. start = htmlResponse.indexOf('"topLevelButtons":[', end);
  83. start =
  84. htmlResponse.indexOf('"accessibilityData":', start) +
  85. '"accessibilityData":'.length;
  86. end = htmlResponse.indexOf("}", start);
  87. let likes = +htmlResponse.substring(start, end).replace(/\D/g, "");
  88. let dislikes = (likes * (5 - rating)) / (rating - 1);
  89. let result = {
  90. likes,
  91. dislikes: Math.round(dislikes),
  92. rating,
  93. viewCount: +jsonResult.viewCount
  94. };
  95. return result;
  96. }
  97. function sendUserSubmittedStatisticsToApi(statistics) {
  98. fetch(`${apiUrl}/votes/user-submitted`, {
  99. method: "POST",
  100. headers: {
  101. "Content-Type": "application/json",
  102. },
  103. body: JSON.stringify(statistics),
  104. });
  105. }