-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from matushorvath/trigger-bot-update
Trigger bot update from browser
- Loading branch information
Showing
5 changed files
with
718 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.