Javascript 返回单击的单词出现在其中的句子

Javascript 返回单击的单词出现在其中的句子,javascript,nlp,Javascript,Nlp,上一个问题的后续行动: 这个问题我已经摸索了一段时间了。然而,我今天早上醒来,开始读到: 瞧!分支允许用户选择一个句子,然后共享它,保存它,等等…这正是我想要做的。看起来他们正在用标签包装每个句子 以前,人们建议找到每个标记,然后在标记中拆分每个句子。然而,我正在制作一个chrome扩展,这几乎需要在任何网站上运行,因此一个单词可能出现在标记之外,可能出现在类型标记中,甚至出现在中 了解Branch是如何做到的吗?他们似乎将所有内容都包装在中,并添加了有关字符计数的元数据。从他们的来源: <

上一个问题的后续行动:

这个问题我已经摸索了一段时间了。然而,我今天早上醒来,开始读到:

瞧!分支允许用户选择一个句子,然后共享它,保存它,等等…这正是我想要做的。看起来他们正在用
标签包装每个句子

以前,人们建议找到每个
标记,然后在标记中拆分每个句子。然而,我正在制作一个chrome扩展,这几乎需要在任何网站上运行,因此一个单词可能出现在
标记之外,可能出现在
类型标记中,甚至出现在


了解Branch是如何做到的吗?

他们似乎将所有内容都包装在
中,并添加了有关字符计数的元数据。从他们的来源:

<p><span class="highlight js-highlight-this" data-end-char="23"
data-highlight-count="0" data-start-char="0" id="highlight-86552-0">No
doubt they can lose.</span> <span class="highlight js-highlight-this"
data-end-char="132" data-highlight-count="0" data-start-char="24" id=
"highlight-86552-24">As Adi says, I don't think they will, but OKC - in
particular - still looms as a legit threat to the throne.</span>
<span class="highlight js-highlight-this" data-end-char="336"
data-highlight-count="0" data-start-char="133" id="highlight-86552-133">The
Thunder are better on both ends this year than last, have the experience of
having been there before, and you know Durant doesn't want to spend the
rest of his career playing second fiddle to LeBron.</span> <span class=
"highlight js-highlight-this" data-end-char="588" data-highlight-count="0"
data-start-char="337" id="highlight-86552-337">The problem, and I think the
reason so many assume the Heat will repeat, is that we haven't seen this
version of the Thunder (with Kevin Martin rather than James Harden in the
6th man role) in the playoffs before so the mystery factor comes into
play.</span></p>
否
我怀疑他们会输。正如阿迪所说,我认为他们不会,但OKC-in
特别-仍然是对王位的合法威胁。
这个
雷霆今年两头都比去年好,有经验吗
以前去过那里,你知道杜兰特不想浪费时间
余下的职业生涯对勒布朗来说是次要的。这个问题,我认为
这么多人认为高温会重演的原因是我们还没有看到这一点
雷霆的版本(与凯文·马丁而不是詹姆斯·哈登在一起)
第6人(角色)在季后赛之前,所以神秘因素开始出现
玩

然而,另一种更灵活的方法是简单地使用正则表达式匹配从任何元素的文本中提取句子,无论是span、p、h1等


在这个场景中,您将通过正则表达式匹配找到句子,然后使用javascript动态地用
元素将每个句子包围起来。然后,您可以将事件侦听器附加到这些动态创建的标记上,以进行高亮显示,以及在悬停、单击等操作时希望执行的任何其他操作。

您可以执行类似的操作,我想这与您所要执行的操作不太一样吧?但可能会给你一个进一步想法的开始

<div>In cryptography, a keyed-hash message authentication code (HMAC) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authentication of a message. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and on the size and quality of the key.</div>
<button id="get">Get Selected</button>

function getText() {
    var selectedText

    if (typeof window.getSelection === "function") {
        selectedText = window.getSelection();
    } else if (typeof document.getSelection === "function") {
        selectedText = document.getSelection();
    } else if (document.selection && typeof document.selection.createRange() === "function") {
        selectedText = document.selection.createRange().text;
    } else {
        selectedText = "";
        alert("No method to get selected text");
    }

    if (!selectedText || selectedText === "") {
        if (document.activeElement.selectionStart) {
            selectedText = document.activeElement.value.substring(
            document.activeElement.selectionStart.document.activeElement.selectionEnd);
        }
    }

    alert(selectedText);
}

document.getElementById("get").addEventListener("click", getText, false);
在密码学中,密钥化散列消息认证码(HMAC)是用于计算消息认证码(MAC)的特定构造,该消息认证码涉及密码散列函数与秘密密码密钥的组合。与任何MAC一样,它可用于同时验证数据完整性和消息的身份验证。任何加密散列函数,例如MD5或SHA-1,可用于HMAC的计算中;由此产生的MAC算法被相应地称为HMAC-MD5或HMAC-SHA1。HMAC的加密强度取决于基础哈希函数的加密强度、其哈希输出的大小以及密钥的大小和质量。
被选中
函数getText(){
变量selectedText
if(typeof window.getSelection==“函数”){
selectedText=window.getSelection();
}else if(typeof document.getSelection==“函数”){
selectedText=document.getSelection();
}else if(document.selection&&typeof document.selection.createRange()=“函数”){
selectedText=document.selection.createRange().text;
}否则{
selectedText=“”;
警报(“没有获取所选文本的方法”);
}
如果(!selectedText | | selectedText==“”){
if(document.activeElement.selectionStart){
selectedText=document.activeElement.value.substring(
document.activeElement.selectionStart.document.activeElement.selectionEnd);
}
}
警报(选择文本);
}
document.getElementById(“get”).addEventListener(“单击”,getText,false);

你也可以看到我对这个想法的进一步解释

作者提出了另一个问题,但是

总结

返回表示所选文本范围的选择对象 用户

规格

DOM级别0。不是任何标准的一部分

它预计将在新的DOM范围规范中指定

还有一个名为的库,应该可以处理这种瘦的交叉浏览器,从未尝试过,但您可能想看看

跨浏览器JavaScript范围和选择库。它提供了一个 简单的基于标准的API,用于执行常见的DOM范围和 所有主要浏览器中的选择任务,将 Internet和Internet之间此功能的不同实现 浏览器版本8及DOM兼容的浏览器