first commit

This commit is contained in:
Vyn 2024-08-04 21:00:15 +02:00
commit 8a543a4a5a
13 changed files with 9621 additions and 0 deletions

26
extension/manifest.json Normal file
View file

@ -0,0 +1,26 @@
{
"manifest_version": 2,
"name": "Codingame-ide-sync",
"version": "1.0",
"description": "Merge all your code from your local dev environment in 1 file and copy it automatically into the codingame IDE",
"icons": {},
"content_scripts": [
{
"matches": [
"https://www.codingame.com/ide/puzzle/*",
"https://www.codingame.com/ide/challenge/*"
],
"js": ["utils.js", "sync.js"]
}
],
"permissions": [
"<all_urls>",
"tabs",
"activeTab"
]
}

64
extension/sync.js Normal file
View file

@ -0,0 +1,64 @@
document.body.style.border = "1px solid red"; // Just to see if it's active
const PORT = "8080"
let textInfo = null
async function lifeCheck() {
const res = await fetch("http://localhost:${PORT}/ping")
return res.status === 0
}
async function fetchCode() {
try {
const res = await fetch(`http://localhost:${PORT}/code`)
if (res.status !== 200) {
log("Fetching code failed")
return null
}
const code = await res.text();
return code;
} catch (e) {
log("Fetching code failed")
return null
}
}
const updateCode = async (e) => {
debug("Fetching code")
const code = await fetchCode()
if (!code) {
textInfo.textContent = `[CG-Sync] Update failed`
return
}
textInfo.textContent = `[CG-Sync] Last update : ${new Date().toLocaleTimeString()}`
let data = {status: "updateCode", code}
data = cloneInto(data, window);
const ev = new CustomEvent('ExternalEditorToIDE', { detail: data });
window.document.dispatchEvent(ev);
}
window.document.addEventListener('focus', updateCode);
log("Script started")
// We wait before injecting cg-sync state elements
setTimeout(async () => {
log("Loading elements in page")
textInfo = document.createElement("button")
textInfo.textContent = `[CG-Sync] Waiting for first sync...`
textInfo.style = "color: rgb(0, 255, 255)"
textInfo.onclick = () => updateCode()
const actionRowDocument = window.document.getElementsByClassName("code-header")
actionRowDocument[0].children[0].after(textInfo)
updateCode()
}, 3000)

7
extension/utils.js Normal file
View file

@ -0,0 +1,7 @@
function log(data) {
console.log(`[cg-sync] ${data}`)
}
function debug(data) {
console.debug(`[cg-sync] ${data}`)
}