return-youtube-dislike.background.js 3.7 KB

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