Getting the currently selected text in a chrome searchbar (input field)

I’m currently working on a chrome extension that injects a js script into a visited website. It gets the currently selected text (highlighted by cursor), then after running re-focuses on that text. My saveSelection and restoreSelection functions are as follows:

saveSelection = function(containerEl) {
    let range = window.getSelection()
    let preSelectionRange = range.cloneRange();
    preSelectionRange.setEnd(range.startContainer, range.startOffset);
    let start = preSelectionRange.toString()
    return {start: start,
        end: start + range.toString()

restoreSelection = function(containerEl, savedSel)
    let charIndex = 0,
        range = document.createRange();
    range.setStart(containerEl, 0);
    let nodeStack = [containerEl],
        node, foundStart = false,
        stop = false;
    while (!stop && (node = nodeStack.pop()))
        if (node.nodeType === 3)
            let nextCharIndex = charIndex + node.length;
            if (!foundStart && savedSel.start >= charIndex && savedSel.start <= nextCharIndex)
                range.setStart(node, savedSel.start - charIndex);
                foundStart = true;
            if (foundStart && savedSel.end >= charIndex && savedSel.end <= nextCharIndex)
                range.setEnd(node, savedSel.end - charIndex);
                stop = true;
            charIndex = nextCharIndex;
            let i = node.childNodes.length;
            while (i--)
    let sel = window.getSelection();

These work for text within a paragraph, but for text within input fields (such as the google searchbar, codepen, jsfiddle) the script is not able to re-focus on the text. In debugging, I found that the range startOffset and endOffset = 0.

How can I 1) get the selection range, and 2) re-focus on the text in the searchbar?

How to give a browser extension all text data input into that browser?

I’m experimenting with Chrome browser extensions. I want to be able to take any text input into the browser (through the URL bar of the browser or onto text boxes on social media sites and so on) and send that text to a server. I’m wondering if this is possible in chrome and what functions I need to use to achieve this?

I suspect the logic of the extension would work something like this: any time a link is clicked where there is text in an input form / text box, extract that text or form input and send it using some JS requests library to a server.

Which Chrome API function can be used to get the text input into a form?