← All examples

JavaScript Privacy news.ycombinator.com

Hacker News: copy a clean link

Adds a button to each story that copies its address without tracking parameters, ready to share.

Code to copy

// Add a button to copy each story link with tracking params stripped
(function () {
  document.querySelectorAll('.titleline > a:first-child').forEach(function (a) {
    if (a.parentNode.querySelector('.jz-copy-clean')) { return; }
    const btn = document.createElement('a');
    btn.className = 'jz-copy-clean';
    btn.textContent = ' [copy clean]';
    btn.href = 'javascript:void(0)';
    btn.style.cssText = 'color:#ff6600;font-size:8pt;margin-left:6px;cursor:pointer;';
    btn.addEventListener('click', function () {
      try {
        const u = new URL(a.href);
        ['utm_source','utm_medium','utm_campaign','utm_term','utm_content',
         'fbclid','gclid','ref','ref_src'].forEach(function (p) {
          u.searchParams.delete(p);
        });
        navigator.clipboard.writeText(u.toString());
        btn.textContent = ' [copied]';
        setTimeout(function () { btn.textContent = ' [copy clean]'; }, 1500);
      } catch (e) {}
    });
    a.parentNode.appendChild(btn);
  });
})();

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