|
1 |
| -const aocBotUrl = 'https://7b79gj2si4.execute-api.eu-central-1.amazonaws.com/Prod/start'; |
| 1 | +const aocBotUrl = 'https://7b79gj2si4.execute-api.eu-central-1.amazonaws.com/Prod'; |
2 | 2 |
|
3 | 3 | // With manifest V2 and Firefox, chrome.* APIs don't return promises, but browser.* APIs do.
|
4 | 4 | const browserOrChrome = typeof browser !== "undefined" ? browser : chrome;
|
5 | 5 |
|
6 | 6 | browserOrChrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
7 | 7 | 'use strict';
|
| 8 | + |
8 | 9 | if (changeInfo.status == 'complete' && tab.url) {
|
9 |
| - const matches = tab.url.match(/\.com\/([0-9]{4})\/day\/([0-9]+)/); |
10 |
| - const timestamp = Math.floor(Date.now() / 1000); |
11 |
| - if (matches !== null) { |
12 |
| - const [, year, day] = matches; |
13 |
| - const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_user_name' }); |
14 |
| - if (error) return; |
15 |
| - let { aoc_times: times } = await browserOrChrome.storage.sync.get(['aoc_times']); |
16 |
| - if (times === undefined) times = {}; |
17 |
| - if (times[year] === undefined) times[year] = {}; |
18 |
| - if (times[year][day] === undefined) times[year][day] = {}; |
19 |
| - if (times[year][day][part] === undefined) { |
20 |
| - if (userName) { |
21 |
| - const body = { |
22 |
| - version: 1, |
23 |
| - year: Number(year), |
24 |
| - day: Number(day), |
25 |
| - part: Number(part), |
26 |
| - name: userName |
27 |
| - }; |
28 |
| - const response = await fetch(aocBotUrl, { |
29 |
| - method: 'post', |
30 |
| - headers: { |
31 |
| - 'Content-Type': 'application/json' |
32 |
| - }, |
33 |
| - body: JSON.stringify(body) |
34 |
| - }); |
35 |
| - console.log(response); |
36 |
| - } |
37 |
| - |
38 |
| - times[year][day][part] = timestamp; |
39 |
| - await browserOrChrome.storage.sync.set({ aoc_times: times }); |
40 |
| - console.log('User:', userName); |
41 |
| - console.log(times); |
| 10 | + const matches = tab.url.match(/\.com\/([0-9]{4})\/day\/([0-9]+)(\/answer)?/); |
| 11 | + //const matches = tab.url.match(/file:\/\/\//) ? [, 2022, 3, '/answer'] : undefined; |
| 12 | + if (matches) { |
| 13 | + const [, year, day, answer] = matches; |
| 14 | + if (!answer) { |
| 15 | + await onProblemUpdated(tab, year, day); |
| 16 | + } else { |
| 17 | + await onAnswerUpdated(tab, year, day); |
42 | 18 | }
|
43 | 19 | }
|
44 | 20 | }
|
45 | 21 | });
|
| 22 | + |
| 23 | +const onProblemUpdated = async (tab, year, day) => { |
| 24 | + 'use strict'; |
| 25 | + |
| 26 | + const timestamp = Math.floor(Date.now() / 1000); |
| 27 | + |
| 28 | + const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_start_data' }); |
| 29 | + if (error) return; |
| 30 | + |
| 31 | + let { aoc_times: times } = await browserOrChrome.storage.sync.get(['aoc_times']); |
| 32 | + |
| 33 | + if (userName && part && times?.[year]?.[day]?.[part] === undefined) { |
| 34 | + const body = { |
| 35 | + version: 1, |
| 36 | + year: Number(year), |
| 37 | + day: Number(day), |
| 38 | + part: Number(part), |
| 39 | + name: userName |
| 40 | + }; |
| 41 | + const response = await fetch(`${aocBotUrl}/start`, { |
| 42 | + method: 'post', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/json' |
| 45 | + }, |
| 46 | + body: JSON.stringify(body) |
| 47 | + }); |
| 48 | + console.log(response); |
| 49 | + |
| 50 | + if (times === undefined) times = {}; |
| 51 | + if (times[year] === undefined) times[year] = {}; |
| 52 | + if (times[year][day] === undefined) times[year][day] = {}; |
| 53 | + times[year][day][part] = timestamp; |
| 54 | + |
| 55 | + await browserOrChrome.storage.sync.set({ aoc_times: times }); |
| 56 | + console.log('User:', userName); |
| 57 | + console.log(times); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +const onAnswerUpdated = async (tab, year, day) => { |
| 62 | + 'use strict'; |
| 63 | + |
| 64 | + const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_stop_data' }); |
| 65 | + if (error) return; |
| 66 | + |
| 67 | + if (userName && part) { |
| 68 | + const body = { |
| 69 | + version: 1, |
| 70 | + year: Number(year), |
| 71 | + day: Number(day), |
| 72 | + part: Number(part), |
| 73 | + name: userName |
| 74 | + }; |
| 75 | + const response = await fetch(`${aocBotUrl}/stop`, { |
| 76 | + method: 'post', |
| 77 | + headers: { |
| 78 | + 'Content-Type': 'application/json' |
| 79 | + }, |
| 80 | + body: JSON.stringify(body) |
| 81 | + }); |
| 82 | + console.log(response); |
| 83 | + } |
| 84 | +}; |
0 commit comments