This can be done but it’s not sophisticated enough for many ads. Websites are smart and will smuggle ads through known-good domains that you can’t block. You really need to be able to navigate the dom and use JS heuristics to identify ads and popups.
Move your ad blocking to a different layer. Like say, network level.