highlightWords

Highlight Words

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/535935/1588732/highlightWords.js

作者
Viktor Burdyey
版本
0.0.1.20250514040647
创建于
2025-05-14
更新于
2025-05-14
大小
2.3 KB
许可证
暂无

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.