Martin2
(Martin)
April 5, 2021, 12:20pm
1
I added “clipboardRead” to permissions to my extension, but can’t find right way to use it. My javascript file:
browser.browserAction.onClicked.addListener(function() {
// Returns: false
//var item = “Google ” + String(document.execCommand(“copy”));
// Returns: true
//let item = “Google ” + document.execCommand(“paste”);
// Returns: [object Promise]
let item = “Google ” + navigator.clipboard.readText();
// Returns nothing
//let item = “Google ” + await this.getClipboardText();
var creating = browser.tabs.create({
url: item
});
creating.then(onCreated, onError);
});
The easiest way to get the text contents of the clipboard without using HTML elements is
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText
Martin2
(Martin)
April 5, 2021, 5:32pm
3
Thank you! Got code partially working. Can use only URLs from clipboard. Now need to find a way to compare if clipboard contains URL or some other text string and that case to start google search.
Current code
navigator.clipboard.readText().then(
clipText => browser.tabs.create({
url:clipText
})
)
You could use the URL
constructor to detect strings that aren’t valid URLs, since it throws in that case:
https://developer.mozilla.org/en-US/docs/Web/API/URL/URL