修改 javascript 代码,以便获取 iframe 中当前选定的文本
我有一个基于 javascript 的 Google Chrome 扩展程序,它将网页中当前选定的文本作为输入,然后将该文本翻译成英语。原始文本被相同位置的翻译文本替换...
我有一个要求,某些网站将显示在 iframe 中,对于这些网站,我也想翻译 iframe 中当前选定的文本。
下面给出了扩展开头的源代码,它在其中获取当前选定的文本 -
var selection = document.getSelection();
var range = selection.getRangeAt(0);
如何更改上述代码/添加一些代码,以便变量“选择”可以将当前选定的文本存储在 main 中窗口,还是 iframe 中当前选定的文本?此外,即使要翻译的文本位于 iframe 内,也应正确初始化变量“范围”...
此外,翻译完成后,原始语言文本将被网页中同一位置的翻译文本替换,变量“replacement”存储翻译后的文本 -
var holder = document.createElement('span');
holder.innerHTML = replacement;
var child = holder.firstChild;
var ref = child.cloneNode(true);
// ... now insert into document in place of the original content:
range.deleteContents();
range.insertNode(ref);
while (child = child.nextSibling) {
ref.parentNode.insertBefore(child.cloneNode(true), ref.nextSibling);
ref = ref.nextSibling;
}
请注意,此问题中显示的第一个代码部分给出了“range”初始化的代码...
我如何修改上述代码以便创建新的 span 元素在 iframe 内?我的理解是,要做到这一点,关键部分是代码
var holder = document.createElement('span');
变量'holder'应该能够在iframe中创建span元素...
我该如何修改这段代码?
I have a javascript based Google Chrome Extension that takes as input the currently selected text in the web page, and then translates that text into English. The original text is replaced with the translated text at same position...
I have a requirement where some sites will be shown in an iframe, and for those sites also I want to translate the currently selected text within the iframe.
The source code at the beginning of the extension, where it obtains the currently selected text, is given below--
var selection = document.getSelection();
var range = selection.getRangeAt(0);
How do I change the above code/add some code so that the variable 'selection' can store either the currently selected text in main window, or the currently selected text within an iframe? Also the variable 'range' should be initialised correctly even if the text to be translated is within an iframe...
Also, once the translation is done, the original language text is replaced by translated text at same place in web page, the variable 'replacement' stores the translated text--
var holder = document.createElement('span');
holder.innerHTML = replacement;
var child = holder.firstChild;
var ref = child.cloneNode(true);
// ... now insert into document in place of the original content:
range.deleteContents();
range.insertNode(ref);
while (child = child.nextSibling) {
ref.parentNode.insertBefore(child.cloneNode(true), ref.nextSibling);
ref = ref.nextSibling;
}
Note that the code for initialisation of 'range' is given in the first code section shown in this question...
How do I go about modifying the above code so that the new span element is created within the iframe? What I understood is that to do this, the key part is the code
var holder = document.createElement('span');
The variable 'holder' should be able to create the span element in an iframe also...
How do I modify this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该引用
iframe
文档而不是当前的主文档
,从那里您可以确定实际选择的文本。请注意,这尚未经过测试,但这个想法是有道理的。
you should reference the
iframe
document instead of the current maindocument
, from there you can determine which text is actually selected.note that this is untested but the idea makes sense.