Advertisement
A_GUES

Java block ads

May 30th, 2023 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. import org.jsoup.Jsoup;
  2. import org.jsoup.nodes.Document;
  3. import org.jsoup.nodes.Element;
  4. import org.jsoup.select.Elements;
  5.  
  6. public class AdBlocker {
  7.     public static void main(String[] args) {
  8.         // Replace the "html" variable with the actual HTML content
  9.         String html = "<html><body><div class='ad'>Ad 1</div><p>This is some content.</p><div id='ad2'>Ad 2</div></body></html>";
  10.  
  11.         // Load the HTML document
  12.         Document document = Jsoup.parse(html);
  13.  
  14.         // Select all elements with class names or attributes commonly used in ad tags
  15.         Elements adElements = document.select(".ad, [class*=ad], [id*=ad], [id*=Ad], [id*=AD], [class*=ad], [class*=Ad], [class*=AD]");
  16.  
  17.         // Loop through the ad elements and remove them
  18.         for (Element adElement : adElements) {
  19.             adElement.remove();
  20.         }
  21.  
  22.         // Print the modified HTML document
  23.         System.out.println(document.html());
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement