import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class HtmlInstrumenter {
public static void instrumentHtml(String urlString) throws IOException {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
String htmlContent = connection.getInputStream().readString(connection.getContentLength());
// Inject script tag at the beginning of the HTML
String instrumentedHtml = "<script>function logError(message) { console.error('HTML Instrumentation Error: ' + message); } </script>\n" + htmlContent;
// Output the instrumented HTML to a file (or back to the user)
try (PrintWriter writer = new PrintWriter(System.out)) {
writer.println(instrumentedHtml);
}
}
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Usage: java HtmlInstrumenter <url>");
return;
}
String url = args[0];
instrumentHtml(url);
}
}
Add your comment