highlightWords

Highlight Words

Този скрипт не може да бъде инсталиран директно. Това е библиотека за други скриптове и може да бъде използвана с мета-директива // @require https://update.greasyfork.org/scripts/535935/1588732/highlightWords.js

Автор
Viktor Burdyey
Версия
0.0.1.20250514040647
Създаден
14.05.2025
Обновен
14.05.2025
Размер
2 КБ
Лиценз
Няма

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.