Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify code in DevTools #342

Open
flat-eric147 opened this issue Jan 13, 2022 · 3 comments
Open

modify code in DevTools #342

flat-eric147 opened this issue Jan 13, 2022 · 3 comments
Assignees
Labels

Comments

@flat-eric147
Copy link

Originally posted by @ClearScriptLib in #260 (comment)

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
Copy link
Author

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
Copy link
Author

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:

  • It seems it is not possible to add workspace with the DevTools opened, see cef forum comment
    Without the workspace you cannot save any js files, meaning that it's pointless to use it in the hosting application. I want the user to be able to edit and save the code in the DevTool transparently.
  • In theory one could construct a link like
    devtools://devtools/bundled/inspector.html?ws=localhost:4999/devtools/page/256606BCB8B7F6CA7CFD3ED6989AD45D
    and open that in an external chrome browser (instead of the internal CEF). The problem with this approach is that you can only do this MANUALLY by copying the url by hand to a chrome browser, so there is no way to invoke this from the host application through a process invocation. This must be some "protective feature" in chrome.

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

  • Can use DevTools and add workspaces
  • Needs installation of webView2 package on target machine
  • A WebView2 instance needs to be added to visual tree (which can be hidden though)
  • CoreWebView2.AddHostObjectToScript accepts only classes that are com visible (eg.[ComVisible(true)]). That makes it difficult/impossible to use certain external class instances within the script.
  • Exposed objects can be referenced in JavaScript through a lengthy syntax like
    window.chrome.webview.hostObjects.name instead of just name as in ClearScript.

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:

  • Connect to you ClearScript with chrome inspector (chrome://inspect/#devices), remote debugging in ClearScript must be enabled of course.
  • Go to Sources and select Module, right click on it and select Override content.
    OverrideContent
  • It will ask you to select a folder, select your folder of choice
    SelectFolderDialog
  • Allow full access to the folder
    AllowDialog
  • The module should have a violet circle to indicate that it is overwritten
    ModuleOverwrittenSuccess

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
Copy link
Collaborator

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants