collapseEmptyDivs() is for ads (including AdSense ads) delivered through Google Ad Manager (previously called Doubleclick for Pusblishers or DFP in short), it will not work for direct Google AdSense ads neither manual nor auto-ads.
It does work just fine for DFP ads, though I am not sure if it is a good idea to use it is it most likely would increase CLS
I surrounded the ad space in a div, but it just leaves a large white space when nothing shows up. I have service workers so that the user can use the website while offline. I would like the ad space to collapse so that there is more room for content. This is what I have pasted into the my website.
AdSense is already doing that - though I'm not sure if it's rolled out to all accounts yet.
Simplifying "If no ads available"
We're continuously working to increase the quality and safety of our ad network and reduce the number of malicious ads that enter the AdSense network. As a result, we're removing the "If no ads available" option and we'll be defaulting to collapsing the ad space or showing a blank space instead. If your current setting is a color or another URL, it will be updated in the near future.