Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:03:01+00:00 2026-06-11T10:03:01+00:00

I am writing a script to insert presaved html content into Gmail’s editable iframe

  • 0

I am writing a script to insert presaved html content into Gmail’s editable iframe in the compose page mentioned below. This script is only to be used in Greasemonkey on Firefox. So I don’t need to consider any other browser.

Currently it inserts the text once and then gets bugged. I guess because range.setStart() expects first parameter is a node, which createContextualFragment() does not return.

Is there any other way to add html at current cursor’s position and then move cursor to the end of the added html (not to the end of all content)?

https://mail.google.com/mail/?view=cm&fs=1&tf=1&source=mailto&to=example@example.com

function insertTextAtCursor(text) {
    var sel, range, html, textNode;
    if (window.frames[3].window.frames[0].getSelection()) {
        sel = window.frames[3].window.frames[0].getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();
            textNode = range.createContextualFragment(text);
            //textNode = document.createTextNode(text);
            range.insertNode( textNode );
            range.setStart(textNode, textNode.length);
            range.setEnd(textNode, textNode.length);
            sel.removeAllRanges();
            sel.addRange(range);
            window.frames[3].window.frames[0].focus();
        }
    }
}

Update 1:
If i comment the code to move the cursor after inserting html then its no longer bugged, but the cursor stays in the same place.

//range.setStart(textNode, textNode.length);
//range.setEnd(textNode, textNode.length);
sel.removeAllRanges();
//sel.addRange(range);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:03:02+00:00Added an answer on June 11, 2026 at 10:03 am

    got you something.

    with the help from this answer, i wrote this:

    1. paste your text/html at the cursor position
    2. move the cursor to the end of the text area

    save this as test.html to test it locally

    <html>
    
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script>
        jQuery.fn.extend({
            insertAtCaret: function(myValue){
              return this.each(function(i) {
                if (document.selection) {
                  //For browsers like Internet Explorer
                  this.focus();
                  sel = document.selection.createRange();
                  sel.text = myValue;
                  this.focus();
                }
                else if (this.selectionStart || this.selectionStart == '0') {
                  //For browsers like Firefox and Webkit based
                  var startPos = this.selectionStart;
                  var endPos = this.selectionEnd;
                  var scrollTop = this.scrollTop;
                  this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
                  this.focus();
                  this.selectionStart = startPos + myValue.length;
                  this.selectionEnd = startPos + myValue.length;
                  this.scrollTop = scrollTop;
                } 
                else {
                  this.value += myValue;
                  this.focus();
                }
              })
            }
        });
    
        var myhtml = '<b> this html will be <i>added</i> to the </b> cursor position (and will move to the end)';
        var movetotheendof = '';
        $(window).load(function(){
            $('#btnpastepre').click(function() {
                $('#txtarea').insertAtCaret(myhtml)
                movetotheendof = $('#txtarea').val()
                $('#txtarea').val("").val(movetotheendof)
            })
        });
        </script>
    </head>
    
    <body>
    
        <div id="formcontainer">
            <button id="btnpastepre">click to paste</button>
    
            <form id="formulario">
                <textarea id="txtarea" cols=60 rows=20></textarea>
            </form>
        </div>
    
    </body>
    
    </html>
    

    or click here to test it online: http://jsfiddle.net/RASG/Vwwm4/

    Now all you have to do is change it according to your needs (gmail or any other site)

    EDIT

    I forgot that you wanted a GM script 🙂
    Here it is

    // ==UserScript==
    // @name        TESTE 3
    // @namespace   TESTE 3
    // @description TESTE 3
    // @require     http://code.jquery.com/jquery.min.js
    // @include     *
    // @version     1
    // ==/UserScript==
    
    
    jQuery.fn.extend({
        insertAtCaret: function(myValue){
            return this.each(function(i) {
                if (document.selection) {
                    //For browsers like Internet Explorer
                    this.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                    this.focus();
                }
                else if (this.selectionStart || this.selectionStart == '0') {
                    //For browsers like Firefox and Webkit based
                    var startPos = this.selectionStart;
                    var endPos = this.selectionEnd;
                    var scrollTop = this.scrollTop;
                    this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
                    this.focus();
                    this.selectionStart = startPos + myValue.length;
                    this.selectionEnd = startPos + myValue.length;
                    this.scrollTop = scrollTop;
                } 
                else {
                    this.value += myValue;
                    this.focus();
                }
            })
        }
    });
    
    var myhtml = '<b> this html will be <i>added</i> to the </b> cursor position (and will move to the end)';
    var movetotheendof = '';
    
    $(window).load(function(){
        $('#btnpastepre').click(function() {
            $('#txtarea').insertAtCaret(myhtml)
            movetotheendof = $('#txtarea').val()
            $('#txtarea').val("").val(movetotheendof)
        })
    })
    

    Just create an html file with this content to test it

    <html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <div id="formcontainer">
            <button id="btnpastepre">click to paste</button>
            <form id="formulario">
                <textarea id="txtarea" cols=60 rows=20></textarea>
            </form>
        </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script to output Google Analytics API data and insert it into
I'm writing a script that is trying to insert a directory name into a
I am writing a script to convert a picture into MIDI notes based on
I'm writing a script to parse some text files, and insert the data that
I am currently writing a script that intelligently strips a database down into a
I'm writing a PHP script which inserts rows into a SQLite database. The code
I am writing a user script for youtube.com, and want to insert a button
I'm writing a Python script that reads tweets and inserts them into MySQL. Depending
I'm writing a bulk insert script using Django's ORM + custom raw SQL. The
I am writing a web app using HTML and Wicket. In my HTML page,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.