Skip to content

Instantly share code, notes, and snippets.

@Amitind
Created September 15, 2022 16:53
Show Gist options
  • Save Amitind/f360178c551b14efa982bc853ae787be to your computer and use it in GitHub Desktop.
Save Amitind/f360178c551b14efa982bc853ae787be to your computer and use it in GitHub Desktop.
// grab all links on page and convert Nodelist to array using spread operator
const links = [...document.querySelectorAll('a')]
// filter links that contain specific word
links
.filter((i) => i.href.includes('amazon.com'))
.forEach((i) => {
// convert string href to URL
let link = new URL(i.href);
// update params if present or add new params if not present
link.searchParams.set('tag', 'test-tag');
// update the url on page
i.href = link.href;
// log to see update links
// console.log(i.href);
});
/* you can remove the .filter part if you are selecting links on page like
document.querySelectorAll('a[href^="http://www.amazon.com"], a[href^="https://www.amazon.com"], a[href^="http://amazon.com"], a[href^="https://amazon.com"]');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment