microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.76k stars 148 forks source link

modify code in DevTools #342

Open flat-eric147 opened 2 years ago

flat-eric147 commented 2 years ago

Originally posted by @ClearScriptLib in https://github.com/microsoft/ClearScript/issues/260#issuecomment-833759613

Hello and thank you for your reply! I am not quite sure how you achieved that. I connected to my host through the DevTools (tried both chrome and edge), set a breakpoint and modified the code in DevTools, but as expected the new code is not executed. When the debugger hits the breakpoint again, a different tab opens with the original code. Any specific settings I forgot? I'm also not sure how the eventually modified code can be read back into my host application. The ScriptEngine class dos not appear to have getter methods to retrieve the code in execution.

flat-eric147 commented 2 years ago

Small update, I somehow managed to modify the code when the code hit a breakpoint, then I used CTR+S to save and continue, the code was then executed with the modification, but on further breakpoints it did not execute any modifications, it just kept running the first modification. Said that, I still have no idea on how to retrieve the modified code back into my application. Having this all working properly would be a really powerful feature. But anyhow, have I told you how much I love ClearScript? :-D

flat-eric147 commented 5 months ago

Hi, another update after more than 2 years :) In the meantime I was playing with CEF and webView2 but they both have massive disadvantages, for the interested reader her my results:

Using CEF and the ShowDevTools mehod to bring up and use the debugger:

Using MS webView2 (invoking CoreWebView2.OpenDevToolsWindow()):

None of the above approaches suit my needs, although it's intriguing to have a "proper' web browser doing the job opening up new possibilities. So, back to ClearScript. Having now being forced to explore the DevTools in more depth, I figured out something that might solve my original problem sticking to ClearScript. The "magic" can be done by overriding the script content:

Now you can set breakpoints, modify the code, save it with ctrl+s! The modifications will be written to disc of course filewrittentodisc

Once you stop the script from running in your host application, you can take the script from disc and store it with the modifications made during the debug session. The good thing is that those settings persist over time in chrome, the next time you run the ClearScript and you attach to it, the settings are already in place. You only need to take care of the copy stored on disc, the script will be overwritten by whatever has been found on disc as soon as you attach the debugger. For code consistency, one needs to overwrite the file with the content of whatever you pass to ClearScript BEFORE calling Execute.

I suppose all this works in a similar fashion using Visual Studio Code !?

To make this workflow a bit smoother, it might be useful to have in ClearScript a method "Execute" that takes a file name instead of a string containing the script, but it's easy to workaround this of course. Maybe you have other&better suggestions!

Sorry for the lengthy post, but I feel this might help other users who might stumble about this problem.

ClearScriptLib commented 5 months ago

Hi @flat-eric147,

I suppose all this works in a similar fashion using Visual Studio Code !?

Perhaps not; the content override feature could be unique to DevTools.

it might be useful to have in ClearScript a method "Execute" that takes a file name instead of a string containing the script

Sure, you can do the following:

engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading;
engine.ExecuteDocument(filePath);

Good luck!