1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.net.URL;
  4. import java.net.URLConnection;
  5. public class HtmlInstrumenter {
  6. public static void instrumentHtml(String urlString) throws IOException {
  7. URL url = new URL(urlString);
  8. URLConnection connection = url.openConnection();
  9. String htmlContent = connection.getInputStream().readString(connection.getContentLength());
  10. // Inject script tag at the beginning of the HTML
  11. String instrumentedHtml = "<script>function logError(message) { console.error('HTML Instrumentation Error: ' + message); } </script>\n" + htmlContent;
  12. // Output the instrumented HTML to a file (or back to the user)
  13. try (PrintWriter writer = new PrintWriter(System.out)) {
  14. writer.println(instrumentedHtml);
  15. }
  16. }
  17. public static void main(String[] args) throws IOException {
  18. if (args.length != 1) {
  19. System.out.println("Usage: java HtmlInstrumenter <url>");
  20. return;
  21. }
  22. String url = args[0];
  23. instrumentHtml(url);
  24. }
  25. }

Add your comment