Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Dev Tools - Modify javascript and reload

Is it possible to modify the JavaScript of a page and then reload the page without reloading the modified JavaScript file (and thus losing modifications)?

like image 996
CDT Avatar asked May 11 '13 05:05

CDT


People also ask

Can you edit JavaScript in Chrome developer tools?

Edit JavaScript in Google Chrome Google Chrome provides the most efficient and excellent way to make changes in JavaScript code. We can also associate a folder with the Google Chrome Developer tool's workspace.

How do I edit and run JavaScript in Chrome?

Editing JavaScript code in real-time is possible in Chrome and Chromium based browsers. After loading a web page completely, press the F12 key to open the developer tools, then open the 'Sources' tab. Now open any Javascript file loaded on the browser and you can directly edit it by clicking anywhere in that file.

How do I refresh JavaScript in Chrome?

Different operating systems and browsers use different commands or shortcuts. For Windows in Chrome or Edge, the keyboard shortcut Ctrl + F5 (or Ctrl + Reload) refreshes. For Mac, hold Cmd-Shift-R or Shift-Reload. Most browsers also have a refresh button next to the URL.


4 Answers

This is a bit of a work around, but one way you can achieve this is by adding a breakpoint at the start of the javascript file or block you want to manipulate.

Then when you reload, the debugger will pause on that breakpoint, and you can make any changes you want to the source, save the file and then run the debugger through the modified code.

But as everyone has said, next reload the changes will be gone - at least it lets you run some slightly modified JS client side.

like image 136
Ashley Schroder Avatar answered Oct 11 '22 10:10

Ashley Schroder


Great news, the fix is coming in March 2018, see this link: https://developers.google.com/web/updates/2018/01/devtools

"Local Overrides let you make changes in DevTools, and keep those changes across page loads. Previously, any changes that you made in DevTools would be lost when you reloaded the page. Local Overrides work for most file types

How it works:

  • You specify a directory where DevTools should save changes. When you make changes in DevTools, DevTools saves a copy of the modified file to your directory.
  • When you reload the page, DevTools serves the local, modified file, rather than the network resource.

To set up Local Overrides:

  1. Open the Sources panel.
  2. Open the Overrides tab.
  3. Click Setup Overrides.
  4. Select which directory you want to save your changes to.
  5. At the top of your viewport, click Allow to give DevTools read and write access to the directory.
  6. Make your changes."

UPDATE (March 19, 2018): It's live, detailed explanations here: https://developers.google.com/web/updates/2018/01/devtools#overrides

like image 33
Nico Avatar answered Oct 11 '22 10:10

Nico


The Resource Override extension allows you to do exactly that:

  • create a file rule for the url you want to replace
  • edit the js/css/etc in the extension
  • reload as often as you want :)
like image 44
laktak Avatar answered Oct 11 '22 11:10

laktak


  1. In the devtools preferences check the Enable local overrides.
  2. Go to network tab, find the file you want to edit, rigth click on it and select Save for overrides (on the sources/overrides tab you need to add a local folder)
  3. The file appears in a new tab on the Sources tab as local copy, so you can edit this file, and after site reload the new (and edited) override file will load on the site!
like image 45
Péter Zajácz Avatar answered Oct 11 '22 10:10

Péter Zajácz