Bookmarklet copy & paste that preserves formatting

Discussion in 'Javascript' started by Amir Michail, Jun 5, 2007.

  1. Amir  Michail

    Amir Michail Guest

    Hi,

    I would like to add a submission bookmarklet to my app that takes the
    current selection and pastes it into the app's rich text editor while
    preserving formatting. It should work in at least Firefox & IE.

    Any pointers on how this could be done? Are there any examples that I
    can look at?

    Amir
     
    Amir Michail, Jun 5, 2007
    #1
    1. Advertisements


  2. use
    getSelection().getRangeAt(0).cloneContents();
    to clone the contents of current selection then append it to your rte.

    if you want the HTML then append the cloned contents to an element
    (e.g. div) and use innerHTML property to get the HTML
     
    scripts.contact, Jun 6, 2007
    #2
    1. Advertisements

  3. Amir  Michail

    -Lost Guest

    OK, since *you* brought it up. ;)

    Can you explain what the hell a range is for?

    Why would you need to addRange?
     
    -Lost, Jun 7, 2007
    #3
  4. Amir  Michail

    -Lost Guest

    I have thought on this some, and think I may have an example of its usage.

    Let's say you provide the user with the ability to click multiple
    paragraphs and do something with them.

    You would need to addRange on a per-paragraph basis, right?

    Jeez, I gotta' stop replying to myself.
     
    -Lost, Jun 7, 2007
    #4
  5. use
    var c=getSelection().getRangeAt(0).cloneContents();
    method to clone the contents of current selection then append it
    anywhere using appendChild(c).

    If you wand to get the HTML of selection then just append the content
    in a DIV and use innerHTML property
     
    scripts.contact, Jun 8, 2007
    #5
  6. Amir  Michail

    Amir Michail Guest

    I still don't understand the mechanism for copying the selection to
    a RichTextArea widget in a gwt app via a bookmarklet.

    Clicking on a bookmarklet normally redirects to an app url along with
    some additional parameters added.

    So maybe I need to get the selection as html first and add that html
    to the
    end of the url? Or is there some other way?

    Amir
     
    Amir Michail, Jun 8, 2007
    #6
  7. try this :
    <script>
    function copyCont(){
    var ifr=document.getElementById("s");

    ifr.contentDocument.body.appendChild(getSelection().getRangeAt(0).cloneContents())
    }
    </script>
    <iframe id="s" onload="this.contentDocument.designMode='on';"></
    iframe>
    <div>content<b> content</b> <i> content</i></div>
    <button onclick="copyCont()">Copy contents of selection in IFRAME</
    button>



    what is the URL ?
     
    scripts.contact, Jun 8, 2007
    #7
  8. Amir  Michail

    Amir Michail Guest

    The gwt app may not be open when the user clicks on the bookmarklet.

    Moreover, if the bookmarklet opens it and tries to access the rich
    text editor therein, won't that be a security violation?
    This is for my Study Stickies gwt app:

    http://studystickies.com

    Currently, the bookmarklets look like this (but they do not preserve
    formatting in selections):

    Add Resource
    ============

    javascript:t=document.title;u=document.location.href;s=(document.getSelection)?
    document.getSelection():document.selection.createRange().text;location.href='http://
    studystickies.com#?cmd=add_resource&title=' + encodeURIComponent(t)
    +'&url='+encodeURIComponent(u)+'&selection='+encodeURIComponent(s);

    Add Sticky
    ==========

    javascript:t=document.title;u=document.location.href;s=(document.getSelection)?
    document.getSelection():document.selection.createRange().text;location.href='http://
    studystickies.com#?cmd=add_sticky&title=' + encodeURIComponent(t)
    +'&url='+encodeURIComponent(u)+'&selection='+encodeURIComponent(s);

    Amir
     
    Amir Michail, Jun 8, 2007
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.