Semi-sandboxing Javascript eval

前端 未结 4 2205
-上瘾入骨i
-上瘾入骨i 2021-01-12 16:03

Background: I\'m working on a framework/library to be used for a specific site in coordination with greasemonkey/userscripts. This framework/library will al

4条回答
  •  醉话见心
    2021-01-12 16:58

    If all you want is a simple getter, program one instead of trying to eval anything.

    function get(input) {
        // if input is a string, it will be used as a DOM selector
        // if it is an array (of strings), they will access properties of the global object
        if (typeof input == "string")
            return document.querySelector(input);
        else if (Array.isArray(input)) {
            var res = window;
            for (var i=0; i

提交回复
热议问题