ChatGPT解决这个技术问题 Extra ChatGPT

Editing in the Chrome debugger

How do I "dynamically" edit JavaScript code in the Chrome debugger? It's not for me, so I don't have access to the source file. I want to edit code and see what effects they have on the page, in this case stopping an animation from queuing up a bunch of times.

You could use Opera. Opera allows editing of JS files. After you soft reload the page, your changes will be applied. Right click > Source > Make changes > Apply Changes.
This is an awesome tutorial for the Chrome debugger. It shows the very simple steps for making in-debugger changes to your scripts.
You can also "inject" code via conditional breakpoint. In the given example use num = 5, console.log(arguments[0], num), false to update and log the value inside foo function.

J
Jossef Harush Kadouri

I came across this today, when I was playing around with someone else's website.

I realized I could attach a break-point in the debugger to some line of code before what I wanted to dynamically edit. And since break-points stay even after a reload of the page, I was able to edit the changes I wanted while paused at break-point and then continued to let the page load.

So as a quick work around, and if it works with your situation:

Add a break-point at an earlier point in the script Reload page Edit your changes into the code CTRL + s (save changes) Unpause the debugger


Thanks! I found this worked when needing to make changes in a self-executing anonymous function that was called right near page load.
Argh, because I had tried this earlier and it didn't work and though oh man, how did I miss it. But yep, doesn't work for me.
Crucial for me was to know that changes should be saved (step 4). Thanks!
Doesn't work on javascript inside html files for me. Also, if you've added a folder to workspace, select the local js file, right-click and map that file to the network dito.
So there isn't any way to modify scripts in html files?
A
Adam

You can use the built-in JavaScript debugger in Chrome Developer Tools under the "Scripts" tab (in later versions it's the "Sources" tab), but changes you apply to the code are expressed only at the time when execution passes through them. That means changes to the code that is not running after the page loads will not have an effect. Unlike e.g. changes to the code residing in the mouseover handlers, which you can test on the fly.

There is a video from Google I/O 2010 event introducing other capabilities of Chrome Developer Tools.


In later versions of Chrome the tab icons have gone and the 'Scripts' tab is renamed to 'Sources' - so might not be obvious to find the javascript debugger. Some more info here stackoverflow.com/questions/12113769/…
There is no such method. I'm also unable to modify code under Sources tab.
Chrome debugger does not allow for local modification of javascript. False.
@oMiKeY What is false? Chrome certainly does allow modifying the script in the debugger.
@oMiKeY you can modify if the script is not prettified
M
Mark Foreman

You can use "Overrides" in Chrome to persist javascript changes between page loads, even where you aren't hosting the original source.

Create a folder under Developer Tools > Sources > Overrides Chrome will ask for permission to the folder, click Allow Edit the file in Sources>Page then save (ctrl-s). A purple dot will indicate the file is saved locally.

https://i.stack.imgur.com/BS7Fu.png


Omg this the only one that worked for me. I have tried editing directly on its own, adding folders to Filesystem, Snippets and my changes never get reflected. After adding a folder to overrides, it reloads and changes reflected! Thank you thank you
This looked promising but didn't work for me. I wonder if this is meant for only the simplest JS and not for something like Vue or anything compiled with Webpack.
This does work with Webpack. The easiest way to make it work is to go into the HTML inspector, find the <script src="js file" > Right Click > "Reveal in Source Panel". Then from there Right Click anywhere in the JS file > "Save for overrides".
Reiterating Ryan's comment, this does not work with Vuetify / Webpack. The chrome tools indicate that the local override is in place (with the lower left blue/purple dot showing on the source mapped .js file), but does not respect the updated code, even after various types of soft / hard / cache refreshes.
Thanks very much, this was what I was looking for and gets the job done!
a
artemisian

This is what you are looking for:

1.- Navigate to the Source tab and open the javascript file

2.- Edit the file, right-click it and a menu will appear: click Save and save it locally.

In order to view the diff or revert your changes, right-click and select the option Local Modifications... from the menu. You will see your changes diff with respect to the original file if you expand the timestamp shown.

More detailed info here: http://www.sitepoint.com/edit-source-files-in-chrome/


g
gnur

Pretty easy, go to the 'scripts' tab. And select the source file you want and double-click any line to edit it.


It would be awesome if you could save the changes to disk in the case of file:// URLs
That was exactly what I did, but the changes weren't reflected on the page, as one would expect. I need to change $(selector).fadeIn() ... to $(selector).stop(true,true).fadeIn() ... Y'know? And I wanna be able to see that happen on the page.
Oh damn, you're right. Now I wonder why Chrome allows us to edit anything if it doesn't have any effect..
"Save as..." only saves the file to your computer. It doesn't apply the changes after the page is reloaded. If you want to apply changes to JS files for debugging, you could use Opera.
and this one called tincr: chrome.google.com/webstore/detail/…
C
Chris Hayes

Chrome Overrides

Open the JS file in the sources panel.

Right Click on script src URL > Reveal in Sources panel

https://i.stack.imgur.com/iUsz5.png

Make sure "Enable Local Overrides" is checked.

https://i.stack.imgur.com/JrHsm.png

Right Click anywhere in the JS file > Save for overrides

https://i.stack.imgur.com/NB1Yb.png

All Set!

Just edit the file, and save with CMD/CTRL + S. Now whenever you refresh the page, it'll use the modified file. (As long as the filename remains the same)

You'll know it's working if you see a purple dot in the file icon.

https://i.stack.imgur.com/wkkYd.png


has this changed? I can't see the "enable local changes" nor the "save for override" options
I see, you first need to select a folder for overrides, it'd be nice to mention it
@raquelhortab I'll take a look when I get a chance, since you recently had to do this, it might be most helpful for users if you suggest an edit to the answer with that step included.
L
Lukáš Novák

Place a breakpoint Right click on the breakpoint and select 'Edit breakpoint' Insert your code. Use SHIFT+ENTER to create a new line.

https://i.stack.imgur.com/k8Jrf.png

https://i.stack.imgur.com/32sny.png


d
developer747

If its javascript that runs on a button click, then making the change under Sources>Sources (in the developer tools in chrome ) and pressing Ctrl +S to save, is enough. I do this all the time.

If you refresh the page, your javascript changes would be gone, but chrome will still remember your break points.


d
dominik

As this is quite popular question that deals with live-editing of JS, I want to point out another useful option. As described by svjacob in his answer:

I realized I could attach a break-point in the debugger to some line of code before what I wanted to dynamically edit. And since break-points stay even after a reload of the page, I was able to edit the changes I wanted while paused at break-point and then continued to let the page load.

The above solution didn't work for me for quite large JS (webpack bundle - 3.21MB minified version, 130k lines of code in prettified version) - chrome crashed and asked for page reloading which reverted any saved changes. The way to go in this case was Fiddler where you can set AutoRespond option to replace any remote resource with any local file from your computer - see this SO question for details.

In my case I also had to add CORS headers to fiddler to successfully mock response.


s
stackinfostack

Now google chrome has introduce new feature. By Using this feature You can edit you code in chrome browse. (Permanent change on code location)

For that Press F12 --> Source Tab -- (right side) --> File System - in that please select your location of code. and then chrome browser will ask you permission and after that code will be sink with green color. and you can modify your code and it will also reflect on you code location (It means it will Permanent change)

Thanks


P
Prashant Pimpale

Just like @mark 's answer, we can create a Snippets in Chrome DevTools, to override the default JavaScript. Finally, we can see what effects they have on the page.

https://i.stack.imgur.com/LhliU.png


m
meeech

here's a gentle introduction to the js debugger in chrome that i wrote. Maybe it will help others looking for info on this: http://meeech.amihod.com/getting-started-with-javascript-debugging-in-chrome/


That's a good writeup. Unfortunately, it says nothing of editing JS in the debugger which is the task in question.
Disagree. You see at the end I specifically give example how to ""dynamically" edit JavaScript code in the Chrome debugger" which is part of the question.
meeech, unless I am misunderstanding you post, you demonstrate how to evaluate JS at the console. That is different from editing a function so that when the function is called in the future, it behaves differently.
Fair enough. I understood edit to mean change around the values of the live page, and thought the write up would help, since once you are have it in the console, you can play around with the values, and any thing else js in the execution context.
yes, meeech, I did exactly what you describe for many years. I just finally got frustrated with not being able to achieve a good feedback loop for altering event handlers. Especially those that trigger during page load. As AaronLS and I discussed in the comments of gnur's answer, sometimes editing in DevTools > Sources > Sources works and other times it doesn't. But when it does work, it's pretty sweet!
S
Seif Tml

https://i.stack.imgur.com/wdAL1.jpg


you have to keep the console open so the chrome will stop in breakpoints , else chrome will ignore the breakpoints
not working. I reload using F5 and it stops, I change the file, then I continue running and I get the same error I just fixed. It's a js file beside the main page.,
1
100r

I was looking for a way to change the script and debug that new script. Way I managed to do that is:

Set the breakpoint in the first line of the script you want to change and debug. Reload the page so the breakpoint is being hit Paste your new script and set desired breakpoints in it Ctrl+s, and the page will refresh causing that breakpoint in first line to be hit. F8 to continue, and now your newly pasted script replaces original one as long as no redirections and reloads are made.


m
mark

Chrome DevTools has a Snippets panel where you can create and edit JavaScript code as you would in an editor, and execute it. Open DevTools, then select the Sources panel, then select the Snippets tab.

https://developers.google.com/web/tools/chrome-devtools/snippets

https://i.stack.imgur.com/BYzBR.jpg