How to disable JavaScript function calls from the browser console?

前端 未结 5 1498
执念已碎
执念已碎 2020-11-30 10:21

I\'m working on a web application in HTML/JavaScript, and I want to prevent users from calling functions in their browser console in order to avoid cheating. All these funct

5条回答
  •  时光说笑
    2020-11-30 10:44

    if (window.webkitURL) {
        var ish, _call = Function.prototype.call;
        Function.prototype.call = function () { //Could be wrapped in a setter for _commandLineAPI, to redefine only when the user started typing.
            if (arguments.length > 0 && this.name === "evaluate" && arguments [0].constructor.name === "InjectedScriptHost") { //If thisArg is the evaluate function and the arg0 is the ISH
                ish = arguments[0];
                ish.evaluate = function (e) { //Redefine the evaluation behaviour
                    throw new Error ('Rejected evaluation of: \n\'' + e.split ('\n').slice(1,-1).join ("\n") + '\'');
                };
                Function.prototype.call = _call; //Reset the Function.prototype.call
                return _call.apply(this, arguments);  
            }
        };
    }
    

提交回复
热议问题