← All examples

JavaScript Reading & focus ebay.com

eBay: highlight search keywords in results

Highlights your typed keywords in yellow inside listing titles so exact matches are easier to spot.

Code to copy

// Highlight the searched keywords inside result titles
function highlightKeywords() {
  const term = new URL(window.location.href).searchParams.get('_nkw');
  if (!term) { return; }
  const words = term.toLowerCase().split(/\s+/).filter(function (w) { return w.length > 2; });
  if (!words.length) { return; }
  document.querySelectorAll('.srp-results .s-item__title:not([data-jz-hl])').forEach(function (el) {
    el.setAttribute('data-jz-hl', '1');
    let html = el.innerHTML;
    words.forEach(function (w) {
      const re = new RegExp('(' + w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'gi');
      html = html.replace(re, '<mark style="background:#fff176">$1</mark>');
    });
    el.innerHTML = html;
  });
}
highlightKeywords();
const obs = new MutationObserver(highlightKeywords);
obs.observe(document.documentElement, { childList: true, subtree: true });

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases