'use strict';

/**
 * Insert the text at the cursor into the active element. Note that we're
 * intentionally not appending or simply replacing the value of the editable
 * field, as we want to allow normal pasting conventions. If you paste at the
 * beginning, you shouldn't see all your text be replaced.
 * Taken from:
 * http://stackoverflow.com/questions/7404366/how-do-i-insert-some-text-where-the-cursor-is
 */
function insertTextAtCursor(text) {
    var el = document.activeElement;
    var val = el.value;
    var startIndex;
    var endIndex;
    var range;
    var doc = el.ownerDocument;
    var sel;
    if (typeof el.selectionStart === 'number' &&
        typeof el.selectionEnd === 'number') {
        startIndex = el.selectionStart;
        endIndex = el.selectionEnd;
        el.value = val.slice(0, startIndex) + text + val.slice(endIndex);
        // standard behavior is move to the end of the selection
        el.selectionEnd = startIndex + text.length;
    } else if (doc.selection !== undefined && doc.selection.createRange) {
        el.focus();
        range = doc.selection.createRange();
        range.collapse(false);
        range.text = text;
        range.select();
    } else if (el.tagName === 'DIV') {
        // We're pasting into an editable div, like gmail.
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                range = sel.getRangeAt(0);
                range.deleteContents();
                var pastedText = document.createTextNode(text);
                range.insertNode(pastedText);
                range.setStartAfter(pastedText);
                sel.removeAllRanges();
                sel.addRange(range);
            } else {
                console.log('getRangeAt or rangeCount not defined');
            }
        } else if (document.selection && document.selection.createRange) {
            document.selection.createRange().text = text;
        }
    } else {
        // We aren't imitating pasting for this tag yet.
        console.log('Not handling paste for tag type: ' + el.tagName);
    }
}

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.data) {
        insertTextAtCursor(request.data);
    }
});


@@ -698,24 +698,54 @@
 'number') %7B%0A
+        // input and textarea%0A
         star
@@ -1003,230 +1003,8 @@
 th;%0A
-    %7D else if (doc.selection !== undefined && doc.selection.createRange) %7B%0A        el.focus();%0A        range = doc.selection.createRange();%0A        range.collapse(false);%0A        range.text = text;%0A        range.select();%0A
     

remove unnecessary IE support