Is a there a way where I can copy content from browser clipboard using Puppeteer in nodejs. I am trying to copy content post the page is rendered. This is being achieved by the bellow code but unable to get the content.
await page.keyboard.down('ControlLeft');
await page.keyboard.press('KeyA');
await page.keyboard.up('ControlLeft');
await page.keyboard.down('ControlLeft');
await page.keyboard.press('KeyC');
await page.keyboard.up('ControlLeft');
Copy from input box
You can evaluate the following snippet to copy data from any input element.
function copyText(selector) {
var copyText = document.querySelector(selector);
copyText.select();
document.execCommand("Copy");
return copyText.value;
}
Usage:
const result = await page.evaluate(() => {
function copyText(selector) {
var copyText = document.querySelector(selector);
copyText.select();
document.execCommand("Copy");
return copyText.value;
}
return copyText("#foo");
});
now result should contain the text copied from the input box.
Copy anything into clipboard
You can evaluate the snippet from this answer.
const result = await page.evaluate(() => {
function copy(text) {
var input = document.createElement('input');
input.setAttribute('value', text);
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input)
}
return copy(document.body.innerHTML); // copy whatever want to copy
});
It will create a input element, set your provided text as value and then copy the data from that element, finally remove it after usage.