Last active
November 22, 2022 16:02
-
-
Save shrekuu/30c0eb6c8c1fccda59030113f984b72c to your computer and use it in GitHub Desktop.
Click 5 times of an item to enable debug mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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