diff --git a/examples/example.js b/examples/example.js index 3485b88..6fb871f 100644 --- a/examples/example.js +++ b/examples/example.js @@ -92,6 +92,11 @@ export default { console.log('Selected element (DOM)', targetDOM); } + // Read and set the clipboard with getClipboard/setClipboard + const text = await app.getClipboard(); + console.log('Text in clipboard', text); + await app.setClipboard(`

${text}

`); + // Anything returned from the function will be returned to the called return 42; }, diff --git a/src/script.js b/src/script.js index 202cdd9..5aada7b 100644 --- a/src/script.js +++ b/src/script.js @@ -36,6 +36,18 @@ export default { }); }, + getClipboard: () => { + return new Promise((resolve, reject) => { + navigator.clipboard.readText().then(text => resolve(text), error => reject(error)); + }); + }, + + setClipboard: (text) => { + return new Promise((resolve, reject) => { + navigator.clipboard.writeText(text).then(() => resolve(), error => reject(error)); + }); + }, + openTab: (url) => { const port = browser.runtime.connect({ name: 'url' }); port.postMessage({