0

I'm trying to run the following bookmarklet from This article on Firefox and Chrome on Android, but it doesn't work. It works on Windows Firefox, though.

alert(getSelectionText()));

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

On Android, it will just show an empty alert message. I assume I overlooked some conceptual key difference? Any suggestion on how to fix it?

Florian Straub
  • 826
  • 9
  • 18
  • What is `getgetSelectionText`? – Makyen Aug 12 '19 at 03:29
  • A copy and paste mistake ;) – Florian Straub Aug 12 '19 at 15:36
  • OK, but `alert(getSelectionText);` doesn't actually execute `getSelectionText`. That should show you the `getSelectionText` function as a string. What are you expecting it to do? Did you mean `alert(getSelectionText());`? – Makyen Aug 12 '19 at 18:55
  • Fixed, please excuse sleepy me – Florian Straub Aug 13 '19 at 19:56
  • I haven't tried it in the exact circumstances you are working in, but I've generally found the code in the snippet within [this answer to Get the Highlighted/Selected text](https://stackoverflow.com/a/5379408/3773011) to be effective. I have also found the less complex code which you copied to not work under many circumstances. – Makyen Aug 13 '19 at 20:01
  • That's exactly what I meant: works good on basically every Windows browser, but doesn't work on Android :( – Florian Straub Aug 15 '19 at 12:55
  • Did you try the code I was referring to (the code in the snippet; i.e. the second code block), or did you keep using the code that I said doesn't work that well (i.e. the code in the first code block in that answer, which you have in this question, which is known not to work for some selections in Desktop Firefox)? – Makyen Aug 15 '19 at 15:06
  • Yes, I also tried plain ``window.getSelection()`` and it didn't work either – Florian Straub Aug 17 '19 at 09:12

1 Answers1

0

Chrome Android doesn't let you run Bookmarklets the traditional way, But, there is a workaround, you can call Bookmarklet from the address bar,

First, you have to manually create a Bookmarklet by editing it,

For example, javascript:alert('foo'); and name it.

Then, on the page you waant to run this, search for the bookmarklet name in the address bar, and click on it.

Bookmarklet on Android

Below Tutorial might be helpful.

https://paul.kinlan.me/use-bookmarklets-on-chrome-on-android/

  • 1
    It has been some time already, but in my perception, the simple execution of bookmarklets on Android was not the problem, but using this particular function.Thanks anyway – Florian Straub Jan 13 '21 at 15:55