Setting breakpoints dynamically at runtime in Javascript

99封情书 提交于 2019-12-06 11:51:42
jbalsas

I'd say you can definitely do this for webkit browsers using the remote debugging protocol. This is based on a websocket connection and a json message protocol that goes back and forth.

You can read the announcement and the whole protocol schema.

Chrome also offers more information about this inside its remote developer-tools docs.

For the debugger domain, for instance, you can see how you can use Debugger.setBreakpoint, Debugger.setBreakpointByUrl and Debugger.setBreakpointsActive to work with breakpoints.

On the other hand, Mozilla also seems to be working on this as you can see in https://developer.mozilla.org/en-US/docs/Tools/Debugger-API and https://wiki.mozilla.org/Remote_Debugging_Protocol though I don't know the completion status of it.

In this case, you can work with breakpoints using the Debugger.Script APIs setBreakPoint, getBreakPoint, getBreakpoints, clearBreakpoints and clearAllBreakpoints

I hope this helps you move forward.

There isn't such a thing, at least not using the public, scriptable side of JavaScript. It would be possible if you have a privileged browser extension that could do that for you. For example, Firebug has a debug method which you can call from its command line, but not from scripts inside a page.

So, you have two solutions:

  1. Implement your own JavaScript interpreter, which you can control as you wish. Might be a bit too ambitious, though...
  2. Rely on a browser extension that can set breakpoints anywhere in the code, expose some API to public code, and interact with it from your JavaScript. But that means that users will have to install some extra piece of software before they can use your "Web IDE".

Use _defineSetter__ to watch variables, and combine it with a call to debugger when an assignment happens.

__defineSetter__("name", function() { debugger; });

or defineProperty:

function setter () { debugger; }

Object.defineProperty(Math, 'name', { set: setter });

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!