Skip to content

Commit a089a64

Browse files
authored
Merge pull request #7 from matushorvath/trigger-bot-update
Trigger bot update from browser
2 parents 825790b + 4fee1ac commit a089a64

File tree

5 files changed

+718
-340
lines changed

5 files changed

+718
-340
lines changed

background.js

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,84 @@
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';
22

33
// With manifest V2 and Firefox, chrome.* APIs don't return promises, but browser.* APIs do.
44
const browserOrChrome = typeof browser !== "undefined" ? browser : chrome;
55

66
browserOrChrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
77
'use strict';
8+
89
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);
4218
}
4319
}
4420
}
4521
});
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+
};

content.js

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
22
'use strict';
3-
if (msg.text === 'get_user_name') {
4-
const el = document.querySelector('header .user');
5-
const error = (document.getElementsByClassName('day-desc').length === 0);
6-
if (el === null) sendResponse({ userName: '', part: 1, error: error });
7-
else {
8-
const i = el.innerHTML.indexOf('<');
9-
sendResponse({
10-
userName: (i <= 0 ? el.innerHTML : el.innerHTML.substring(0, i)),
11-
part: (document.getElementById('part2') !== null) + 1,
12-
error: error
13-
});
14-
}
3+
4+
if (msg.text === 'get_start_data') {
5+
onGetStartData(sendResponse);
6+
} else if (msg.text === 'get_stop_data') {
7+
onGetStopData(sendResponse);
158
}
169
});
10+
11+
const onGetStartData = (sendResponse) => {
12+
'use strict';
13+
14+
const error = (document.getElementsByClassName('day-desc').length === 0);
15+
if (error) {
16+
sendResponse({ error: true });
17+
return;
18+
}
19+
20+
const userName = getUserName();
21+
if (!userName) {
22+
sendResponse({ error: true });
23+
return;
24+
}
25+
26+
const part = (document.getElementById('part2') === null ? 1 : 2);
27+
sendResponse({ userName, part });
28+
};
29+
30+
const onGetStopData = (sendResponse) => {
31+
'use strict';
32+
33+
const error = (document.getElementsByClassName('day-success').length === 0);
34+
if (error) {
35+
sendResponse({ error: true });
36+
return;
37+
}
38+
39+
const userName = getUserName();
40+
if (!userName) {
41+
sendResponse({ error: true });
42+
return;
43+
}
44+
45+
const part = (document.getElementsByClassName('share').length === 0 ? 1 : 2);
46+
sendResponse({ userName, part });
47+
};
48+
49+
const getUserName = () => {
50+
'use strict';
51+
52+
const element = document.querySelector('header .user');
53+
if (!element) {
54+
return undefined;
55+
}
56+
57+
return element.firstChild.textContent.trim();
58+
};

manifest.input.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"common": {
33
"name": "AoC Time Logger",
4-
"version": "2.1.10",
4+
"version": "3.0.0",
55
"description": "Logs time when user opens the task",
66
"content_scripts": [{
77
"matches": ["https://adventofcode.com/*"],

0 commit comments

Comments
 (0)