2

I am trying to get the carret position inside an iframe whose <body> has contentEditable = true. I am running a content script from a chrome addon and binding the action like so:

getCaretPosition = function(element) {
    var range = parent.$("iframe.html").getCursorPosition();
    alert(range);
    return caretOffset;
}

I found the following code for getCursorPosition in a similar stack overflow answer:

(function ($, undefined) {
$.fn.getCursorPosition = function() {
    var el = $(this).get(0);
    var pos = 0;
    if('selectionStart' in el) {
        pos = el.selectionStart;
    } else if('selection' in document) {
        el.focus();
        var Sel = document.selection.createRange();
        var SelLength = document.selection.createRange().text.length;
        Sel.moveStart('character', -el.value.length);
        pos = Sel.text.length - SelLength;
    }
    return pos;
}
})(jQuery);

But it is always returning a position of 0.

bottleboot
  • 1,659
  • 2
  • 25
  • 41
Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
  • Within a Chrome extension, `parent` and references to other `window` objects are always `undefined`. I think that this answer covers everything: http://stackoverflow.com/questions/13266192/accessing-iframe-from-chrome-extension/13271692#13271692 – Rob W Dec 21 '12 at 09:23

0 Answers0