selectionAction_frame_xorigin.html (1423B)
1 <html> 2 <head> 3 <meta charset="utf-8" /> 4 <script> 5 window.addEventListener("message", e => { 6 switch (e.data.type) { 7 case "focus": 8 window.focus(); 9 break; 10 11 case "select": { 12 <!-- On fission, there's no way to wait for parent position change. --> 13 <!-- So we use requestAnimationFrame as a workaround. --> 14 window.requestAnimationFrame(() => { 15 window.requestAnimationFrame(() => { 16 const text = document.body.firstChild; 17 document 18 .getSelection() 19 .setBaseAndExtent(text, 0, text, e.data.length); 20 }); 21 }); 22 break; 23 } 24 25 case "selectedOffset": { 26 const sel = document.getSelection(); 27 const text = document.body.firstChild; 28 if (sel.anchorNode !== text || sel.focusNode !== text) { 29 window.parent.postMessage([-1, -1], "*"); 30 } else { 31 window.parent.postMessage( 32 [sel.anchorOffset, sel.focusOffset], 33 "*" 34 ); 35 } 36 break; 37 } 38 39 case "content": 40 window.parent.postMessage(document.body.textContent, "*"); 41 break; 42 } 43 }); 44 </script> 45 </head> 46 <body onload="document.body.textContent = 'elit'"></body> 47 </html>