Skip to content

Instantly share code, notes, and snippets.

View librz's full-sized avatar

Patrick Ren librz

View GitHub Profile
@librz
librz / groupByKeys.js
Created January 29, 2024 19:13
用 object 的 key 对 array of objects 进行分组
const list = [
{ province: '天津', city: '南开', district: '三马路' },
{
province: '天津',
city: '南开',
district: '四马路'
},
{
province: '天津',
city: '河西',
@librz
librz / crawl_menu_urls.js
Last active October 8, 2023 09:10
Example of crawling 2-level deep menus & asynchronously get their URLs
/*
note: this is not a general purpose crawling script & can be only used as a reference
background:
1. target site has a side bar which contains many level-1 menus, each level-1 menu contains many level-2 menus
2. level-1 menu stores category name; level-2 menu stores system name, upon click will fire related event & load content as an iframe
*/
const sideMenu = document.querySelector("#side-menu");
const menus = Array.from(sideMenu.querySelectorAll(":scope > li:has(> ul.mm-collapse)"));
@librz
librz / rename_files.js
Last active September 8, 2023 20:19
Batch rename files (using regex with capture group)
// this script does not handle file suffix for you
// as some files have multiple suffix segments, e.g: *.sc.ass & *.tc.ass are two common subtitle formats
// instead, you should manually specify suffix in regex as the last capture group
const fs = require("fs")
const isDryrun = !process.argv.includes("-r");
if (isDryrun) {
console.log("Note: By default, this script will only dry run, specify -r if you want to actually run it.")
@librz
librz / NotFound.html
Last active August 17, 2023 01:41
404 page using tailwind
<section className="h-full w-full flex justify-center items-center gap-8 p-6 flex-col md:flex-row">
<div className="rounded-full w-36 h-36 bg-black font-semibold flex justify-center items-center text-6xl text-white">
404
</div>
<div className="flex flex-col items-end">
<div className="text-5xl">Oops</div>
<div className="text-2xl mt-2 mb-6 text-end">This page doesn't exist</div>
<span className="bg-black text-white px-4 py-2 rounded-md cursor-pointer">
Home
</span>
@librz
librz / ErrorFallback.tsx
Created August 15, 2023 20:48
React Error Fallback Component
import { FallbackProps } from "react-error-boundary";
import React, { CSSProperties } from "react";
const ErrorFallback: React.ComponentType<FallbackProps> = ({
error,
resetErrorBoundary,
}) => {
const sectionStyle: CSSProperties = {
display: "flex",
flexDirection: "column",
@librz
librz / prevent_dev_tools.js
Created August 4, 2023 21:29
Evil hack that prevents user from inspecting web page
function check() {
const minimalUserResponseInMiliseconds = 200;
console.clear();
let before = new Date().getTime();
debugger;
let after = new Date().getTime();
if (after - before > minimalUserResponseInMiliseconds) {
document.write(" Don't open Developer Tools.");
window.location.reload();
}
@librz
librz / char_byte_count.js
Last active August 4, 2023 21:30
read a file, print char & byte count using nodejs
const fs = require('fs');
fs.readFile('content.txt', 'utf-8', (err, data) => {
if (err) {
console.log(err)
return;
}
let charsCount = 0;
let totalBytes = 0
Array.from(data).forEach(char => {

direct use of /v1/rule/targets:

1. <AlarmSettingModal /> (Alarm Mute Settings)

only devices are used:

devices.map(device =>
  <Option key={device.mac}>
    <Icon type={getDeviceIcon(device)} />
@librz
librz / url.ts
Last active March 10, 2023 04:17
Manipulate URL using JavaScript
// this works but is quite messy and error prone
function getUrl(server: string, path: string, name: string, age: number): string {
const trimmedServer = server.endsWith("/") ? server.slice(0, -1) : server; // remove trialing slash if any
const origin = trimmedServer.startsWith("http") ? trimmedServer : `https://${trimmedServer}`; // add protocol if needed
const trimmedPath = path.endsWith("/") ? path.slice(0, -1) : path; // remove trialing slash if any
const realPath = trimmedPath.startsWith("/") ? trimmedPath : `/${trimmedPath}`; // add slash to begining if needed
const params = new URLSearchParams({
name: name,
age: String(age)
@librz
librz / home.tsx
Last active January 18, 2023 05:57
Preload images(e.g. logo & background image) in Create React App
import { FC } from "react";
const Home: FC = () => {
return (
<div>
<header>
<img src="/images/site-logo.svg" alt="logo" />
</header>
<main