给你一些东西。
在这个答案的帮助下,我写了这个:
- 将您的文本/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>
或点击这里在线测试:http: //jsfiddle.net/RASG/Vwwm4/
现在您所要做的就是根据您的需要更改它(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)
})
})
只需创建一个包含此内容的 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>