background.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
  2. const securityToken = Math.random().toString(36).substring(2, 15);
  3. chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  4. sendResponse(securityToken);
  5. });
  6. chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
  7. if (request.securityToken === securityToken) {
  8. if (request.message === "get_auth_token") {
  9. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  10. // console.log(token);
  11. // chrome.identity.getProfileUserInfo(function (userInfo) {
  12. // console.log(JSON.stringify(userInfo));
  13. // });
  14. // });
  15. } else if (request.message === "log_off") {
  16. // console.log("logging off");
  17. // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
  18. } else if (request.message == "set_state") {
  19. // console.log(request);
  20. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  21. let token = "";
  22. fetch(`${apiUrl}/votes?videoId=${request.videoId}`, {
  23. method: "GET",
  24. headers: {
  25. Accept: "application/json",
  26. Authorization: "Bearer " + token,
  27. },
  28. })
  29. .then((response) => response.json())
  30. .then((response) => {
  31. console.log(response);
  32. sendResponse(response);
  33. })
  34. .catch();
  35. //});
  36. return true;
  37. }
  38. }
  39. });