Skip to content

Instantly share code, notes, and snippets.

@panzi
panzi / js_equals.py
Last active Sep 8, 2021
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.
View js_equals.py
# 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 Jul 31, 2021
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)`
View smart_formatter.py
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 Jul 5, 2021
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.
View pil2cv.py
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 Aug 11, 2020
Make a screenshot of a video.
View VideoScreenshot.js
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 Jul 22, 2020
Bookmarklet to re-enable context menus on sites that disable it.
View enable_context_menu.js
javascript:(function(pd){Event.prototype.preventDefault=function(){if(this.type!=='contextmenu')return%20pd.apply(this,arguments);};})(Event.prototype.preventDefault);void(0)
View YouTubeThumbs.js
(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 Oct 2, 2019
convert a video into a GIF with optimal palette
View mkgif.sh
#!/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
Created Apr 9, 2019
A bookmarklet that removes these nasty overlays from websites that would work perfectly fine without a login, but still want you to create one.
View no_blocking_overlay.js
javascript:Array.forEach(document.body.querySelectorAll("*"), e => { var s = getComputedStyle(e); if (s.position !== 'static' && s.position !== 'relative') { var r = e.getBoundingClientRect(); if (r.x <= 0 && r.y <= 0 && r.width >= window.innerWidth && r.height >= window.innerHeight) { e.remove(); } } }); document.body.style.overflow="auto";void(0)
@panzi
panzi / show_hide_passwords.js
Created Jan 19, 2019
Bookmarklets to make passwords in password fields readable and to hide them again.
View show_hide_passwords.js
// Show Passwords:
javascript:document.querySelectorAll('input[type=password]').forEach(e=>{e.type='text';e.setAttribute('data-io-github-panzi-password','true');});void(0)
// Hide Passwords again:
javascript:document.querySelectorAll('input[data-io-github-panzi-password=true]').forEach(e=>e.type='password');void(0)
@panzi
panzi / git-kmerge3.sh
Last active Nov 20, 2018
Merge files useing kdiff3. Using git mergetool merges files one by one, this opens all files in kdiff3 at once. TODO: somehow git add merged files after kdiff3 exists
View git-kmerge3.sh
#!/bin/bash
set -e
SUBDIRECTORY_OK=1
. "`git --exec-path`/git-sh-setup"
cd_to_toplevel
O=".git-kmerge3-tmp-$$"
list=$O/.git-kmerge3-list