highlightWords

Highlight Words

Ce script ne devrait pas être installé directement. C'est une librairie créée pour d'autres scripts. Elle doit être inclus avec la commande // @require https://update.greasyfork.ip-ddns.com/scripts/535935/1588732/highlightWords.js

Auteur
Viktor Burdyey
Version
0.0.1.20250514040647
Créer
2025-05-14
Mis à jour
2025-05-14
Taille
2,31 ko
Licence
S/O

Example Usage

<!DOCTYPE html>
<html>
<head>
  <style>
    .custom-highlight {
      background-color: lightgreen;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <p>Hello world! Say hello again.</p>
  <script src="highlightWords.js"></script>
  <script>
    highlightWords(['hello', 'world'], 'custom-highlight');
  </script>
</body>
</html>

Output: The words "Hello" and "world" (in any case) are wrapped in , appearing with a light green background and bold text.

Notes:

  • Default Class: If you call highlightWords(['hello']), it uses the default highlight class.
  • Custom Styling: Define the CSS for your highlightClass (e.g., .custom-highlight) to style the highlighted text.
  • Single Highlighting: The XPath ensures that text already inside a with the specified highlightClass is skipped, so words are highlighted only once, even if the function is called multiple times with the same or different classes.
  • Class Name Safety: The function assumes highlightClass is a valid CSS class name. Avoid spaces or special characters unless they’re valid in CSS.