Skip to content

Instantly share code, notes, and snippets.

View happyincent's full-sized avatar

Vincent Chen happyincent

View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Target Name="PublishHash" AfterTargets="AfterPublish">
<ItemGroup>
<FilesToHash Include="$(PublishDir)**" Exclude="$(PublishDir)*.sha256;" />
</ItemGroup>
<GetFileHash Files="@(FilesToHash)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="Hashes" />
</GetFileHash>
[ -f /tmp/mnt/DDL/D/tomato/syncthing-linux-arm/syncthing ] && /tmp/mnt/DDL/D/tomato/syncthing-linux-arm/syncthing --gui-address=192.168.2.1:8384 --home=/tmp/mnt/DDL/D/tomato/syncthing-linux-arm/home
#>/tmp/mnt/DDL/D/tomato/syncthing-linux-arm/log.txt 2>&1
@happyincent
happyincent / ToDbString.cs
Created April 1, 2023 15:03
Convert string to Dapper's DbString with StringLengthAttribute and UnicodeAttribute.
public static DbString ToDbString<T>(this string value, string key, bool? IsFixedLength, int? MaxLength, bool? IsAnsi)
{
var property = typeof(T).GetProperty(key);
var attrLength = property?.GetCustomAttributes<StringLengthAttribute>().SingleOrDefault();
var maxLength = MaxLength ?? attrLength?.MaximumLength ?? DbString.DefaultLength;
var attrAnsi = property?.GetCustomAttributes<UnicodeAttribute>().SingleOrDefault();
var isAnsi = IsAnsi ?? attrAnsi?.IsUnicode switch
{
@happyincent
happyincent / yupHelper.ts
Created February 16, 2023 07:33
A workaround to get autocomplete work in yup.object with Partial keys of an existing type.
type Shape<T, K extends keyof T> = { [key in K]?: yup.ISchema<T[key]> | ReturnType<typeof yup.ref<T[key]>> };
export type YupObjectType<T> = Pick<T, keyof T>;
export type YupObjectShape<T> = Shape<T, keyof T>;
// Usage: yup.object<YupObjectType<T>, YupObjectShape<T>>({ ... })
@happyincent
happyincent / lockThenRun.ts
Created January 13, 2023 18:21
Lock then run the Promise only in one window/tab with Web Locks API (native/polyfill).
if (!window.isSecureContext) import("navigator.locks");
async function lockThenRun<T>(name: string, callback: (lock: Lock | null) => Promise<T>) {
return await window.navigator.locks
.request(name, { ifAvailable: true }, async (lock) => {
if (lock === null) throw new DOMException("The lock was not granted.");
await new Promise((resolve) => setTimeout(resolve, Math.random() * 100));
return lock;
})
.then(callback)
@happyincent
happyincent / chromeurl.reg
Last active October 30, 2021 16:53
URL Scheme to open Chrome
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\chromeurl]
@="URL:Google Chrome Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\chromeurl\Shell]
[HKEY_CLASSES_ROOT\chromeurl\Shell\Open]
[HKEY_CLASSES_ROOT\chromeurl\Shell\Open\Command]
@="cmd /V /C \"set \"URL=%1\" && set URL=!URL:^&=^^^&! && set URL=!URL:chromeurl://=! && cmd /c \"start chrome !URL!\" \""
; chromeurl://https://www.google.com/search?q=google&oq=google
@happyincent
happyincent / plates.js
Last active June 22, 2021 04:11
車牌號碼吉凶判斷
// Ref1: https://www.mvdis.gov.tw/m3-emv-plate/webpickno/queryPickNo
// Ref2: http://tanzih.blogspot.com/2013/09/4-80-80-yw-6675-6675-80-83.html
// Step0: Fill the form and Reset storage in `Ref1`
localStorage.removeItem("plates")
// Step1: Keep running until ERROR (final page)
localStorage.setItem(
"plates",
JSON.stringify(
@happyincent
happyincent / encode.py
Last active September 24, 2020 08:28
encode.py (https://file.io)
#! python3
import base64, binascii, json, urllib.request
s1 = 'Hello, World!'
code = base64.b64encode(binascii.hexlify(s1[::-1].encode('utf-8'))).decode('utf-8')
s2 = bytes.fromhex(base64.b64decode(code).decode('utf-8')).decode('utf-8')[::-1]
assert s1 == s2
link = json.loads(urllib.request.urlopen("https://file.io", data='text={}'.format(code).encode('utf-8')).read())['link']
link = base64.b64encode(link.encode('utf-8')).decode('utf-8')
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
Get-AppxPackage -AllUsers | where-object {
$_.name -notlike "*Store*" -And
$_.name -notlike "*Calculator*" -And
$_.name -notlike "*ScreenSketch*"
} | Remove-AppxPackage
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');