Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Javascript to get selected text in Mobile Safari

So I'm working on a bookmarklet where it would be ideal for me to grab the content selected by the user using the "loop". Both window.getSelection and document.getSelection are functions that I can call, however, they always return an empty string.

I believe the problem is that when you tap on the bookmark icon in Mobile Safari, the selection is released. For example, if you select some text, tap the "+", bookmark or other tab, the selection is unselected even if you cancel.

Any thoughts on if it is possible to get at this data at all? Or is this pretty much impossible?

like image 370
Nirmal Avatar asked Jun 29 '10 03:06

Nirmal


2 Answers

I think you would have to have the bookmarklet insert some content into the page that would operate on the selection. You might add a button to the top or bottom of the page, and when clicked it would act on the current selection. It could then clean up the added content or leave it there.

like image 67
drawnonward Avatar answered Nov 14 '22 06:11

drawnonward


The contents of the "loop" are not exposed to javascript in the mobile browser, period. So this is impossible (I am assuming that you are working in the full browser, not in the browser window created when you launch a "saved to home page" icon)

like image 30
Michael Mullany Avatar answered Nov 14 '22 06:11

Michael Mullany