Added clipboard methods to the script API

This commit is contained in:
Fabio Manganiello 2020-07-15 00:00:39 +02:00
parent f7a8aa17a8
commit fb8acdda40
2 changed files with 17 additions and 0 deletions

View File

@ -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(`<p>${text}</p>`);
// Anything returned from the function will be returned to the called
return 42;
},

View File

@ -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({