ChatGPT You said: Is it possible to make a Proxy of document.write in JavaScript? ChatGPT Thought for 6 seconds Yes, it is possible to create a Proxy for document.write in JavaScript. By using a Proxy, you can intercept and modify calls to document.write, allowing you to add custom behavior such as logging, modifying arguments, or even preventing certain writes. Here's a comprehensive guide on how to achieve this: Understanding Proxies in JavaScript A Proxy in JavaScript allows you to create a wrapper around an object or function, intercepting operations like property access, assignment, enumeration, function invocation, etc. This makes Proxies powerful tools for customizing behavior without modifying the original object or function directly. Creating a Proxy for document.write Since document.write is a method (function) on the document object, you can create a Proxy specifically for this function. Here's how you can do it: Store the Original document.write Function Before creating a Proxy, it's essential to keep a reference to the original document.write to ensure that you can still call it after interception. javascript Copy code const originalDocumentWrite = document.write.bind(document); Note: Using .bind(document) ensures that the original context (this) is preserved when calling document.write later. Define the Proxy Handler The handler defines the traps (interceptors) for various operations. For document.write, you'll primarily be interested in the apply trap, which intercepts function calls. javascript Copy code const handler = { apply(target, thisArg, argumentsList) { // Custom behavior before calling the original function console.log('document.write called with arguments:', argumentsList); // Example: Modify the arguments if needed const modifiedArgs = argumentsList.map(arg => { // For instance, append a comment to each argument return `${arg} `; }); // Call the original document.write with modified arguments return target(...modifiedArgs); } }; Create the Proxy With the handler defined, create the Proxy that wraps the original document.write function. javascript Copy code const proxiedDocumentWrite = new Proxy(originalDocumentWrite, handler); Replace document.write with the Proxy Finally, assign the Proxy back to document.write so that all subsequent calls go through the Proxy. javascript Copy code document.write = proxiedDocumentWrite; Full Example Putting it all together, here's a complete example: html Copy code Proxy Example for document.write '); Explanation of the Example: Logging: Every time document.write is called, the Proxy logs the arguments to the console. Modifying Arguments: The Proxy appends a HTML comment to each argument, demonstrating how you can alter the content being written. Blocking Specific Content: The example includes a condition to block writing of