When paused on a statement within the browser's dev tools, how to terminate execution immediately after that statement?

╄→尐↘猪︶ㄣ 提交于 2020-05-14 17:55:32

问题


Let's say I have this function:

function test () {

    // statements 1

    statement_X;

    // statements 2

}

I'm stepping trough the statements with the browser's dev tools. Now, when I'm paused at "statement_X", I would like to terminate the function execution (I don't want the "statements 2" part of the function to be executed), as if the "statement_X" is immediately followed by a return; statement.

I know that Chrome has inline script editing, so I could manually add the return statement after the paused statement and then hit CTRL+S to re-execute the entire thing, but I need this feature for IE too, so I am hoping for a general solution.

Terminating execution early seems like an easy enough thing to do (for the browser), so I expect such a functionality from the dev tools.

enter image description here


回答1:


I tested this successfully in IE 9, so I'm posting it here as an answer: while pausing at statement_X in the script debugger, hit F10 so statement_X is still executed, then right click on the last line of the enclosing function (the line with the right curly bracket } that terminates the function body), and select "Set next statement" from the drop down menu. This will skip execution until the end of the function as if there were a void return statement just after statement_X.

If there are any other statements on the last line of a function as shown in the code below, be careful to right click just on the curly bracket for this technique to work.

function test () { alert("statement 1");
    alert("statement 2"); } function test2 () { alert("statement 3"); }

This may be sometimes necessary in the case of inline functions, or in minified scripts not intended for debugging.




回答2:


If I understand correctly, you can't do this.

The debuggers (Chrome's debugger at any rate) are themselves javascript-based.

These guys use eval()(eventually) to run the injected Code. Trawling through Chrome inspector, it seems that the debugger code eventually calls this when you try to evaluate something (I think):

function (evalFunction, object, expression, isEvalOnCallFrame, injectCommandLineAPI)
{
    // Only install command line api object for the time of evaluation.
    // Surround the expression in with statements to inject our command line API so that
    // the window object properties still take more precedent than our API functions.

    try {
        if (injectCommandLineAPI && inspectedWindow.console) {
            inspectedWindow.console._commandLineAPI = new CommandLineAPI(this._commandLineAPIImpl, isEvalOnCallFrame ? object : null);
            expression = "with ((window && window.console && window.console._commandLineAPI) || {}) {\n" + expression + "\n}";
        }
        return evalFunction.call(object, expression);
    } finally {
        if (injectCommandLineAPI && inspectedWindow.console)
            delete inspectedWindow.console._commandLineAPI;
    }
}

evalFunction is just an alias for eval().

The issue is, we cannot use return statements in eval, even in hard code. It will always give you SyntaxError: Illegal return statement.

So no, no voodoo return statement.



来源:https://stackoverflow.com/questions/10229996/when-paused-on-a-statement-within-the-browsers-dev-tools-how-to-terminate-exec

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