highlightWords

Highlight Words

Este script não deve ser instalado diretamente. Este script é uma biblioteca de outros scripts para incluir com o diretório meta // @require https://update.greasyfork.ip-ddns.com/scripts/535935/1588732/highlightWords.js

Autor
Viktor Burdyey
Versão
0.0.1.20250514040647
Criado
14/05/2025
Atualizado
14/05/2025
Tamanho
2,31 KB
Licença
N/A

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.