在gmail撰写邮件区域中获取选中/突出显示的文本html [英] get selected/highlighted text html in gmail compose mail area
本文介绍了在gmail撰写邮件区域中获取选中/突出显示的文本html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为Gmail设计一个Chrome扩展程序。在这我想获得选定/突出显示的文字。我尝试了以下代码:
I am designing an chrome extension for Gmail. In this I want to get selected/highlighted text. I tried following code:
if (!window.x) {
x = {};
}
x.Selector = {};
x.Selector.getSelected = function() {
var t = '';
if($('.compose-container').getSelection){
t = $('.compose-container').getSelection();
alert(t);
} else if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
它不会在撰写邮件中给我选定的文本。
请帮助我。
It is not giving me selected text in compose mail. Please help me out.
推荐答案
根据gRenzFries的回答,我的代码与他提供的链接相同。但是在代码中添加了一些细节以便将其粘贴到文本框中。
As per gRenzFries answer, I code same as link provided by him. But slight addition in code to paste it in textbox.
复制文本的代码: $ b
Code to Copy text :
var emailLink = document.querySelector('.gmail_default');
var range = document.createRange();
range.selectNode(emailLink);
window.getSelection().addRange(range);
try {
// Now that we've selected the anchor text, execute the copy command
var successful = document.execCommand('copy', true);
} catch(err) {
}
将其粘贴到文本框中的代码:
Code to paste it in textbox:
$('#text-to-display').val(""); //reset textbox value
$('#text-to-display').focus(); //set focus to textbox
document.execCommand("Paste");
此代码正常工作。
这篇关于在gmail撰写邮件区域中获取选中/突出显示的文本html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文