隱藏vconsole小把戲,幸福感爆棚

線上環(huán)境有時(shí)候也需要啟用vconsole來調(diào)試程序,來回部署又有些不方便
想了一個(gè)好玩的辦法來隱藏vconsole
想要設(shè)置一個(gè)debug開關(guān),在開啟之后呢,我就一直處于debug模式可以使用vconsole
保持狀態(tài)可以使用localstorage
開關(guān)我用的是觸摸事件輸入摩爾斯碼開啟的
大改是這個(gè)邏輯
1.設(shè)置個(gè)存放正確密碼在success數(shù)組,和存放輸入密碼的action數(shù)組
2.監(jiān)聽觸摸的時(shí)間長(zhǎng)短來判斷是tap還是press
3.輸入的這位對(duì)應(yīng)success對(duì)應(yīng)位置的密碼正確,就放進(jìn)action數(shù)組,錯(cuò)誤就清空action數(shù)組
4.當(dāng)success數(shù)組等于action數(shù)組時(shí),就開啟開關(guān)啦

(function () {

    var _YK_VC_OPEN_ = '_YK_VC_OPEN_'

    var shouldOpen = localStorage.getItem(_YK_VC_OPEN_) === _YK_VC_OPEN_

    if (shouldOpen) {
        open()
    } else {
        lock()
    }

    function open() {
        var vConsole = new VConsole();
    }

    function lock() {
        /*
                打開條件
                V   ...━
                C   ━ .━ .
            */
        var TYPES = {
            tap: 'tap',
            press: 'press'
        }
        // 開啟條件
        var success = [
            TYPES.tap,
            TYPES.tap,
            TYPES.tap,
            TYPES.press,
            TYPES.press,
            TYPES.tap,
            TYPES.press,
            TYPES.tap
        ]
        // action策略
        var Strategy = {
            [TYPES.tap]: function onTap() {
                actionList.push(TYPES.tap)
            },
            [TYPES.press]: function onPress() {
                actionList.push(TYPES.press)
            }
        }
        // action記錄
        var actionList = []
        var tapDuration = [0, 500]
        var pressDuration = [3000, 10000]

        var lastDownTime = 0

        function onAction(currType) {
            try {
                const action = Strategy[currType]
                if (typeof action !== 'function') {
                    throw new Error('不正確的點(diǎn)擊')
                }
                if (!check(currType)) {
                    throw new Error('輸入錯(cuò)誤')
                }
                action()
                // console.log('鍵入', currType)
                if (actionList.length === success.length) {
                    unlock()
                }
            } catch (e) {
                // console.log(e.message)
                actionList = []
            }
        }

        function check(currType) {
            const curIndex = actionList.length
            return currType === success[curIndex]
        }

        function onDown() {
            // console.log('down')
            lastDownTime = Date.now()
        }

        function onUp(e) {
            var duration = Date.now() - lastDownTime
            // console.log('up', duration)
            if (duration < tapDuration[1]) {
                onAction(TYPES.tap)
            } else if (duration > pressDuration[0] && duration < pressDuration[1]) {
                onAction(TYPES.press)
            } else {
                onAction('other')
            }
        }

        function unlock() {
            alert('行,你破解了密碼,進(jìn)入debug模式吧')
            window.removeEventListener('touchstart', onDown)
            window.removeEventListener('touchend', onUp)
            // 存localstorage
            localStorage.setItem(_YK_VC_OPEN_, _YK_VC_OPEN_)
            open()
        }

        window.addEventListener('touchstart', onDown)
        window.addEventListener('touchend', onUp)
    }
})()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,268評(píng)論 6 342
  • 轉(zhuǎn)載請(qǐng)注明出處(http://www.itdecent.cn/p/5f538820e370),您的打賞是小編繼續(xù)...
    福later閱讀 29,700評(píng)論 8 73
  • 小程序巧應(yīng)用,微信小程序開發(fā)實(shí)戰(zhàn) 配置 app.json pages -> 所有的頁(yè)面 window -> 窗口的...
    Anancncn閱讀 608評(píng)論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • # 云計(jì)算實(shí)驗(yàn) 如果說亞馬遜的AWS(Amazon Web Service)是一個(gè)IaaS平臺(tái),為用戶提供計(jì)算服務(wù)...
    喲桑_6481閱讀 697評(píng)論 0 0

友情鏈接更多精彩內(nèi)容