# 去除链接跟踪参数

> 删除文本中链接的 utm_ 参数和 fbclid、gclid 等点击 ID，其他参数保留

版本：1.0.0 · 作者 Deck

- 安装到 Deck：deck://install-plugin?id=remove-tracking-parameters&url=https%3A%2F%2Fgithub.com%2Fyuzeguitarist%2Fdeck-plugins%2Freleases%2Fdownload%2Fplugins%2Fremove-tracking-parameters-1.0.0.zip
- 下载 .zip：https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/remove-tracking-parameters-1.0.0.zip
- 在 GitHub 上查看：https://github.com/yuzeguitarist/deck-plugins/tree/main/plugins/remove-tracking-parameters

也可以用终端安装：

```sh
mkdir -p ~/.deck/scripts/remove-tracking-parameters && curl -fsSL https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/remove-tracking-parameters-1.0.0.zip | tar -xf - -C ~/.deck/scripts/remove-tracking-parameters
```

## 脚本

```js
// Removes tracking parameters (utm_*, fbclid, gclid and other click ids) from every URL in the text.
const PREFIXES = ["utm_", "mtm_"];
const NAMES = new Set(("fbclid gclid gclsrc dclid gbraid wbraid gad_source srsltid _gl msclkid igshid igsh twclid ttclid " +
  "li_fat_id epik yclid ysclid mc_cid mc_eid _hsenc _hsmi __hstc __hssc __hsfp hsctatracking mkt_tok pk_campaign pk_kwd " +
  "pk_keyword pk_source pk_medium pk_content pk_cid _openstat oly_anon_id oly_enc_id vero_id vero_conv wickedid " +
  "rb_clickid s_cid ml_subscriber ml_subscriber_hash __s oft_c oft_ck oft_d oft_id oft_ids oft_k oft_lk oft_sk").split(" "));
const URL_PATTERN = /\b(?:https?:\/\/|www\.)[A-Za-z0-9\-._~:\/?#[\]@!$&()*+,;=%]+/gi;

function isTracking(param) {
  const equals = param.indexOf("=");
  const name = (equals === -1 ? param : param.slice(0, equals)).toLowerCase();
  return NAMES.has(name) || PREFIXES.some((prefix) => name.startsWith(prefix));
}

function cleanUrl(url) {
  const query = url.indexOf("?");
  const hash = url.indexOf("#");
  if (query === -1 || (hash !== -1 && hash < query)) return url;
  const end = hash === -1 ? url.length : hash;
  const params = url.slice(query + 1, end).split("&");
  if (!params.some(isTracking)) return url;
  const kept = params.filter((param) => param !== "" && !isTracking(param));
  return url.slice(0, query) + (kept.length > 0 ? "?" + kept.join("&") : "") + url.slice(end);
}

function transform(input) {
  return String(input ?? "").replace(URL_PATTERN, (match) => {
    // Punctuation at the end belongs to the sentence; a ) or ] belongs to the URL only when it closes one inside it.
    let parens = 0;
    let brackets = 0;
    for (let i = 0; i < match.length; i += 1) {
      if (match[i] === "(") parens += 1;
      else if (match[i] === ")") parens -= 1;
      else if (match[i] === "[") brackets += 1;
      else if (match[i] === "]") brackets -= 1;
    }
    let end = match.length;
    while (end > 0) {
      const c = match[end - 1];
      if (".,;:!?*".indexOf(c) !== -1) {
        end -= 1;
      } else if (c === ")" && parens < 0) {
        parens += 1;
        end -= 1;
      } else if (c === "]" && brackets < 0) {
        brackets += 1;
        end -= 1;
      } else {
        break;
      }
    }
    return cleanUrl(match.slice(0, end)) + match.slice(end);
  });
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/remove-tracking-parameters
- English：https://deckclip.app/plugins/remove-tracking-parameters.md

© 2026 潘禹泽
