'Javascript: format text and paste as richtext
I am writing a Chrome extension, and I need to be able to format a string in the clipboard using navigator.clipboard.writeText so that it can be pasted (using Ctrl+V) into a Google Doc or elsewhere as formatted text (bold, or as a hyperlink).
I have:
function bolder() {
navigator.clipboard.writeText(String("text to bold").bold()).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: bolder
});
});
However, when I paste the text, it just pastes as HTML formatted text: i.e. <b>text to bold</b>
I get why this works this way, but I can't find a way to format things on the clipboard so they can be pasted as formatted text in Word/Google Docs the way copying/pasting text works on the Windows clipboard.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
