Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / export_local_storage.js
Created March 6, 2024 18:51
Bookmarklets to export/import localStorage. Import does *not* clean localStorage, only sets the loaded values.
// bookmarklet:
// javascript:(function()%7Bconst%20blob%3Dnew%20Blob(%5BJSON.stringify(localStorage)%5D%2C%7Btype%3A'application%2Fjson'%7D)%3Bconst%20link%3Ddocument.createElement('a')%3Bconst%20url%3DURL.createObjectURL(blob)%3Blink.href%3Durl%3Blink.download%3D'local_storage.json'%3Blink.style.display%3D'none'%3Bdocument.body.appendChild(link)%3Blink.click()%3BsetTimeout(()%3D%3E%7BURL.revokeObjectURL(url)%3Bdocument.body.removeChild(link)%3B%7D%2C250)%3B%7D)()%3B
function exportLocalStorage(){
const blob = new Blob([JSON.stringify(localStorage)],{type:'application/json'});
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.href = url;
link.download = 'local_storage.json';
link.style.display='none';
document.body.appendChild(link);
@panzi
panzi / truthiness.md
Last active September 14, 2023 12:35
Table of truthiness values in some dynamically typed programming languages.
Value Ruby Python JavaScript PHP Perl
"" true False false false false
"0" true True true false false
" " true True true true true
0 true False false false false
NaN true True false true true
[] true False true false true
{}/new \stdClass true False true true true
nil/null/None false False false false -
@panzi
panzi / python-c-quine.c
Last active February 14, 2023 22:33
The first file is a polyglot C and Python program that prints its own source code, i.e. a quine. This means you can run it as a Python script, but you can also compile it as a C program. The second file is the same, but in C and Ruby.
#include<stdio.h>
#define len int main(){char*
#define zip return 0;}
#if 0
def printf(f,*a):print(f%a,end=str())
#endif
len
s="#include<stdio.h>%c#define len int main(){char*%c#define zip return 0;}%c#if 0%cdef printf(f,*a):print(f%%a,end=str())%c#endif%clen%cs=%c%s%c;printf(s,10,10,10,10,10,10,10,34,s,34,10);zip%c";printf(s,10,10,10,10,10,10,10,34,s,34,10);zip
@panzi
panzi / rekey.py
Last active March 9, 2024 05:43
Re-key all the embedded vaults in an Ansible vars file.
#!/usr/bin/env python3
# derived from https://stackoverflow.com/a/67161907/277767
# Changes to the StackOverflow version:
# * delete temporary files that contain vaults!
# * prompt for passwords instead of passing them as program argument
# * more precise vault replacement
# * a bit nicer error messages that points at the line where re-keying failed
# * decryption if no password is provided
@panzi
panzi / preproc.js
Created October 12, 2022 22:27
A very simple pre-processor that I use in some build step. Could be more efficient and feature rich, but that is not the scope of this.
/**
* Very simple pre-processor.
*
* Syntax:
*
* #if FLAG
* ...
* #elif OTHER_FLAG
* ...
* #else
@panzi
panzi / database-url.ts
Last active August 30, 2022 20:26
Simple parser for common database URLs. (MIT License)
// Copyright 2022 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
@panzi
panzi / jqlog.sh
Created April 6, 2022 21:06
Follow JSON logs formatted using jq. You can also pass jq options after the filename for filtering.
#!/usr/bin/bash
set -eo pipefail
RED=$(echo -e '\033[0;1;31m')
NORMAL=$(echo -e '\033[0m')
if [[ $# -lt 1 ]]; then
echo "usage: $0 <logfile> [jq-options...]">&2
exit 1
fi
logfile=$1
shift
@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