Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / global.d.ts
Last active September 30, 2021 19:12
Some missing types in axios for NodeJS (types would be different in browser).
import axios from 'axios';
import { IncomingMessage } from 'http';
// browser version would use Uint8Array instead of Buffer and woudln't have stream/IncomingMessage
// since Buffer is a sub-class of Uint8Array you could use that in nodejs too, if you don't need any of the Buffer specific methods
declare module 'axios' {
interface AxiosInstance {
request(config: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
request(config: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
request(config: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
@panzi
panzi / omit.ts
Created September 24, 2021 17:47
Omit keys from object in a typesafe way.
export default function omit<T, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K> {
const obj2 = { ...obj };
for (const key of keys) {
delete obj2[key];
}
return obj2;
}
@panzi
panzi / js_equals.py
Last active September 8, 2021 03:59
Compare values using JavaScript semantics in Python. Not sure if I got everything right. This is more of a demonstration that JavaScript equality semantics are crazy.
# Copyright 2021 Mathias Panzenböck
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
@panzi
panzi / smart_formatter.py
Last active February 28, 2024 22:32
Python argparse help formatter that keeps new lines and doesn't break words (so URLs are preserved), but still wraps lines. Use with: `argparse.ArgumentParser(formatter_class=SmartFormatter)`
import argparse
from typing import List
class SmartFormatter(argparse.HelpFormatter):
def _split_lines(self, text: str, width: int) -> List[str]:
lines: List[str] = []
for line_str in text.split('\n'):
line: List[str] = []
line_len = 0
for word in line_str.split():
@panzi
panzi / pil2cv.py
Created July 5, 2021 16:00
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome.
from PIL import Image
import numpy as np
import cv2
def pil2cv(image: Image) -> np.ndarray:
mode = image.mode
new_image: np.ndarray
if mode == '1':
new_image = np.array(image, dtype=np.uint8)
@panzi
panzi / VideoScreenshot.js
Last active May 7, 2022 10:59
Make a screenshot of a video.
javascript:(function() {
function screenshot(video) {
var canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context = canvas.getContext('2d');
var now = new Date();
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
var url = canvas.toDataURL('image/png');
var link = document.createElement('a');
@panzi
panzi / enable_context_menu.js
Created July 22, 2020 21:17
Bookmarklet to re-enable context menus on sites that disable it.
javascript:(function(pd){Event.prototype.preventDefault=function(){if(this.type!=='contextmenu')return%20pd.apply(this,arguments);};})(Event.prototype.preventDefault);void(0)
(function() {
"use strict";
function padd(x) {
var x = String(x);
return x.length < 2 ? '0' + x : x;
}
var duration = +document.querySelector("video").duration;
var playerData = document.querySelector("ytd-watch-flexy").__data.playerData;
@panzi
panzi / mkgif.sh
Created October 2, 2019 22:49
convert a video into a GIF with optimal palette
#!/usr/bin/bash
input=/dev/stdin
output=/dev/stdout
slice=
palette="/tmp/mkgif-$$-palette.png"
fps=15
width=
crop=
@panzi
panzi / no_blocking_overlay.js
Last active November 28, 2021 03:00
A bookmarklet that removes these nasty overlays from websites that would work perfectly fine without a login, but still want you to create one. Create a new bookmark and paste the script source as the URL. (Use "Raw" to see only the script source for easy copy-paste.) When on a page with a blocking overlay click the bookmark.
javascript:(()=>{var d=document.documentElement,b=document.body,es=b.querySelectorAll("*"),m=20;for(var i=0;i<es.length;++i){var e=es[i];var s=getComputedStyle(e);if(s.position!=='static'&&s.position!=='relative'){var r=e.getBoundingClientRect();if(r.x<=m&&r.y<=m&&r.right>=window.innerWidth - m&&r.bottom>=window.innerHeight - m){e.remove();}}}var s='\n/**/;position:static!important;overflow:auto!important;width:auto!important;height:auto!important;';d.setAttribute('style',(d.getAttribute('style')||'')+ s);b.setAttribute('style',(b.getAttribute('style')||'')+ s);})();void(0)