ryd.background.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. await register();
  62. return;
  63. }
  64. fetch(`${apiUrl}/interact/vote`, {
  65. method: "POST",
  66. headers: {
  67. "Content-Type": "application/json",
  68. },
  69. body: JSON.stringify({
  70. userId: storageResult.userId,
  71. videoId,
  72. value: vote,
  73. }),
  74. })
  75. .then(async (response) => {
  76. if (response.status == 401) {
  77. await register();
  78. await sendVote(videoId, vote);
  79. return;
  80. }
  81. return response.json()
  82. })
  83. .then((response) => {
  84. solvePuzzle(response).then((solvedPuzzle) => {
  85. fetch(`${apiUrl}/interact/confirmVote`, {
  86. method: "POST",
  87. headers: {
  88. "Content-Type": "application/json",
  89. },
  90. body: JSON.stringify({
  91. ...solvedPuzzle,
  92. userId: storageResult.userId,
  93. videoId,
  94. }),
  95. });
  96. });
  97. });
  98. });
  99. }
  100. function register() {
  101. let userId = generateUserID();
  102. api.storage.sync.set({ userId });
  103. return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, {
  104. method: "GET",
  105. headers: {
  106. Accept: "application/json",
  107. },
  108. })
  109. .then((response) => response.json())
  110. .then((response) => {
  111. return solvePuzzle(response).then((solvedPuzzle) => {
  112. return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, {
  113. method: "POST",
  114. headers: {
  115. "Content-Type": "application/json",
  116. },
  117. body: JSON.stringify(solvedPuzzle),
  118. }).then((response) =>
  119. response.json().then((result) => {
  120. if (result === true) {
  121. return api.storage.sync.set({ registrationConfirmed: true });
  122. }
  123. })
  124. );
  125. });
  126. })
  127. .catch();
  128. }
  129. api.storage.sync.get(null, (res) => {
  130. if (!res || !res.userId || !res.registrationConfirmed) {
  131. register();
  132. }
  133. });
  134. const sentIds = new Set();
  135. let toSend = [];
  136. function sendUserSubmittedStatisticsToApi(statistics) {
  137. fetch(`${apiUrl}/votes/user-submitted`, {
  138. method: "POST",
  139. headers: {
  140. "Content-Type": "application/json",
  141. },
  142. body: JSON.stringify(statistics),
  143. });
  144. }
  145. function countLeadingZeroes(uInt8View, limit) {
  146. let zeroes = 0;
  147. let value = 0;
  148. for (let i = 0; i < uInt8View.length; i++) {
  149. value = uInt8View[i];
  150. if (value === 0) {
  151. zeroes += 8;
  152. } else {
  153. let count = 1;
  154. if (value >>> 4 === 0) {
  155. count += 4;
  156. value <<= 4;
  157. }
  158. if (value >>> 6 === 0) {
  159. count += 2;
  160. value <<= 2;
  161. }
  162. zeroes += count - (value >>> 7);
  163. break;
  164. }
  165. if (zeroes >= limit) {
  166. break;
  167. }
  168. }
  169. return zeroes;
  170. }
  171. async function solvePuzzle(puzzle) {
  172. let challenge = Uint8Array.from(atob(puzzle.challenge), (c) =>
  173. c.charCodeAt(0)
  174. );
  175. let buffer = new ArrayBuffer(20);
  176. let uInt8View = new Uint8Array(buffer);
  177. let uInt32View = new Uint32Array(buffer);
  178. let maxCount = Math.pow(2, puzzle.difficulty) * 5;
  179. for (let i = 4; i < 20; i++) {
  180. uInt8View[i] = challenge[i - 4];
  181. }
  182. for (let i = 0; i < maxCount; i++) {
  183. uInt32View[0] = i;
  184. let hash = await crypto.subtle.digest("SHA-512", buffer);
  185. let hashUint8 = new Uint8Array(hash);
  186. if (countLeadingZeroes(hashUint8) >= puzzle.difficulty) {
  187. return {
  188. solution: btoa(String.fromCharCode.apply(null, uInt8View.slice(0, 4))),
  189. };
  190. }
  191. }
  192. }
  193. function generateUserID(length = 36) {
  194. const charset =
  195. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  196. let result = "";
  197. if (crypto && crypto.getRandomValues) {
  198. const values = new Uint32Array(length);
  199. crypto.getRandomValues(values);
  200. for (let i = 0; i < length; i++) {
  201. result += charset[values[i] % charset.length];
  202. }
  203. return result;
  204. } else {
  205. for (let i = 0; i < length; i++) {
  206. result += charset[Math.floor(Math.random() * charset.length)];
  207. }
  208. return result;
  209. }
  210. }