在 Chrome 中内联 javascript 重定向之前中断 javascript

我正在查看一个包含内联 javascript 重定向(window.location = "/anotherpage")的页面。我想在 Chrome 中加载这个页面,但是禁用了重定向行,这样我就可以使用这个页面而不会被重定向。

以下是我试过的方法:

  • Developer tools -> Cog -> General -> Disable JavaScript. Load the page. It doesn't redirect (yay!). But I still want the rest of the page's javascript to run, and it hasn't.

  • Type the URL, then click Developer tools -> Sources -> Pause (F8) real fast! It hasn't redirected yet (yay!) Now I want to disable the redirect line before unpausing, but that part hasn't even loaded yet into Developer Tools. So I'll start stepping through the other files javascript code until I get there?? But as soon as I step out of the other files javascript, it immediately redirects away (doh!).

这可以做到吗? 我认为禁用一行 javascript 应该很容易,但我被难住了。

57668 次浏览

Developer Tools -> Sources -> Event Listener Breakpoints (on the right sidebar) -> Load -> check unload

This will make debugger break on unload event which is dispatched before navigation.

Do the following

  1. Open Developer Tools
  2. Go to Sources tab
  3. Look for Event Listener Breakpoints
  4. Expand Load option
  5. Here check unload option

Chrome unload breakpoint

I have a 3rd party JS library, which has had a wrong condition to reload the page. And the page has been reloaded continuously because of this. I tried to find where is the wrong code.

I tried to use the "Event Listener Breakpoints" method, but as a comment said you don't have stack trace in unload events, so it is pretty useless.

The solution which has worked for me: I created a page with an iframe tag with sandbox attribute, e.g. <iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe> and put my site in it. This way security errors will occur inside chrome and the console shows where the JS tries to access the location object. You can click on it and see the code. The best is Chrome has a JS decompressor (the {} button in the bottom left of the source window), which is clever, can show the line even after pretty printing, so you can see it even in compressed JS.

More info about sandbox property: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox