Highlight Sentence on triple click

Highlights sentence when mouse hovers over it

  1. // ==UserScript==
  2. // @name Highlight Sentence on triple click
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @author https://github.com/b-y-f
  6. // @grant none
  7. // @version 1.4
  8. // @license MIT
  9. // @description Highlights sentence when mouse hovers over it
  10. // ==/UserScript==
  11. let isStarted = false;
  12. let clickCount = 0;
  13. let clickTime = Date.now();
  14.  
  15. function getSentences(node) {
  16. return node.innerText.split(/(?<=[\.!\?])\s/);
  17. }
  18.  
  19. function wrapSentences(node) {
  20. let html = node.innerHTML;
  21. html = html.replace(/(?<=[\.!\?])\s/g, '</span> <span>');
  22. if (window.trustedTypes && trustedTypes.createPolicy) { // Check if Trusted Types is enabled
  23. var policy = trustedTypes.createPolicy('myPolicy', {
  24. createHTML: (string) => string, // Policy that allows any string
  25. });
  26. node.innerHTML = policy.createHTML('<span>' + html + '</span>');
  27. } else {
  28. node.innerHTML = '<span>' + html + '</span>';
  29. }
  30. }
  31.  
  32. function highlightSentence(e) {
  33. const target = e.target;
  34. if (target.tagName === 'SPAN') {
  35. target.style.backgroundColor = e.type === 'mouseover' ? '#B4D5FE' : '';
  36. }
  37. }
  38.  
  39. function start(){
  40. document.body.addEventListener('mouseover', highlightSentence);
  41. document.body.addEventListener('mouseout', highlightSentence);
  42. const allParagraphs = document.querySelectorAll('p, li');
  43. allParagraphs.forEach(node => wrapSentences(node));
  44. console.log("start!")
  45. }
  46.  
  47. window.addEventListener('click', function() {
  48. let currentTime = Date.now();
  49. if (currentTime - clickTime < 200) {
  50. clickCount++;
  51. } else {
  52. clickCount = 1;
  53. }
  54. clickTime = currentTime;
  55.  
  56. if (clickCount === 3) {
  57. if (!isStarted) {
  58. start();
  59. isStarted = true;
  60. } else {
  61. }
  62. clickCount = 0;
  63. }
  64. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址