線上環(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)
}
})()