Javascript Chrome Extention get caret coordinates

187 views Asked by At

I just started with a new chrome extension however I couldn't find how to get the exact caret (text cursor) position in JavaScript

code

chrome.commands.onCommand.addListener(function (command) {
    console.log('Command:', command);
    if (command == "OpenRightclick-menu") {
        // Get coordinates
    }
});
1

There are 1 answers

0
Sergii Rudenko On

You can get access to current cursor position from your content script.

If you have an DOM node of element, you can check selectionStart and selectionEnd properties.

If you don't have DOM node - you can check answer on this question.