Skip to content

Instantly share code, notes, and snippets.

View librz's full-sized avatar
💭
Chilling

Patrick Ren librz

💭
Chilling
View GitHub Profile
@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
@librz
librz / native-fetch-wrapper.ts
Last active March 10, 2023 05:45
An exmaple of a wrapper on top of browser's native fetch method
/**
* example of a wrapper of browser's native fetch method
* Assumptions:
* 1. server uses JWT & bearer token for authorization
* 2. client stores auth token in local storage
* 3. response's content type
* a. "application/json" for normal response
* b. "application/octet-stream" for file download
* c. "text/plain" when error
* 4. server returns 401 when auth token is invalid
@librz
librz / setstate-is-async.tsx
Created December 29, 2022 10:12
Proof React's setState is asynchronous
import { useState, FC } from "react";
const App: FC = () => {
const [number, setNumber] = useState(1);
const nextNumber = number + 1;
function handleIncrease() {
setNumber((num) => num + 1);
console.log("current number is", number); // still the old number
@librz
librz / render-props.tsx
Last active December 14, 2022 04:37
Render props pattern in react
// ref: https://www.patterns.dev/posts/render-props-pattern/
// note: in many cases, render props can be replaced by hooks
import { FC, useState } from "react";
interface IContainerProps {
children: (bg: "silver" | "gold") => React.ReactNode;
}
const Container: FC<IContainerProps> = ({ children }) => {
@librz
librz / pubsub.ts
Last active December 4, 2022 01:28
naive example code for "topic based publish/subscribe model"
// naive example code for "topic based publish/subscribe model"
// this is just to illustrate the idea, feature is incomplete, do NOT use in production
// reference: https://github.com/mroderick/PubSubJS
type TopicHandler = (data?: any) => void;
interface ISubscriber {
topic: string;
handler: TopicHandler
}
@librz
librz / use-context.tsx
Last active February 2, 2023 09:39
Example of using context in react
/* define context */
import React, { createContext } from 'react'
// the biggest limitation of context is there's no way of refering to & setting state when defining context
// actions can only be implemented inside context provider
const LayoutContext = createContext<{
collapsed: boolean,
toggleCollapsed: () => void
}>({