gpt4 book ai didi

javascript - Gmail 上的 JS 文本选择

转载 作者:行者123 更新时间:2023-11-30 18:48:04 24 4
gpt4 key购买 nike

我正在构建一个小书签,它获取选定的文本并将其发送回我的服务器进行处理。它适用于除 Gmail 之外的所有站点。任何人都知道如何让它在 Gmail 上运行。这是我正在使用的代码:

var selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection();
} else if (document.getSelection) {
selectedText = document.getSelection();
} else if (document.selection) {
selectedText = document.selection.createRange().text;
} else {
selectedText = document.activeElement.contentWindow.getSelection();
};

最佳答案

我遇到了同样的问题,发现你的问题是我自己在寻找答案。

据我所知,您的代码中的问题不是 window.getSelection 在 gmail 中未定义,而是 getSelection().toString() 返回零长度字符串,尽管已选择文本。在 Firefox 中,Tim Down 的解决方案对我有效,但在 Chrome 中无效,因为 contentWindow 不可用。

我修改后的代码遍历页面上的任何框架,在 Firefox、Chrome 和 Safari 中对我来说在 Gmail 中工作。 (我没有在其他浏览器中测试过)。

var selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection().toString();
}
if (selectedText == '') {
var frames = window.frames;
for (var i = 0; i < frames.length; i++) {
if (selectedText == '') {
selectedText = frames[i].document.getSelection().toString();
}
else { break; }
}
}

关于javascript - Gmail 上的 JS 文本选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4818882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com