Of course there is. It’s called sandboxing.
This would be solved by an extremely straightforward regulation. I would propose:
If a website operator grants another company the ability to modify their website, they are liable for any effects of such a grant of control as though they had done it themselves. Additionally, to the extent that any law, regulation or contract requires a website operator to maintain confidentiality or privacy of user data, granting a third party the ability to violate the requirement is deemed violated even if the third party did not exercise that ability.
So, for example, a healthcare company that includes an ad script violates HIPPA if the provider is not itself HIPPA compliant regardless of whether the ad script read any data. If your chatbot said they would not train on your data but they included an unprotected ad script, then they presumptively sold your data and trained on it.
Grant some small defense if the side operator credibly attempted to ensure security.
And how about fixing the actual credit card system directly? There are some banking systems (ex: IDEAL - now I think rebranded as Wero - in the Netherlands) that just ask for authentication with a trusted device when you want to do a transaction, so there is no problem from people knowing your card number. The idea that in 2026 there is a "security code" written on a card is mind-boggling. Would you recommend writing your password on the monitor?
I’m suggesting that negligently leaving one’s digital door open be treated as if someone went in and robbed someone regardless of whether this actually occurred. And that the entity leaving the door open be jointly liable with the robber.
GDPR and EU261 (flight compensation) has similar provisions. Essentially end user can choose who to sue (within the "chain" defined by the law), get the compensation and the companies are free to fight out who is responsible for it. Now companies suddenly have interest on making sure they are not the ones who are left holding the bag (contractually & making sure they can actually recover money from ultimately liable party).
You actually described the other way perfectly fine, and when I grew up, this was the way the WWW functioned. There were actually annoying, flashing ads back then, too, but I mean this was the dotcom boom baby, the sky was the limit. Then came a company which went back to the old way: they came with text ads. That company is now one of the largest in the world. There absolutely is another way, but it requires some trust by advertisers, and as long as they don't want such, we'll get this one where they have free rein over websites while they should not have such power. Because they have proven over, and over, and over again they cannot keep such power responsively. So what do you do then as responsible site owner? You demote them.
I don't care if your business model is untenable if you don't use ads. You can join the rest of the economy and charge money for your product or service, instead of relying on exploitation to peddle likely worthless junk, if it has so little value that no one will pay for it otherwise.
"But I like targeted ads, how else would people find stuff?" This is where ad defenders usually act like their manipulative surveillance is somehow similar to a print ad in a newspaper. If your product actually has value to people, then they will find you, the same way they have throughout the history of markets.
I’m fine with companies paying a podcast host to mention their products (as long as the payment is acknowledged). I’m fine with companies paying to be shown on billboards (but I’m maybe not fine with permitting the billboards in the first place). I’m fine with print ads. I’m fine with conventional TV ads. I’d be fine with ads in online videos if (a) it was just content and not script and (b) the ad shown was purely a function of the video, possibly the time, and possibly the coarse geography of the viewer.