为了更好地了解 Flipboard 流量与普通 Web 流量相比的表现,我们希望在 Flipboard 在其移动应用程序中加载我们的 AMP 页面时为 Flipboard 使用唯一的亚马逊会员 ID。
AMP 为自定义 JavaScript 提供amp-script,但它有很大的局限性。值得注意的是,它不允许您在没有相关用户输入的情况下改变 DOM。我想到的最好的主意是:
<script id="entry-content" type="text/plain" target="amp-script">
document.querySelectorAll('a').forEach(function(el) {
el.addEventListener('click', function() {
var link = el.getAttribute('href');
if (-1 === link.indexOf('amazon.com')) {
return;
}
el.setAttribute('href', link.replace(/tag=[A-Za-z0-9\-]+/, 'tag=newid' ));
});
});
</script>
AMP 允许突变,但浏览器使用新选项卡的原始link
值,因此这不起作用。
我也试过:
- 调用
window.open()
事件click
处理程序。open()
虽然不可用。 - 只需修改上的链接
document.addEventListener('scroll')
。AMP杀死了突变。
还有什么巧妙的想法吗?