Skip to content

Instantly share code, notes, and snippets.

@shrekuu
Last active November 22, 2022 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrekuu/30c0eb6c8c1fccda59030113f984b72c to your computer and use it in GitHub Desktop.
Save shrekuu/30c0eb6c8c1fccda59030113f984b72c to your computer and use it in GitHub Desktop.
Click 5 times of an item to enable debug mode
import * as Cookies from 'js-cookie'
import { fromEvent } from 'rxjs'
import { buffer, debounceTime, filter, map } from 'rxjs/operators'
// debug 模式配置
// 连续 5 次快速点击, 切换 debug 模式
const FAST_CLICK_COUNT = 5
const DEBUG_COOKIE_KEY = 'debug'
const clickStream = fromEvent(document, 'click')
// TODO: Cookies.getJSON is not available in latest js-cookie, hahahaha
console.log('debug mode: ', Cookies.getJSON(DEBUG_COOKIE_KEY))
clickStream.pipe(
buffer(clickStream.pipe(debounceTime(250))),
map(function (arr) {
return arr.length
}),
filter(function (len) {
return len === FAST_CLICK_COUNT
})
).subscribe(function (event) {
let debug = Cookies.getJSON(DEBUG_COOKIE_KEY) || false
if (debug || (!debug && confirm('打开 debug 模式'))) {
if (!debug) {
Cookies.set(DEBUG_COOKIE_KEY, true)
} else {
Cookies.remove(DEBUG_COOKIE_KEY)
}
location.href = '/'
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment