ryd.background.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const apiUrl = "https://returnyoutubedislikeapi.com";
  2. let api;
  3. if (typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined")
  4. api = chrome;
  5. else if (
  6. typeof browser !== "undefined" &&
  7. typeof browser.runtime !== "undefined"
  8. )
  9. api = browser;
  10. api.runtime.onMessage.addListener((request, sender, sendResponse) => {
  11. if (request.message === "get_auth_token") {
  12. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  13. // console.log(token);
  14. // chrome.identity.getProfileUserInfo(function (userInfo) {
  15. // console.log(JSON.stringify(userInfo));
  16. // });
  17. // });
  18. } else if (request.message === "log_off") {
  19. // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
  20. } else if (request.message == "set_state") {
  21. // chrome.identity.getAuthToken({ interactive: true }, function (token) {
  22. let token = "";
  23. fetch(`${apiUrl}/votes?videoId=${request.videoId}&likeCount=${request.likeCount || ''}`, {
  24. method: "GET",
  25. headers: {
  26. Accept: "application/json",
  27. },
  28. })
  29. .then((response) => response.json())
  30. .then((response) => {
  31. sendResponse(response);
  32. })
  33. .catch();
  34. return true;
  35. } else if (request.message == "send_links") {
  36. toSend = toSend.concat(request.videoIds.filter((x) => !sentIds.has(x)));
  37. if (toSend.length >= 20) {
  38. fetch(`${apiUrl}/votes`, {
  39. method: "POST",
  40. headers: {
  41. "Content-Type": "application/json",
  42. },
  43. body: JSON.stringify(toSend),
  44. });
  45. for (const toSendUrl of toSend) {
  46. sentIds.add(toSendUrl);
  47. }
  48. toSend = [];
  49. }
  50. } else if (request.message == "register") {
  51. register();
  52. return true;
  53. } else if (request.message == "send_vote") {
  54. sendVote(request.videoId, request.vote);
  55. return true;
  56. }
  57. });
  58. async function sendVote(videoId, vote) {
  59. api.storage.sync.get(null, async (storageResult) => {
  60. if (!storageResult.userId || !storageResult.registrationConfirmed) {
  61. register().then(() => sendVote(videoId, vote));
  62. }
  63. fetch(`${apiUrl}/interact/vote`, {
  64. method: "POST",
  65. headers: {
  66. "Content-Type": "application/json",
  67. },
  68. body: JSON.stringify({
  69. userId: storageResult.userId,
  70. videoId,
  71. value: vote,
  72. }),
  73. })
  74. .then((response) => response.json())
  75. .then((response) => {
  76. solvePuzzle(response).then((solvedPuzzle) => {
  77. fetch(`${apiUrl}/interact/confirmVote`, {
  78. method: "POST",
  79. headers: {
  80. "Content-Type": "application/json",
  81. },
  82. body: JSON.stringify({
  83. ...solvedPuzzle,
  84. userId: storageResult.userId,
  85. videoId,
  86. }),
  87. });
  88. });
  89. });
  90. });
  91. }
  92. function register() {
  93. let userId = generateUserID();
  94. api.storage.sync.set({ userId });
  95. return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, {
  96. method: "GET",
  97. headers: {
  98. Accept: "application/json",
  99. },
  100. })
  101. .then((response) => response.json())
  102. .then((response) => {
  103. return solvePuzzle(response).then((solvedPuzzle) => {
  104. return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, {
  105. method: "POST",
  106. headers: {
  107. "Content-Type": "application/json",
  108. },
  109. body: JSON.stringify(solvedPuzzle),
  110. }).then((response) =>
  111. response.json().then((result) => {
  112. if (result === true) {
  113. return api.storage.sync.set({ registrationConfirmed: true });
  114. }
  115. })
  116. );
  117. });
  118. })
  119. .catch();
  120. }
  121. api.storage.sync.get(null, (res) => {
  122. if (!res.userId || !res.registrationConfirmed) {
  123. register();
  124. }
  125. });
  126. const sentIds = new Set();
  127. let toSend = [];
  128. function sendUserSubmittedStatisticsToApi(statistics) {
  129. fetch(`${apiUrl}/votes/user-submitted`, {
  130. method: "POST",
  131. headers: {
  132. "Content-Type": "application/json",
  133. },
  134. body: JSON.stringify(statistics),
  135. });
  136. }
  137. function countLeadingZeroes(uInt8View, limit) {
  138. let zeroes = 0;
  139. let value = 0;
  140. for (let i = 0; i < uInt8View.length; i++) {
  141. value = uInt8View[i];
  142. if (value === 0) {
  143. zeroes += 8;
  144. } else {
  145. let count = 1;
  146. if (value >>> 4 === 0) {
  147. count += 4;
  148. value <<= 4;
  149. }
  150. if (value >>> 6 === 0) {
  151. count += 2;
  152. value <<= 2;
  153. }
  154. zeroes += count - (value >>> 7);
  155. break;
  156. }
  157. if (zeroes >= limit) {
  158. break;
  159. }
  160. }
  161. return zeroes;
  162. }
  163. async function solvePuzzle(puzzle) {
  164. let challenge = Uint8Array.from(atob(puzzle.challenge), (c) =>
  165. c.charCodeAt(0)
  166. );
  167. let buffer = new ArrayBuffer(20);
  168. let uInt8View = new Uint8Array(buffer);
  169. let uInt32View = new Uint32Array(buffer);
  170. let maxCount = Math.pow(2, puzzle.difficulty) * 5;
  171. for (let i = 4; i < 20; i++) {
  172. uInt8View[i] = challenge[i - 4];
  173. }
  174. for (let i = 0; i < maxCount; i++) {
  175. uInt32View[0] = i;
  176. let hash = await crypto.subtle.digest("SHA-512", buffer);
  177. let hashUint8 = new Uint8Array(hash);
  178. if (countLeadingZeroes(hashUint8) >= puzzle.difficulty) {
  179. return {
  180. solution: btoa(String.fromCharCode.apply(null, uInt8View.slice(0, 4))),
  181. };
  182. }
  183. }
  184. }
  185. function generateUserID(length = 36) {
  186. const charset =
  187. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  188. let result = "";
  189. if (crypto && crypto.getRandomValues) {
  190. const values = new Uint32Array(length);
  191. crypto.getRandomValues(values);
  192. for (let i = 0; i < length; i++) {
  193. result += charset[values[i] % charset.length];
  194. }
  195. return result;
  196. } else {
  197. for (let i = 0; i < length; i++) {
  198. result += charset[Math.floor(Math.random() * charset.length)];
  199. }
  200. return result;
  201. }
  202. }