OLD | NEW |
1 <html> | 1 <html> |
2 <div id='textDiv'>Some text we can select</div> | 2 <div id='textDiv'>Some text we can select</div> |
3 <input id='textfield' type="text" value="Text in a textfield"> | 3 <input id='textfield' type="text" value="Text in a textfield"> |
4 <script> | 4 <script> |
5 | 5 |
| 6 function select_all_text() { |
| 7 var div = document.getElementById("textDiv"); |
| 8 var range = document.createRange(); |
| 9 range.selectNodeContents(div); |
| 10 var sel = window.getSelection(); |
| 11 sel.removeAllRanges(); |
| 12 sel.addRange(range); |
| 13 } |
| 14 |
| 15 function get_selection() { |
| 16 return (window.getSelection() + ""); |
| 17 } |
| 18 |
6 function focus_textfield() { | 19 function focus_textfield() { |
7 document.getElementById("textfield").focus(); | 20 document.getElementById("textfield").focus(); |
8 // Focusing the textfiled selects its text. Collapse selection to a cursor. | 21 // Focusing the textfiled selects its text. Collapse selection to a cursor. |
9 window.getSelection().collapseToStart(); | 22 window.getSelection().collapseToStart(); |
10 } | 23 } |
11 | 24 |
12 function get_point_inside(element) { | 25 function get_cursor_position() { |
13 var rect = element.getBoundingClientRect(); | 26 var div = document.getElementById("textfield"); |
14 var point = { | 27 var start = div.selectionStart; |
15 x: rect.left + 8, | 28 var end = div.selectionEnd; |
16 y: (rect.top + rect.bottom) / 2 | 29 if (start == end) |
17 }; | 30 return start; |
18 window.domAutomationController.send(JSON.stringify(point)); | 31 else |
19 } | 32 return -1; |
20 | |
21 function get_point_inside_text() { | |
22 get_point_inside(document.getElementById('textDiv')); | |
23 } | |
24 | |
25 function get_point_inside_textfield() { | |
26 get_point_inside(document.getElementById('textfield')); | |
27 } | 33 } |
28 | 34 |
29 </script> | 35 </script> |
30 | 36 |
31 </html> | 37 </html> |
OLD | NEW |