Change selected text content with a chrome extension

I’m trying to edit selected text using content_sript.js in my chrome extension. The highlighted text should only cover one node in the html but I haven’t figured out how to enforce that yet

This is the code in content_scripts.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.action === "changeText") { console.log(window.getSelection().toString()); console.log(window.getSelection()); window.getSelection().deleteFromDocument(); const new_text = "Text i need to insert"; sendResponse({worked:true}); } });
I have figured out how to delete the selected text but now I need to somehow insert the text back in.