diff --git a/README.md b/README.md index f829d09..3a8bca7 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,9 @@ 1. Go to any IDE. 2. Refresh the site. (Yes... I will fix this) +## Buttons + +- `[copy stderr]` - copies the entire stderr output from the program +- `[copy stdin]` - copies only lines prefixed with `?>` and removes this prefix. This allows you to write program input on stderr, retrieve it, and run identical game locally. Prefix can be changed in [contentscript.js](copy-extension/contentscript.js), but requires reinstalling the extension. + +Note: if the string to copy is empty, nothing will hapen after clicking a button (no message). \ No newline at end of file diff --git a/copy-extension/contentscript.js b/copy-extension/contentscript.js index 9043610..8b2d0a8 100644 --- a/copy-extension/contentscript.js +++ b/copy-extension/contentscript.js @@ -1,3 +1,9 @@ +const stdinPrefix = "?>" + +// todo make buttons next to each other +// todo more versions?: ALL; STDIN; DEBUG; FULL JSON + + const copyToClipboardAsync = str => { if (navigator && navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(str); @@ -20,7 +26,7 @@ loading = setInterval(function () { return; } - const title = document.getElementsByClassName("cg-ide-title"); + const title = document.getElementsByClassName("league-value"); // league-value // cg-ide-title if (!title || title.length == 0) { console.log('Game title not loaded!'); @@ -28,10 +34,13 @@ loading = setInterval(function () { } - let button = document.createElement("Button"); - let text = document.createTextNode("Copy stderr"); + let buttonStderr = document.createElement("Button"); + let textStderr = document.createTextNode("[copy stderr]"); + copyNodeStyle(title[0], buttonStderr); - copyNodeStyle(title[0], button); + let buttonStdin = document.createElement("Button"); + let textStdin = document.createTextNode("[copy stdin]"); + copyNodeStyle(title[0], buttonStdin); const rank = document.getElementsByClassName("rank-wrapper"); @@ -42,11 +51,45 @@ loading = setInterval(function () { // TODO: Sometimes rank could not be loaded const rankStyle = window.getComputedStyle(rank[0]); - button.style.webkitTextFillColor = rankStyle.webkitTextFillColor; + buttonStderr.style.webkitTextFillColor = "rgb(146, 150, 155)" //rankStyle.webkitTextFillColor; + buttonStdin.style.webkitTextFillColor = "rgb(255, 255, 255)" //rankStyle.webkitTextFillColor; + } + + buttonStderr.onclick = function (event) { + console.log("Button stderr clicked!"); + + let elements = document.getElementsByClassName('stderr'); + + if (elements.length == 0) { + return; + } + + let contentStderr = ""; + let linesStderr = 0; + + for (let turn = 0; turn < elements.length; turn++) { + let element = elements[turn]; + /* + element.innerText sometimes have new lines symbols + and sometimes doesn't :/ + */ + + // content += elements[turn].innerText + "\n"; + + for (let line of element.getElementsByClassName("outputLine")) { + contentStderr += line.innerText + "\n"; + linesStderr += 1; + } + } + + if (contentStderr.length > 0) { + copyToClipboardAsync(contentStderr); + alert(`Copied stderr ${linesStderr} lines.`); + } } - button.onclick = function (event) { - console.log("Button clicked!"); + buttonStdin.onclick = function (event) { + console.log("Button stdin clicked!"); let elements = document.getElementsByClassName('stderr'); @@ -54,8 +97,8 @@ loading = setInterval(function () { return; } - let content = ""; - let lines = 0; + let contentStdin = ""; + let linesStdin = 0; for (let turn = 0; turn < elements.length; turn++) { let element = elements[turn]; @@ -67,20 +110,24 @@ loading = setInterval(function () { // content += elements[turn].innerText + "\n"; for (let line of element.getElementsByClassName("outputLine")) { - content += line.innerText + "\n"; - lines += 1; + if (line.innerText.startsWith(stdinPrefix)) { + contentStdin += line.innerText.substring(2) + "\n"; + linesStdin += 1; + } } } - if (content.length > 0) { - copyToClipboardAsync(content); - alert(`Copied ${lines} lines.`); + if (contentStdin.length > 0) { + copyToClipboardAsync(contentStdin); + alert(`Copied stdin ${linesStdin} lines.`); } } - button.appendChild(text); + buttonStderr.appendChild(textStderr); + header[0].appendChild(buttonStderr); - header[0].appendChild(button); + buttonStdin.appendChild(textStdin); + header[0].appendChild(buttonStdin); // header[0] console.log('Button is added!');