Last active
February 22, 2024 12:46
-
-
Save cahva/1432e386763afd3de37f970ab1dc3a68 to your computer and use it in GitHub Desktop.
inject id from search params to data-inject-qs elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/** | |
* To add this to links in the page, add data variable data-inject-qs to the link. | |
* For example: | |
* <a href="https://google.fi" data-inject-qs>Google</a> | |
* | |
* And If current url is /something?hello=world, it will inject ?hello=world to that link | |
* It will became https://google.fi?hello=world | |
* | |
**/ | |
waitForElementsPromise("a[data-osko]").then((injectableEls) => { | |
const currentSearchParams = new URL(window.location).searchParams; | |
const params = Object.fromEntries(currentSearchParams.entries()); | |
params.id = params.id || window.oskariAttendeeId; | |
injectableEls.forEach(function(node) { | |
const url = new URL(node.href); | |
Object.keys(params).forEach((key) => { | |
url.searchParams.set(key, params[key]); | |
}); | |
node.href = url.href; | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment