Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Javascript of production website with local Javascript?

On my production website, I have compiled Javascript.

<script src="/js/mycode.min.js"></script> 

It would be very convient for debugging if I could make my browser replace that with

<script src="http://localhost/js/mycode1.js"></script> <script src="http://localhost/js/mycode2.js"></script> ... 

I know I could manipulate the DOM using something like Greasemonkey userscripts, but I couldn't come up with a solution which would prevent the execution of "mycode.min.js".

Any ideas?

like image 675
Julius Eckert Avatar asked Oct 14 '10 18:10

Julius Eckert


People also ask

What are the 3 ways you can include JavaScript in your web?

There are typically three ways to add JavaScript to a web page: Embedding the JavaScript code between a pair of <script> and </script> tag. Creating an external JavaScript file with the . js extension and then load it within the page through the src attribute of the <script> tag.

Can Python replace JavaScript in a website?

1 Answer. No, Python cannot replace JavaScript because: (FRONT-END)JavaScript is browser-native and Python is not. (BACK-END) neither JavaScript nor Python is web-native.


2 Answers

The way I do it:

  1. Download and install Fiddler if you are on windows.
  2. Enable it to catch http traffic [IE/Chrome does it by default, Firefox - enable it through the add on it installs]
  3. Load up the page in question.
  4. Find the file you want to replace in the http traffic list on the left and click on it.
  5. On the right there is an AutoResponder tab. click on it.
  6. Click on the checkbox to "enable automatic responses"
  7. Click Add.. button
  8. The 2nd dropdown on right, choose the option that says "find a file"
  9. Locate the file in the dialog and click save
  10. Repeat steps 4-9 until you replace all the files you want to replace
  11. Refresh the browser window and your new js files are running

Instead of replacing the js file, you can replace the html file and change the js links on the page.

You can install Charles if you are on a mac/linux. (not free, has trial) Steps are similar, but not the same.

If you are using Google Closure to compress files, you can install their plug-in to do the source mapping.

like image 113
epascarello Avatar answered Sep 30 '22 08:09

epascarello


What about using a subdomain like http://static.example.comfor static files (e.g. .js files), and changing the hostfile?

<script type="text/javascript" src="http://static.example.com/js/mycode.min.js"></script> 

Add the following line to the hostfile (/etc/hosts for Linux, C:\WINDOWS\System32\drivers\etc\host):

static.example.com 127.0.0.1 

Of course you've to run a server with the files from http://static.example.com/ on 127.0.0.1.

Another solution is using a (local) proxy server like Privoxy for redirecting http://example.com/js/ to http://localhost/js/.

like image 44
Lekensteyn Avatar answered Sep 30 '22 07:09

Lekensteyn