Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
79 changes: 63 additions & 16 deletions copy-extension/contentscript.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -20,18 +26,21 @@ 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!');
return;
}


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");

Expand All @@ -42,20 +51,54 @@ 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');

if (elements.length == 0) {
return;
}

let content = "";
let lines = 0;
let contentStdin = "";
let linesStdin = 0;

for (let turn = 0; turn < elements.length; turn++) {
let element = elements[turn];
Expand All @@ -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!');

Expand Down