highlightWords

Highlight Words

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @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.