From 81ded4fb78cf32a3d85882708e2f651608100815 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 18 Mar 2016 13:03:10 -0700 Subject: WIP --- content.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/content.js b/content.js index 119fce55..e3ac0998 100644 --- a/content.js +++ b/content.js @@ -20,32 +20,27 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -function insertBreakAtPoint(e) { - var range; - var textNode; - var offset; +function getText(e, length) { + let range = document.caretRangeFromPoint(e.clientX, e.clientY); - if (document.caretPositionFromPoint) { - range = document.caretPositionFromPoint(e.clientX, e.clientY); - textNode = range.offsetNode; - offset = range.offset; - } else if (document.caretRangeFromPoint) { - range = document.caretRangeFromPoint(e.clientX, e.clientY); - textNode = range.startContainer; - offset = range.startOffset; + let node = range.startContainer; + if (node.nodeType !== 3 /* TEXT_NODE */) { + return ''; } - // only split TEXT_NODEs - if (textNode.nodeType == 3) { - var replacement = textNode.splitText(offset); - var br = document.createElement('br'); - textNode.parentNode.insertBefore(br, replacement); - } + let scan = Math.min(- range.startOffset, length); + console.log(scan); + range.setEnd(node, range.startOffset + scan); + + return range.toString(); } -function onMouseDown(e) { - insertBreakAtPoint(e); +function onMouseMove(e) { + const text = getText(e); + if (text !== '') { + console.log(text); + } } -window.addEventListener('mousedown', onMouseDown, false); +window.addEventListener('mousemove', onMouseMove, false); -- cgit v1.2.3