Skip to content

Commit

Permalink
add copy button to tasks_print
Browse files Browse the repository at this point in the history
  • Loading branch information
conlacda committed Dec 29, 2024
1 parent 932d433 commit a964235
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {test, expect} from '../fixtures';
import {Page, Locator} from "@playwright/test";

require('dotenv').config({path: './.env'});

test.describe.configure({mode: 'serial'});

let page: Page;

test.beforeAll(async ({context}) => {
page = await context.newPage();
await page.goto('https://atcoder.jp/contests/abc386/tasks_print');
});

test.afterAll(async () => {
await page.close();
});

test('Check copy buttons are added', async () => {
const copyButtons = page.locator('span.btn-copy');
await expect(copyButtons).toHaveCount(52);
});

test('Check clipboard after copying', async () => {
const copyButtons = page.locator('span.btn-copy');
// First button
await copyButtons.nth(0).click();
let copiedText = await page.evaluate(() => navigator.clipboard.readText());
expect(copiedText.replace(/\r\n/g, '')).toBe('7 7 7 1');

// 28th button
await copyButtons.nth(28).click();
copiedText = await page.evaluate(() => navigator.clipboard.readText());
expect(copiedText.replace(/\r/g, '')).toBe('4 3\n' +
'4 1 B\n' +
'3 2 W\n' +
'1 3 B\n');

// Last button
await copyButtons.nth(51).click();
copiedText = await page.evaluate(() => navigator.clipboard.readText());
expect(copiedText.replace(/\r\n/g, '')).toBe('707081320');
});
8 changes: 8 additions & 0 deletions browser-extension/chrome-extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@
],
"run_at": "document_idle",
"world": "MAIN"
},
{
"js": [
"scripts/tasks_print/main.js"
],
"matches": [
"*://atcoder.jp/contests/*/tasks_print"
]
}
]
}
37 changes: 37 additions & 0 deletions browser-extension/chrome-extension/scripts/tasks_print/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const isVisible = (element) => {
return element.offsetWidth > 0 &&
element.offsetHeight > 0 &&
window.getComputedStyle(element).visibility !== 'hidden'
&& window.getComputedStyle(element).display !== 'none';
}

let index = 0;
while (true) {
const taskStatement = document.getElementById('task-statement');
if (taskStatement) {
taskStatement.id = `task-statement-${index}`;
index++;
} else break;
}

for (let i = 0; i < index; i++) {
const taskStatement = document.getElementById(`task-statement-${i}`);
const divsAfterIODiv = taskStatement.querySelectorAll('div.io-style ~ div.part');
const ioDivs = Array.from(divsAfterIODiv).filter(isVisible);

for (const div of ioDivs) {
const button = (new DOMParser()).parseFromString(`<span class="btn btn-default btn-sm btn-copy ml-1" tabindex="0">Copy</span>`, "text/html").body.firstChild;
var h3Element = div.querySelector('h3');
h3Element.insertAdjacentElement('beforeend', button);
const pre = h3Element.nextElementSibling;
button.addEventListener('click', function () {
copyToClipboard(pre.textContent).then(() => {
button.textContent = 'Copied';
setTimeout(() => {
button.textContent = 'Copy';
button.blur();
}, 500);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const copyButton = $('span[data-toggle="tooltip"]:visible').first();
if (allTestCasesSz === 0)
return;

const downloadButton = (new DOMParser()).parseFromString(`<button class="btn btn-default btn-sm" id="dltc" title="Just click once to download.">Download all test cases (${humanReadable(allTestCasesSz)})</button>`, "text/html").body.children[0];
const downloadButton = (new DOMParser()).parseFromString(`<button class="btn btn-default btn-sm" id="dltc" title="Just click once to download.">Download all test cases (${humanReadable(allTestCasesSz)})</button>`, "text/html").body.firstChild;
document.querySelector('span.h2').appendChild(downloadButton);
downloadButton.onclick = async () => {
downloadButton.disabled = true;
Expand Down

0 comments on commit a964235

Please sign in to comment.