How to edit the javascript sources of a webpage in browser - javascript

So I want to play with the source code of a page. In this particular example it seems like the webpage is an Angular app (that i dont own) with source maps to *.ts files. The main code seems to all be in a bundle.js.
I want to be able to add console.log() statements to this code and learn more about how this webpage works.
I have tried editing the .ts sources (this doesn't work although I expected that).
I try editing the bundle.js but its so big that it causes my dev tools to crash.
Does anyone know of a VS Code plugin (or something similar) that would let me open the sources locally on my machine, edit them and see the changes reflected on the website.
If not what is the best path forward for being able to edit the local js of this page.

Firefox allows you to add log statements into the code when you're using the Debugger/Sources tab. Just right click the line you care about and then choose Add log. I believe Chrome allows something similar.

Related

Firefox developer tools: JavaScript files show up as HTML

Since a few days, the JS debugger in Firefox (68.0.1) doesn't open JS files correctly. Instead, some HTML is displayed. The JS files do not have a file extension at all, yet the Content-Type header correctly contains application/x-javascript. Other files with js extension do show up with a JS icon in front of them.
Is this a recent change? What can I do to make those files show up correctly in the debugger?
The application I use is built in XPages, so there might also be some setting there that used to create files with the .js extension, but I'm not sure of that.
ADDED
I can tell you that xcsDocument definitely is a JS file, and that Firefox has no problems using it. No JS errors displayed, no HTML errors, nothing significant on the Console tab.
I had the same problem a couple of days ago. Add the .js extension to your client-side javascript design element, and if need be, double-check the custom controls etc where the script is being used to verify the .js extension is there too.

Debugging Javascript inside php file possible?

I am trying to find the way to debug javascript code inside the php file. I have a lot of javascript code embedded inside the php file.
I can debug php code using netbeans with the help of XDebug. I can also debug javascript separately inside html or js file with browser like chrome or firefox.
What I want is to debug javascript code inside the php file if it is possible. I am sure a lot of people will be using javascript embedded with php file. I don't like it personally, but I have to work it on. I know I can write the code separately in js file and then can debug with browsers, but it's lot of code, take time for the separation.
Can anybody here suggest me a way if it's possible what I am asking.
Debug Javascript in netbeans 7.4 inside PHP project
https://netbeans.org/kb/docs/php/debugging.html
https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+and+JavaScript+code+at+the+same+time+in+PhpStorm
https://blog.jetbrains.com/phpstorm/2014/06/debugging-php-and-javascript-code-at-the-same-time/
IMHO, without even looking it up, i don't think that it is (nor should) be feasible.
Here's why:
Your PHP gets processed on the server side, that's when XDebug kicks in and enables you to breakpoint all your PHP code. Then the server output gets to the client, that's when the actual JS is processed inline in the parsed HTML. Which means you would have to intercept the HTML in some way, parse it, detect eventual inlined JS scripts... and set your breakpoint(s) at that time (yes on each run), then output to client, which parses the HTML yet again to render it and process eventual breakpoints. It would be a tedious process and even more tedious to get to work i guess and that is why nobody even tried making an extension for that.
To my knowledge, inlined JS is also a lot harder to debug and i never saw an actual setup that would allow breakpointing embedded tags in a static HTML document directly from the IDE, which would've been somewhat a little easier to achieve than breakpointing JS in PHP...
Your best shot i guess would be to externalise your JS in separate files and only hard code <script src="path/to/your/app.js"></script> in your PHP templates, which indeed would be much more comfortable to work with on the long run anyways.
Then you would be able to breakpoint all the stuff in app.js, plus have an actual front-end architecture, syntax-highlighting, impress your boss, make your life a lot easier, the world a better place, etc.
Also, for reference: How to debug JavaScript code with Netbeans? (answer #45515159)
And read on: https://netbeans.org/kb/docs/webclient/html5-js-support.html
Edit: seems like setting JS breakpoints in static HTML tags is feasible in Visual Code for example -> https://github.com/Microsoft/vscode-chrome-debug/issues/122
I don't know if I am late, but, I came across the following website and was able to setup the debugger for Javascript and PHP which supports to debug embedded JavaScript in PHP scripts.
source: https://abcmemo.com/2017/04/20/debug-php-and-javascript-in-visual-studio-code/
The website uses PHP.exe as a web service.
It is also possible to use IIS, Apache or others.
Requirements:
IDE: Visual studio code
Extension: PHP debug
Extension: Debugger for (Chrome, Firefox or Edge)
Xdebug set in php.ini
Extension in browser (optional): PHP xdebug if you want to debug on trigger.
You need to have two debuggers running at the same time one for PHP and one for JavaScrpit.
PHP sample
JavaScript sample
I worked with a lot of wordpress templates where I had to deal with some js inside my php files. Assuming that you can run your code on a server, you can easily debug your output in dev tools in chrome (if using chrome). Chrome also allows for setting of breakpoints so you can go line by line, and since your browser ultimately runs the js, you can monitor your code behavior without dealing with the php. That was my main way of dealing with this issue.
I also recommend separating as much js as possible into separate files in your assets folder. Depending on your project, you rarely have to inline your code, In my opinion it's messy to include a lot of JS right in your php, unless you use onclick="" or onchange="" attributes (which can also be handled with event listeners.
Aside from that, console.log() the crap out of everything.
I helped my self with the following steps if it could help any body else
Note:- If your rendered code is inline specific to java-script then it would hard to debug like this.
Run the required page of your application using browser like chrome, edge etc
Open the inspection page by pressing f12 or (Ctrl+Shift+I), Or you can right click on the page see the option for the inspection and click on it.
Goto sources and double click on the source file(this will be probably your php file).
If code not loaded then reload the page by pressing f5 or Ctrl+R, you will see your java-script code there embedded with the html after rendering, then you can put the break point wherever you want and debug through the browser tools(you can see some buttons there to guide you about debugging like step-over,step-into etc).
You will also see errors there regarding java-script,

How to debug js loaded in ajax request in Chrome DevTools

Actually I am a big fan of Google Chrome DevTools but it is very vast area for learning. I don't know much about it. I know how to out debug point, searching (Ctrl+Alt+F) and debugging, etc. basic things.
But in one of my project (its in asp.net MVC), it is requesting different views in Ajax request and in that views there are some js loading. I want to debug them but I am not able to. Actually I don't know where can I see them - especially the JS which is load after ajax call (its not inline javascript but a separate js files).
A workaround I found is - if I put 1 "debugger;" in that js file then it opens with some VM3012 or similar name with different numbers. Then only I can see the files. But I can not put debugger when I publish the code.
Any body knows then please help.
Thanks a lot for reading.
EDIT
as per first answer, I have tried but I can see that option "Open in Source panel" for normal JS file. But in my case my request is passing some query string to the js file like assets/components/utility/mycustom.js?_=1528282662460 and I am not able to see that option for such request. This help is appreciated but need more.
I know it's been 3 years but...
You can name the piece of javascript code inside the dynamic loaded file by adding the following above the code:
//# sourceURL=my-file-name.js
By doing it, that piece of javascript will be available in the file tree of the source panel and can also be found by the DevTools' CTRL+P shortcut.
Go to the network tab of the dev tools. You can filter by js files to help you find the file you need. Right click on the file you want and select Open in sources panel. You should now be able to put the breakpoint where you like as you are used to.

Use JavaScript Node command line tool in Chrome

Hey I have an app written in js being accessed by $ node script.js
I want to visualize the output of it and thought it'd be a good idea to just "wrap" a HTML page around it. For example in the output in the terminal there are 2 values gradually increasing and I'd like to process both of them in a way that they could represent 2 bars that, according to the 2 values, rise accordingly.
However, I've never done anything in JS and I don't really know the best way to do it.
I found http://iceddev.com/blog/node-js-in-chrome/ to host a local server to run my .js on this but not even the console.log or any errors in the code in the index.js (which is basically opening the server and contains the content you want to see when navigating to it in Chrome) show up in the Chrome console so I am wondering if there is something else I can do here.
To run your script in the browser I recommended you to look this package http://browserify.org/ it compile your js script into browser compatible js

Is there any Mac text editor with tail or watch files change feature?

Ideally I wanted to use this in TextMate but I didn't find any feature besides the Show Web Preview which is nice for the fact I can set the interval to update the page, but definitely doesn't work for watching any file and also apply syntax highlighting or any format.
One neat example of what I wanted to achieve is to simulate exactly the same behavior as CoffeScript Try Now feature where you can type in one side and see what the file would look like in javascript.
So ideally I would open my .coffee file and then run coffee --watch on terminal which will track any file change for that specific file, so I could just pop another window inside my text editor which will just keep updating the coffeescript .js generated file.
like this, where the window on the left shows the current file and the window on the right shows the file being watched with specific interval.
I am not sure if I was clear enought, if not, please just let me know..
but basically I just want to see in real time what happens to my files after run a specific script but with syntax highlighting and anything else possible.
I am just testing this kaleidoscope app, it is really nice the way the visualization works, no editing is possible neither syntax highlighting features though but it is really good, so it makes me think that something like this would be really nice:
cheers
Emacs can do both of these things (and you're probably better off running it as a Cocoa app).
ediff works similarly to Kaleidoscope (minus the diagonal lines connecting the two revisions) and does let you edit the files without disturbing the diff process. By default you get the versions above one another but you can press | to toggle to side-by-side and m to expand to the full screen width (unfortunately this doesn't work properly with multiple monitors, at least in the version of Emacs I'm using.)
To tail/auto-revert things, there's auto-revert-mode and auto-revert-tail-mode built into Emacs.
emacswiki.org is pretty good if you're trying to figure out how to do something in Emacs, as is (duh) Stack Overflow. Mastering Emacs is a relatively new blog which has some great articles. There's also M-x all-things-emacs which links to some useful screencasts.
You can open the log file in OSX's Console log viewer utility that is used to monitor system logs. Simple as that. It will not show you diff's but it does emulate the tail -f functionality.

Categories

Resources