我想在包含example.com的页面上添加任何链接url。
下面的脚本可以工作,但它只在链接没有任何参数的情况下才能工作。
<script>
document.body.innerHTML = document.body.innerHTML.replace('example.com', 'example.com?utm_day=monday);
</script>
我需要做的是在example.com页面上找到任何链接并附加它,无论链接中的其他参数如何。例如,脚本可以找到
<a href="https://example.com?formID=1234">
,并将其附加为
<a href="https://example.com?formID=1234&utm_day=monday">
有什么好主意吗?
首先选择所有的a
标签,然后替换它们,如果它们包含example.com.
let text = '&utm_day=monday'
document.querySelectorAll('a').forEach(
item => {
if (item.href.includes("example.com")) {
item.href += text
}
}
)