I am creating a JS bookmarklet that allows me to write down the text that the user selected in his browser and sends it to the web application. I have currently tested several tutorials and has a script that looks like this:
javascript:var t;try {t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));}catch(e){t="";}alert(t);
To make this a little easier, the code here is more readable:
javascript: var t; try { t=((window.getSelection&&window.getSelection()) || (document.getSelection&&document.getSelection()) || (document.selection&&document.selection.createRange&&document.selection.createRange().text)); }catch(e) { t=""; } alert(t);
In the current code, I have selected text and alerts so that I can see what was captured. However, formatting the text is very important to me, so I would like HTML to be in this text too. In fact, I think it would be better to develop where the user selected on the page, and look from the very beginning and back from the end of the selected content to find the closest HTML tags, and then capture what's inside that. (since the user cannot determine whether they selected HTML or not)
I'm much more used to working with jQuery to do DOM selections, so it's a little over my head for now. (If you cannot use jQuery with a bookmarklet ... can you?)
Can anyone help me with this? (Even just pointing me in the right direction would be great, I do it as a hobby educational project, so I'm glad to think about it myself.)
source share