Calling a Javascript Function from Console

后端 未结 5 2054
礼貌的吻别
礼貌的吻别 2020-12-23 16:18

In Chrome\'s JavaScript console, how do I call a function that belongs to a .js file included in the webpage I am viewing?

5条回答
  •  甜味超标
    2020-12-23 16:31

    I just discovered this issue. I was able to get around it by using indirection. In each module define a function, lets call it indirect:

    function indirect(js) { return eval(js); }
    

    With that function in each module, you can then execute any code in the context of it.

    E.g. if you had this import in your module:

    import { imported_fn } from "./import.js";
    

    You could then get the results of calling imported_fn from the console by doing this:

    indirect("imported_fn()");
    

    Using eval was my first thought, but it doesn't work. My hypothesis is that calling eval from the console remains in the context of console, and we need to execute in the context of the module.

提交回复
热议问题