Link rewriting for affiliate codes

哇,多么精彩且富有教育意义的教程!

那个 JSFiddle 只是我找到的一个示例。我们这位新的潜在合作伙伴使用的是 https://前缀,而不是在末尾附加联盟 ID。他们使用的是 adtraction.com 平台。

在此发布我修改后的版本,供其他人使用。


<script type="text/discourse-plugin" version="0.8.42">
api.decorateCookedElement(
  element => {
        const affiliate = 'https://their-ad-network.com/...';
        const affiliate_links = element.querySelectorAll('a[href*="potentialparterwebsite.com"]');

        affiliate_links.forEach(function(el) {
            if (!el.href.startsWith(affiliate)) {
                el.href = affiliate + encodeURIComponent(el.href);
            }
        });
    },
  {
    onlyStream: true
  }
);
</script>

现在,该代码会先添加广告网络的 URL 以跟踪点击,然后再追加合作伙伴链接。

3 个赞