Skip to content

Instantly share code, notes, and snippets.

View gh640's full-sized avatar
🍉

Goto Hayato gh640

🍉
View GitHub Profile
@gh640
gh640 / use_mysqltuner_in_mariadb_container.sh
Last active November 19, 2024 18:27
Sample: Use MySQLTuner in `mariadb` Docker container
# MySQLTuner: https://github.com/major/MySQLTuner-perl
# Open Bash with the container.
docker exec [mariadb_or_mysql] bash
# Change the working directory to `/tmp`.
cd /tmp
# Install wget.
apt-get update
@gh640
gh640 / searchSelectedTextinAmazonJpBookmarklet.js
Last active November 16, 2024 22:11
選択した文字列をAmazon.co.jpで検索するブックマークレット。
javascript:(function(){
var q, location;
/* 選択された文字列を取得 */
/* 選択されてなければダイアログから文字列を取得 */
q = ""+(window.getSelection?window.getSelection():document.getSelection());
if(!q) q = prompt("Amazon search... ","");
/* urlエスケープした上でAmazonにリクエスト */
@gh640
gh640 / asyncio_streaming_example.py
Created December 12, 2021 02:45
Python: Stream output of `asyncio.create_subprocess_exec()`
"""Stream output of `asyncio.create_subprocess_exec()`"""
import asyncio
import sys
async def run(program: str, args: list[str]):
"""Capture output (stdout and stderr) while running external command."""
proc = await asyncio.create_subprocess_exec(
program, *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
@gh640
gh640 / send_message_on_google_chat.py
Last active October 16, 2024 10:33
Sample: Send a message on Google Chat group with Python `requests`
"""A sample to send message on Google Chat group with Python requests.
Prerequisites:
- Google API v1
- A webhook URL taken
- Python 3
- Requests (last tested with 2.31.0)
Usage:
@gh640
gh640 / confirm.py
Created October 11, 2024 01:29
Python: confirm
def confirm(message: str) -> bool:
answer = input(f"{message} [yes/No]: ").strip().lower()
return answer in ["y", "yes"]
@gh640
gh640 / card.js
Created April 25, 2021 06:39
Sample: Send a message on Google Chat with Node `axios`
const axios = require(`axios`)
// Pass incoming webhook URL with environment variable `WEBHOOK_URL`.
const WEBHOOK_URL = process.env.WEBHOOK_URL
const title = `Hi`
const subtitle = `Hello`
const paragraph = `Hasta La Vista, Baby.`
const widget = { textParagraph: { text: paragraph } }
axios.post(WEBHOOK_URL, {
@gh640
gh640 / functions.php
Created September 5, 2024 04:13
WordPress: Font Library でオリジナルのフォントコレクションを追加する
<?php
add_action( 'init', 'themeslug_add_font_collections' );
/**
* フォントコレクションを追加する
*/
function themeslug_add_font_collections() {
// フォントファミリーの定義:
@gh640
gh640 / functions.php
Created September 5, 2024 04:12
WordPress: Use user ids as subfolders for uploaded media files
<?php
add_filter( 'upload_dir', 'themeslug_upload_dir' );
/**
* Use user ids as subfolders for uploaded media files.
*/
function themeslug_upload_dir( $uploads ) {
$user_id = get_current_user_id() ?: 'anonymous';
@gh640
gh640 / useElementSize.js
Last active September 3, 2024 15:36
React hook to get size of an element
import { useState, useEffect, useRef } from 'react'
function useElementSize() {
const ref = useRef(null)
const [size, setSize] = useState({
width: 0,
height: 0,
})
useEffect(() => {
@gh640
gh640 / functions.php
Last active August 30, 2024 05:16
WordPress: Block Bindings API block samples
<?php
add_action( 'init', function () {
register_meta(
'post',
'book-genre',
[
'show_in_rest' => true,
'single' => true,