I am having a lot of trouble implementing copy and paste in to my electron app. I have made a copy and paste window ui but have been looking for a way to implement the copy and paste feature (That only copies highlighted text). I have been at this for a LONG time and tried to use the clipboard API from electron themselves but that does not copy highlighted text only text that you have added youself. I also tryed to make a button that would just run thr CTRL + C command but i could not get it to work, So any help would be muchly appreciated!
Thanks in advance!
If you just want to get selected text from your own app window, you can use the web API Window.getSelection()
normally like you would do in any web application that runs in the browser
See this answer
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
If you want a context menu (on right-click), you can use Electron Menu
APIs to build this context menu with a copy menu item or you can use an npm package that does this for you electron-context-menu
If you want to get any highlighted text form any application, you can use synthesize a CTRL+C
click then get the text from the clipboard. I have created an npm package electron-selected-text
that does this for you and it preservers the old text in the clipboard as well