Skip to content

Commit

Permalink
Merge pull request #7 from matushorvath/trigger-bot-update
Browse files Browse the repository at this point in the history
Trigger bot update from browser
  • Loading branch information
TrePe0 authored Nov 17, 2023
2 parents 825790b + 4fee1ac commit a089a64
Show file tree
Hide file tree
Showing 5 changed files with 718 additions and 340 deletions.
107 changes: 73 additions & 34 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,84 @@
const aocBotUrl = 'https://7b79gj2si4.execute-api.eu-central-1.amazonaws.com/Prod/start';
const aocBotUrl = 'https://7b79gj2si4.execute-api.eu-central-1.amazonaws.com/Prod';

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

browserOrChrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
'use strict';

if (changeInfo.status == 'complete' && tab.url) {
const matches = tab.url.match(/\.com\/([0-9]{4})\/day\/([0-9]+)/);
const timestamp = Math.floor(Date.now() / 1000);
if (matches !== null) {
const [, year, day] = matches;
const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_user_name' });
if (error) return;
let { aoc_times: times } = await browserOrChrome.storage.sync.get(['aoc_times']);
if (times === undefined) times = {};
if (times[year] === undefined) times[year] = {};
if (times[year][day] === undefined) times[year][day] = {};
if (times[year][day][part] === undefined) {
if (userName) {
const body = {
version: 1,
year: Number(year),
day: Number(day),
part: Number(part),
name: userName
};
const response = await fetch(aocBotUrl, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
console.log(response);
}

times[year][day][part] = timestamp;
await browserOrChrome.storage.sync.set({ aoc_times: times });
console.log('User:', userName);
console.log(times);
const matches = tab.url.match(/\.com\/([0-9]{4})\/day\/([0-9]+)(\/answer)?/);
//const matches = tab.url.match(/file:\/\/\//) ? [, 2022, 3, '/answer'] : undefined;
if (matches) {
const [, year, day, answer] = matches;
if (!answer) {
await onProblemUpdated(tab, year, day);
} else {
await onAnswerUpdated(tab, year, day);
}
}
}
});

const onProblemUpdated = async (tab, year, day) => {
'use strict';

const timestamp = Math.floor(Date.now() / 1000);

const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_start_data' });
if (error) return;

let { aoc_times: times } = await browserOrChrome.storage.sync.get(['aoc_times']);

if (userName && part && times?.[year]?.[day]?.[part] === undefined) {
const body = {
version: 1,
year: Number(year),
day: Number(day),
part: Number(part),
name: userName
};
const response = await fetch(`${aocBotUrl}/start`, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
console.log(response);

if (times === undefined) times = {};
if (times[year] === undefined) times[year] = {};
if (times[year][day] === undefined) times[year][day] = {};
times[year][day][part] = timestamp;

await browserOrChrome.storage.sync.set({ aoc_times: times });
console.log('User:', userName);
console.log(times);
}
};

const onAnswerUpdated = async (tab, year, day) => {
'use strict';

const { userName, part, error } = await browserOrChrome.tabs.sendMessage(tab.id, { text: 'get_stop_data' });
if (error) return;

if (userName && part) {
const body = {
version: 1,
year: Number(year),
day: Number(day),
part: Number(part),
name: userName
};
const response = await fetch(`${aocBotUrl}/stop`, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
console.log(response);
}
};
66 changes: 54 additions & 12 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
'use strict';
if (msg.text === 'get_user_name') {
const el = document.querySelector('header .user');
const error = (document.getElementsByClassName('day-desc').length === 0);
if (el === null) sendResponse({ userName: '', part: 1, error: error });
else {
const i = el.innerHTML.indexOf('<');
sendResponse({
userName: (i <= 0 ? el.innerHTML : el.innerHTML.substring(0, i)),
part: (document.getElementById('part2') !== null) + 1,
error: error
});
}

if (msg.text === 'get_start_data') {
onGetStartData(sendResponse);
} else if (msg.text === 'get_stop_data') {
onGetStopData(sendResponse);
}
});

const onGetStartData = (sendResponse) => {
'use strict';

const error = (document.getElementsByClassName('day-desc').length === 0);
if (error) {
sendResponse({ error: true });
return;
}

const userName = getUserName();
if (!userName) {
sendResponse({ error: true });
return;
}

const part = (document.getElementById('part2') === null ? 1 : 2);
sendResponse({ userName, part });
};

const onGetStopData = (sendResponse) => {
'use strict';

const error = (document.getElementsByClassName('day-success').length === 0);
if (error) {
sendResponse({ error: true });
return;
}

const userName = getUserName();
if (!userName) {
sendResponse({ error: true });
return;
}

const part = (document.getElementsByClassName('share').length === 0 ? 1 : 2);
sendResponse({ userName, part });
};

const getUserName = () => {
'use strict';

const element = document.querySelector('header .user');
if (!element) {
return undefined;
}

return element.firstChild.textContent.trim();
};
2 changes: 1 addition & 1 deletion manifest.input.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"common": {
"name": "AoC Time Logger",
"version": "2.1.10",
"version": "3.0.0",
"description": "Logs time when user opens the task",
"content_scripts": [{
"matches": ["https://adventofcode.com/*"],
Expand Down
Loading

0 comments on commit a089a64

Please sign in to comment.