修改 javascript 代码,以便获取 iframe 中当前选定的文本

发布于 2024-12-10 01:08:05 字数 1180 浏览 4 评论 0原文

我有一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-12-17 01:08:05

您应该引用 iframe 文档而不是当前的主 文档,从那里您可以确定实际选择的文本。

function getIFrameDocument(iframe) {
    var doc;
    if (iframe.contentDocument) {
        doc = iframe.contentDocument;
    } else if (iframe.contentWindow) {
        doc = iframe.contentWindow.document;
    } else if (iframe.document) {
        doc = iframe.document;
    } else {
        doc = window.frames[iframe.id].document;
    }

    return doc;
}

// try to get main document selection
var selection = document.getSelection();
if (!selection) {
    selection = getIFrameDocument(document.getElementById('your-iframe-id')).getSelection();
}

请注意,这尚未经过测试,但这个想法是有道理的。

you should reference the iframe document instead of the current main document, from there you can determine which text is actually selected.

function getIFrameDocument(iframe) {
    var doc;
    if (iframe.contentDocument) {
        doc = iframe.contentDocument;
    } else if (iframe.contentWindow) {
        doc = iframe.contentWindow.document;
    } else if (iframe.document) {
        doc = iframe.document;
    } else {
        doc = window.frames[iframe.id].document;
    }

    return doc;
}

// try to get main document selection
var selection = document.getSelection();
if (!selection) {
    selection = getIFrameDocument(document.getElementById('your-iframe-id')).getSelection();
}

note that this is untested but the idea makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文