Highlight Sentence on triple click

Highlights sentence when mouse hovers over it

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Highlight Sentence on triple click
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @author     https://github.com/b-y-f
// @grant       none
// @version     1.4
// @license     MIT
// @description Highlights sentence when mouse hovers over it
// ==/UserScript==
let isStarted = false;
let clickCount = 0;
let clickTime = Date.now();

function getSentences(node) {
    return node.innerText.split(/(?<=[\.!\?])\s/);
}

function wrapSentences(node) {
    let html = node.innerHTML;
    html = html.replace(/(?<=[\.!\?])\s/g, '</span> <span>');
    if (window.trustedTypes && trustedTypes.createPolicy) { // Check if Trusted Types is enabled
        var policy = trustedTypes.createPolicy('myPolicy', {
            createHTML: (string) => string, // Policy that allows any string
        });
        node.innerHTML = policy.createHTML('<span>' + html + '</span>');
    } else {
        node.innerHTML = '<span>' + html + '</span>';
    }
}

function highlightSentence(e) {
    const target = e.target;
    if (target.tagName === 'SPAN') {
        target.style.backgroundColor = e.type === 'mouseover' ? '#B4D5FE' : '';
    }
}

function start(){
    document.body.addEventListener('mouseover', highlightSentence);
    document.body.addEventListener('mouseout', highlightSentence);
    const allParagraphs = document.querySelectorAll('p, li');
    allParagraphs.forEach(node => wrapSentences(node));
    console.log("start!")
}

window.addEventListener('click', function() {
    let currentTime = Date.now();
    if (currentTime - clickTime < 200) {
        clickCount++;
    } else {
        clickCount = 1;
    }
    clickTime = currentTime;

    if (clickCount === 3) {
        if (!isStarted) {
            start();
            isStarted = true;
        } else {
        }
        clickCount = 0;
    }
});