diff options
Diffstat (limited to 'Extensions/combined/ryd.background.js')
-rw-r--r-- | Extensions/combined/ryd.background.js | 132 |
1 files changed, 62 insertions, 70 deletions
diff --git a/Extensions/combined/ryd.background.js b/Extensions/combined/ryd.background.js index 9e749cf..75f22ae 100644 --- a/Extensions/combined/ryd.background.js +++ b/Extensions/combined/ryd.background.js @@ -78,12 +78,13 @@ api.runtime.onInstalled.addListener((details) => { // No need to show changelog if its was a browser update (and not extension update) details.reason === "browser_update" || // No need to show changelog if developer just reloaded the extension - (details.reason === "update" && - details.previousVersion === chrome.runtime.getManifest().version) + details.reason === "update" ) return; - api.tabs.create({url: api.runtime.getURL("/changelog/3/changelog_3.0.html")}); -}) + api.tabs.create({ + url: api.runtime.getURL("/changelog/3/changelog_3.0.html"), + }); +}); // api.storage.sync.get(['lastShowChangelogVersion'], (details) => { // if (extConfig.showUpdatePopup === true && @@ -96,14 +97,12 @@ api.runtime.onInstalled.addListener((details) => { // } // }); - async function sendVote(videoId, vote) { api.storage.sync.get(null, async (storageResult) => { if (!storageResult.userId || !storageResult.registrationConfirmed) { await register(); - return; } - fetch(`${apiUrl}/interact/vote`, { + let voteResponse = await fetch(`${apiUrl}/interact/vote`, { method: "POST", headers: { "Content-Type": "application/json", @@ -113,82 +112,72 @@ async function sendVote(videoId, vote) { videoId, value: vote, }), - }) - .then(async (response) => { - if (response.status == 401) { - await register(); - await sendVote(videoId, vote); - return; - } - return response.json(); - }) - .then((response) => { - solvePuzzle(response).then((solvedPuzzle) => { - fetch(`${apiUrl}/interact/confirmVote`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - ...solvedPuzzle, - userId: storageResult.userId, - videoId, - }), - }); - }); - }); + }); + + if (voteResponse.status == 401) { + await register(); + await sendVote(videoId, vote); + return; + } + const voteResponseJson = await voteResponse.json(); + const solvedPuzzle = await solvePuzzle(voteResponseJson); + if (!solvedPuzzle.solution) { + await sendVote(videoId, vote); + return; + } + + await fetch(`${apiUrl}/interact/confirmVote`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + ...solvedPuzzle, + userId: storageResult.userId, + videoId, + }), + }); }); } -function register() { - let userId = generateUserID(); +async function register() { + const userId = generateUserID(); api.storage.sync.set({ userId }); - return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, { - method: "GET", + const registrationResponse = await fetch( + `${apiUrl}/puzzle/registration?userId=${userId}`, + { + method: "GET", + headers: { + Accept: "application/json", + }, + } + ).then((response) => response.json()); + const solvedPuzzle = await solvePuzzle(registrationResponse); + if (!solvedPuzzle.solution) { + await register(); + return; + } + const result = await fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, { + method: "POST", headers: { - Accept: "application/json", + "Content-Type": "application/json", }, - }) - .then((response) => response.json()) - .then((response) => { - return solvePuzzle(response).then((solvedPuzzle) => { - return fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(solvedPuzzle), - }).then((response) => - response.json().then((result) => { - if (result === true) { - return api.storage.sync.set({ registrationConfirmed: true }); - } - }) - ); - }); - }) - .catch(); + body: JSON.stringify(solvedPuzzle), + }).then((response) => response.json()); + if (result === true) { + return api.storage.sync.set({ registrationConfirmed: true }); + } } -api.storage.sync.get(null, (res) => { +api.storage.sync.get(null, async (res) => { if (!res || !res.userId || !res.registrationConfirmed) { - register(); + await register(); } }); const sentIds = new Set(); let toSend = []; -function sendUserSubmittedStatisticsToApi(statistics) { - fetch(`${apiUrl}/votes/user-submitted`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(statistics), - }); -} - function countLeadingZeroes(uInt8View, limit) { let zeroes = 0; let value = 0; @@ -223,7 +212,7 @@ async function solvePuzzle(puzzle) { let buffer = new ArrayBuffer(20); let uInt8View = new Uint8Array(buffer); let uInt32View = new Uint32Array(buffer); - let maxCount = Math.pow(2, puzzle.difficulty) * 5; + let maxCount = Math.pow(2, puzzle.difficulty) * 3; for (let i = 4; i < 20; i++) { uInt8View[i] = challenge[i - 4]; } @@ -238,6 +227,7 @@ async function solvePuzzle(puzzle) { }; } } + return {}; } function generateUserID(length = 36) { @@ -283,7 +273,9 @@ function storageChangeHandler(changes, area) { handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue); } if (changes.numberDisplayReformatLikes !== undefined) { - handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue); + handleNumberDisplayReformatLikesChangeEvent( + changes.numberDisplayReformatLikes.newValue + ); } } |