在光标位置插入HTML,并使用javascript将光标移动到插入HTML的末尾

在光标位置插入HTML,并使用javascript将光标移动到插入HTML的末尾,javascript,iframe,greasemonkey,Javascript,Iframe,Greasemonkey,我正在编写一个脚本,将预先保存的html内容插入下面提到的撰写页面中Gmail的可编辑iframe中。此脚本仅用于Firefox上的Greasemonkey。所以我不需要考虑其他浏览器。 目前,它插入文本一次,然后被窃听。我猜是因为range.setStart()希望第一个参数是一个节点,createContextalFragment()不会返回该节点 是否有其他方法可以在当前光标位置添加html,然后将光标移动到添加的html的末尾(而不是所有内容的末尾) 更新1: 如果我在插入html后对

我正在编写一个脚本,将预先保存的html内容插入下面提到的撰写页面中Gmail的可编辑iframe中。此脚本仅用于Firefox上的Greasemonkey。所以我不需要考虑其他浏览器。

目前,它插入文本一次,然后被窃听。我猜是因为range.setStart()希望第一个参数是一个节点,createContextalFragment()不会返回该节点

是否有其他方法可以在当前光标位置添加html,然后将光标移动到添加的html的末尾(而不是所有内容的末尾)

更新1: 如果我在插入html后对代码进行注释以移动光标,那么它就不再有错误,但光标仍保持在原来的位置

//range.setStart(textNode, textNode.length);
//range.setEnd(textNode, textNode.length);
sel.removeAllRanges();
//sel.addRange(range);
给你拿了点东西

在他的帮助下,我写了以下内容:

  • 将文本/html粘贴到光标位置
  • 将光标移动到文本区域的末尾
  • 将此保存为test.html以在本地对其进行测试

    <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>
    
    
    jQuery.fn.extend({
    InsertCart:函数(myValue){
    返回此。每个函数(i){
    if(文档选择){
    //适用于Internet Explorer等浏览器
    这是focus();
    sel=document.selection.createRange();
    sel.text=myValue;
    这是focus();
    }
    else if(this.selectionStart | | this.selectionStart==“0”){
    //适用于基于Firefox和Webkit的浏览器
    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);
    这是focus();
    this.selectionStart=startPos+myValue.length;
    this.selectionEnd=startPos+myValue.length;
    this.scrollTop=scrollTop;
    } 
    否则{
    this.value+=myValue;
    这是focus();
    }
    })
    }
    });
    var myhtml='此html将添加到光标位置(并将移动到末尾)';
    var movetotheendof='';
    $(窗口)。加载(函数(){
    $('#btnpastepre')。单击(函数(){
    $('#txtarea').insertcaret(myhtml)
    movetotheendof=$('#txtarea').val()
    $('#txtarea').val(“”.val(movetotheendof)
    })
    });
    点击粘贴
    
    或单击此处在线测试:

    现在你所要做的就是根据你的需要改变它(gmail或任何其他网站)

    编辑 我忘了你想要一个GM脚本:)
    给你

    // ==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)
        })
    })
    
    /==UserScript==
    //@name TESTE 3
    //@TESTE 3
    //@description TESTE 3
    //@需要http://code.jquery.com/jquery.min.js
    //@包括*
    //@version 1
    //==/UserScript==
    jQuery.fn.extend({
    InsertCart:函数(myValue){
    返回此。每个函数(i){
    if(文档选择){
    //适用于Internet Explorer等浏览器
    这是focus();
    sel=document.selection.createRange();
    sel.text=myValue;
    这是focus();
    }
    else if(this.selectionStart | | this.selectionStart==“0”){
    //适用于基于Firefox和Webkit的浏览器
    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);
    这是focus();
    this.selectionStart=startPos+myValue.length;
    this.selectionEnd=startPos+myValue.length;
    this.scrollTop=scrollTop;
    } 
    否则{
    this.value+=myValue;
    这是focus();
    }
    })
    }
    });
    var myhtml='此html将添加到光标位置(并将移动到末尾)';
    var movetotheendof='';
    $(窗口)。加载(函数(){
    $('#btnpastepre')。单击(函数(){
    $('#txtarea').insertcaret(myhtml)
    movetotheendof=$('#txtarea').val()
    $('#txtarea').val(“”.val(movetotheendof)
    })
    })
    
    只需使用此内容创建一个html文件来测试它

    <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>
    
    
    点击粘贴
    
    给你带了点东西

    在他的帮助下,我写了以下内容:

  • 将文本/html粘贴到光标位置
  • 将光标移动到文本区域的末尾
  • 将此保存为test.html以在本地对其进行测试

    <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>
    
    
    jQuery.fn.extend({
    InsertCart:函数(myValue){
    返回此。每个函数(i){
    if(文档选择){
    //适用于Internet Explorer等浏览器
    这是focus();
    sel=document.selection.createRange();
    sel.text=myValue;
    这是focus();
    }
    else if(this.selectionStart | | this.selectionStart==“0”){
    //适用于基于Firefox和Webkit的浏览器
    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);
    这是focus();
    this.selectionStart=startPos+myValue.length;
    this.selectionEnd=startPos+myValue.length;
    this.scrollTop=scrollTop;
    } 
    否则{
    this.value+=myValue;
    这是focus();
    }
    })
    }
    });
    var myhtml='此html将添加到