background.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }
  54. }
  55. );
  56. const sentIds = new Set();
  57. let toSend = [];