background.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }
  48. });
  49. const sentIds = new Set();
  50. let toSend = [];
  51. browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
  52. if (changeInfo.status == "complete") {
  53. if (tab.url && tab.url.indexOf("youtube.") < 0) return;
  54. browser.tabs.get(tabId, (tab) => {
  55. browser.tabs.executeScript(tab.id, {
  56. file: "script.js",
  57. });
  58. });
  59. }
  60. });