Editing in the Chrome debugger
Asked Answered
F

15

321

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.

Fariss answered 21/2, 2011 at 15:5 Comment(4)
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.Connally
This is an awesome tutorial for the Chrome debugger. It shows the very simple steps for making in-debugger changes to your scripts.Continuant
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.Tsunami
Please find this #66920Gaseous
E
94

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.

Equable answered 31/3, 2011 at 21:54 Comment(9)
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 #12114269Moa
There is no such method. I'm also unable to modify code under Sources tab.Heptane
Chrome debugger does not allow for local modification of javascript. False.Chalaza
@oMiKeY What is false? Chrome certainly does allow modifying the script in the debugger.Chuffy
@oMiKeY you can modify if the script is not prettifiedEvers
Seems like I can't edit the js in between <source> tags in my HTMLEnglishman
To edit a file setup a Chrome workspace under Filesystem.Freezedrying
One other thing to note is that while you can edit/change the javascript in the chrome dev tool if it's in a separate .js file, you can't change it if it's simply javascript on an html page page wrapped in a <script> tag.Compact
"you can modify if the script is not prettified" - you can also add break point in minified file, modify it, then prettify it - you will se modified version where you can additional breakpointsFinnougric
A
325

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:

  1. Add a break-point at an earlier point in the script
  2. Reload page
  3. Edit your changes into the code
  4. CTRL + s (save changes)
  5. Unpause the debugger
Atingle answered 22/10, 2012 at 20:23 Comment(9)
Thanks! I found this worked when needing to make changes in a self-executing anonymous function that was called right near page load.Solicitor
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.Brahms
Crucial for me was to know that changes should be saved (step 4). Thanks!Cherri
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.Alonzo
So there isn't any way to modify scripts in html files?Stylography
Trying to use this technique in an infinite loop for quick testing of ideas (ie. don't have to reload the page, just step through code using F8). Anybody achieve this effectively, maybe with callback? function infiniteLoop() { do { myFunction() } while(false); } Change false to true after running with a break-point at top of script or beforehand.Fanfaronade
I'm surprised that this still seems to work for some people. Live editing was possible around 2011/12 (see #7343776) but isn't anymore: You can edit a script file but Chrome executes the unmodified version. Use local overrides instead (see https://mcmap.net/q/98823/-editing-in-the-chrome-debugger).Cryobiology
@Cryobiology Do Chrome overrides even work for Vue files? #5068032 I have not gotten the overrides to work. Thanks!Dobruja
@Ryan: As long as it's JavaScript it doesn't matter which framework. Of course, you can't run a .vue file in the devtools. Setting up local overrides is a bit tricky - follow developer.chrome.com/blog/new-in-devtools-65/#overrides carefully.Cryobiology
E
94

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.

Equable answered 31/3, 2011 at 21:54 Comment(9)
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 #12114269Moa
There is no such method. I'm also unable to modify code under Sources tab.Heptane
Chrome debugger does not allow for local modification of javascript. False.Chalaza
@oMiKeY What is false? Chrome certainly does allow modifying the script in the debugger.Chuffy
@oMiKeY you can modify if the script is not prettifiedEvers
Seems like I can't edit the js in between <source> tags in my HTMLEnglishman
To edit a file setup a Chrome workspace under Filesystem.Freezedrying
One other thing to note is that while you can edit/change the javascript in the chrome dev tool if it's in a separate .js file, you can't change it if it's simply javascript on an html page page wrapped in a <script> tag.Compact
"you can modify if the script is not prettified" - you can also add break point in minified file, modify it, then prettify it - you will se modified version where you can additional breakpointsFinnougric
B
93

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

(In the new versions of chrome, first you will need to choose a folder to save override files.) enter image description here

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

The Overrides sub tab in Chrome Developer Tools

Byars answered 23/4, 2020 at 1:39 Comment(6)
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 youBinomial
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.Dobruja
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".Frizzy
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.Saliva
Thanks very much, this was what I was looking for and gets the job done!Antonio
For anyone here with the issue of the browser still running the old code, you have to save the file after editing it, event if you do it from the browser (ctrl+s)Through
F
19

Chrome Overrides

  1. Open the JS file in the sources panel.

Right Click on script src URL > Reveal in Sources panel

Context menu for opening script in Sources Panel

  1. Click "+ Select folder for overrides" and choose a local folder to save file overrides

Select folder for overrides button

  1. Make sure "Enable Local Overrides" is checked.

Enable local overrides is checked

  1. Right Click anywhere in the JS file > Save for overrides

Click the "Save for overrides" button in the context menu

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.

file icon with purple dot

Frizzy answered 5/10, 2021 at 23:50 Comment(3)
has this changed? I can't see the "enable local changes" nor the "save for override" optionsThrough
I see, you first need to select a folder for overrides, it'd be nice to mention itThrough
@Through 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.Frizzy
E
15

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/

Eldreeda answered 9/4, 2014 at 20:26 Comment(0)
M
7
  1. Place a breakpoint
  2. Right click on the breakpoint and select 'Edit breakpoint'
  3. Insert your code. Use SHIFT+ENTER to create a new line.

enter image description here

Mushro answered 12/10, 2020 at 6:58 Comment(0)
C
5

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

Courtesy answered 21/2, 2011 at 15:9 Comment(10)
It would be awesome if you could save the changes to disk in the case of file:// URLsEveliaevelin
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.Fariss
Oh damn, you're right. Now I wonder why Chrome allows us to edit anything if it doesn't have any effect..Courtesy
"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.Connally
anyone got any solutions on this further?Bondsman
and this one called tincr: chrome.google.com/webstore/detail/…Sverre
it seems that i cannot modify inline javascript in html fileWalther
@AaronLS I'm also finding that there is no way to edit JS files in DevTools > Sources > Sources. This is very odd because "Local modifications" still shows up in the context menu and opens a blank "Local modifications" pane. HmmmmMell
@RichardBronosky Sometimes it works for me, have yet to figure out the pattern though.Sociolinguistics
Is there any way to modify script which resides directly into html page?Stylography
S
4

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.

Image

Solmization answered 7/1, 2019 at 9:37 Comment(0)
R
3

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.

Ricercare answered 8/7, 2015 at 18:47 Comment(0)
R
3

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.

Riordan answered 2/8, 2017 at 20:19 Comment(0)
G
3

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

Gaseous answered 26/1, 2018 at 11:39 Comment(0)
S
1

you can edit the javascrpit files dynamically in the Chrome debugger, under the Sources tab, however your changes will be lost if you refresh the page, to pause page loading before doing your changes, you will need to set a break point then reload the page and edit your changes and finally unpause the debugger to see your changes take effect. Chrome debugger

Stoa answered 30/9, 2015 at 14:39 Comment(2)
you have to keep the console open so the chrome will stop in breakpoints , else chrome will ignore the breakpointsStoa
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.,Constitutionally
H
1

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

  1. Set the breakpoint in the first line of the script you want to change and debug.

  2. Reload the page so the breakpoint is being hit

  3. Paste your new script and set desired breakpoints in it

  4. Ctrl+s, and the page will refresh causing that breakpoint in first line to be hit.

  5. F8 to continue, and now your newly pasted script replaces original one as long as no redirections and reloads are made.

Homy answered 6/11, 2015 at 16:47 Comment(0)
T
1

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

enter image description here

Toastmaster answered 1/10, 2018 at 1:19 Comment(0)
N
0

You can use "Overrides" in Chrome devtools to update javascript files between page loads.

(In the new versions of chrome, first you will need to choose a folder to save override files.) enter image description here

  1. Chrome will ask for permission to the folder, click Allow
  2. Edit the file in Sources>Page then save (ctrl-s). A purple dot will indicate the file is saved locally. The Overrides sub tab in Chrome Developer Tools
Nidianidicolous answered 10/11, 2023 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.