鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ JS的閉包封裝,如何完成調(diào)用?

JS的閉包封裝,如何完成調(diào)用?

var protection = (function() {
    var data = {
        suffix: "com",
        main: "www.",
        red: "bai",
        beauty: "du",
        dot: "."
    }
    var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
    var url = function() {
        if (document.location.host != "www.baidu.com") {
            location.href = location.href.replace(document.location.host, 'www.baidu.com');
        }
        return location.href;
    }
    var authentication = function() {
        if (window.location.host.indexOf(d) < 0) {
            //$("body").remove();
            document.querySelector('html'). removeChild('body');
            return false
        }
        return true
    }
    var shield = function() {
        document.addEventListener('keydown', function(e) {
            e = window.event || e;
            var keycode = e.keyCode || e.which;
            //屏蔽Ctrl+s 保存頁面
            //
            //
            var disableCopy = function() {
                if (e.ctrlKey && keycode == 83) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disableSource = function() {
                //屏蔽Ctrl+u  查看頁面的源代碼
                if (e.ctrlKey && keycode == 85) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disableF12 = function() {
                //屏蔽F12
                if (keycode == 123) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disbaleConsole = function() {
                //屏蔽Ctrl+shift+i   屏蔽調(diào)出控制臺 和F12一樣
                if (e.ctrlKey && e.shiftKey && keycode == 73) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
        });
    }
    var facility = {
        geturl: url,
        checkurl: authentication,
        shield: shield
    }
    return facility;
})();

這段代碼應(yīng)該怎么修改才可以分別調(diào)用內(nèi)部的方法?

回答
編輯回答
假灑脫

你應(yīng)該是想調(diào)用 那些禁用鍵盤事件的方法

var protection = (function () {
    var data = {
        suffix: "com",
        main: "www.",
        red: "bai",
        beauty: "du",
        dot: "."
    }
    var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
    var url = function () {
        if (document.location.host != "www.baidu.com") {
            location.href = location.href.replace(document.location.host, 'www.baidu.com');
        }
        return location.href;
    }
    var authentication = function () {
        if (window.location.host.indexOf(d) < 0) {
            //$("body").remove();
            document.querySelector('html').removeChild('body');
            return false
        }
        return true
    }

    var shield = function (config) {
        shield.config = config;
        var disable = {
            disableCopy: function (e, keycode) {
                //屏蔽Ctrl+s 保存頁面
                if (e.ctrlKey && keycode == 83) {
                    console.log(shield.config)
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableSource: function (e, keycode) {
                //屏蔽Ctrl+u  查看頁面的源代碼
                if (e.ctrlKey && keycode == 85) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableF12: function (e, keycode) {
                //屏蔽F12
                if (keycode == 123) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableConsole: function (e, keycode) {
                //屏蔽Ctrl+shift+i   屏蔽調(diào)出控制臺 和F12一樣
                if (e.ctrlKey && e.shiftKey && keycode == 73) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            }
        }

        document.addEventListener('keydown', function (e) {
            e = window.event || e;
            var keycode = e.keyCode || e.which;
            for (var i = 0; i < shield.config.length; i++) {
                disable[shield.config[i]](e, keycode);
            }
        });
    }
    var facility = {
        geturl: url,
        checkurl: authentication,
        shield: shield
    }
    return facility;
})();

protection.shield(["disableCopy", "disableConsole"]);
//通過js控制
setTimeout(function(){
    protection.shield.config = ["disableCopy"];
},2000)
2017年6月8日 07:43
編輯回答
若相惜
var protection = (function(win) {
        var data = {
            suffix: "com",
            main: "www.",
            red: "bai",
            beauty: "du",
            dot: "."
        };
        var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
        var url = function() {
            if (document.location.host != "www.baidu.com") {
                location.href = location.href.replace(document.location.host, 'www.baidu.com');
            }
            return location.href;
        };
        var authentication = function() {
            if (window.location.host.indexOf(d) < 0) {
                //$("body").remove();
                document.querySelector('html'). removeChild('body');
                return false
            }
            return true
        };
        var shield = function(arr) {
            document.addEventListener('keydown', function(e) {
                e = window.event || e;
                var keycode = e.keyCode || e.which;

                var disableCopy = function() {
                    if (e.ctrlKey && keycode == 83) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disableSource = function() {
                    //屏蔽Ctrl+u  查看頁面的源代碼
                    if (e.ctrlKey && keycode == 85) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disableF12 = function() {
                    //屏蔽F12
                    if (keycode == 123) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disbaleConsole = function() {
                    //屏蔽Ctrl+shift+i   屏蔽調(diào)出控制臺 和F12一樣
                    if (e.ctrlKey && e.shiftKey && keycode == 73) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                }

                for(var i = 0; i < arr.length; i++){
                    eval(arr[i]+'()')
                }

            });
        };
        win.facility = {
            geturl: url,
            checkurl: authentication,
            shield: shield
        };

    })(window);
    facility.shield(['disableCopy','disableSource'])
2017年8月30日 11:23