【发布时间】:2018-05-14 21:31:20
【问题描述】:
我正在编写一个 chrome 扩展程序,我想在 tech.io 的代码部分中选取选定的文本,例如:https://prnt.sc/hhfkwuhttps://tech.io/playgrounds/347/javascript-promises-mastering-the-asynchronous/what-is-asynchronous-in-javascript
我有这个功能在任何其他网站上几乎都可以使用,但在这里无法正常工作,它返回空字符串或一些奇怪的 unicode 字符:
function getSelectedText() {
var text = "";
var activeEl = document.activeElement;
var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if (
(activeElTagName == "textarea") || (activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
(typeof activeEl.selectionStart == "number")
) {
text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
} else if (window.getSelection) {
text = window.getSelection().toString();
}
return text;
}
我应该如何更改它才能在 tech.io 网站上的这个特定代码部分工作?
【问题讨论】:
标签: javascript html dom textselection