OAuth with different key and secret in one collection

My question:
I created a collection and several requests in it. I also wrote a script to get OAuth token. My requests are in different domain and used different OAuth credentials, my script can swith credential according the request url(domain).
I do not want to add this script for each request, is there a way to save the script function in collection level and call it in request level?

I’ve already tried:
Since the collection level pre-script is excuted before request, so I can not get the request url in this level, but I can not access the pre-script in request level…

This sounds like a use case for Environments.