Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / format.js
Created September 14, 2013 03:41
Very simple string interpolation in JavaScript. {} expands to positional arguments, {NAME} to named arguments, {{ to {, and }} to }.
function format (fmt, kwargs) {
var args = arguments;
var index = 1;
return fmt.replace(/{([^{}]*)}|{{|}}|{|}/g, function (match, key) {
if (key !== undefined) {
if (key) {
return kwargs[key];
}
else {
@panzi
panzi / show_hide_passwords.js
Created January 19, 2019 19:04
Bookmarklets to make passwords in password fields readable and to hide them again.
// 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 / new_ecmascript.html
Last active November 27, 2021 08:36
Pseudo synchronously load scripts that use new ECMAScript syntax in Chrome and Firefox.
<!--
This is useful in debug mode. For production you want to compile your
scripts down to an signle file and an old ECMAScript version.
-->
<script type="application/javascript;version=1.8">
// Firefox needs application/javascript;version=1.7 or ...;version=1.8
// for new ECMAScript syntax features. It will also include Mozilla-only
// syntax extensions, so be careful not to accidentally use those.
// However, other browsers don't understand that script type.
@panzi
panzi / bitfield.js
Created October 4, 2014 04:43
JavaScript bit field class with Base64 serialization.
var BitField = (function () {
"use strict";
function BitField (bits) {
if (Array.isArray(bits)) {
return BitField.fromJSON(bits);
}
else if (typeof bits === "string") {
return BitField.fromBase64(bits);
}
@panzi
panzi / 00_head.html
Created November 1, 2012 04:58
Save/download data generated in JavaScript (1)
<!DOCTYPE html>
<html>
<head>
<title>Save Generated Data</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1"/>
<script type="text/javascript">
// <![CDATA[
@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 / escapeXml.js
Created February 18, 2012 04:16 — forked from atesgoral/escapeXml.js
Escape XML in JavaScript.
var XML_CHAR_MAP = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
'"': '&quot;',
"'": '&apos;'
};
function escapeXml (s) {
return s.replace(/[<>&"']/g, function (ch) {
@panzi
panzi / getMedium.js
Last active January 14, 2020 02:00
Open clicked medium (image, video, audio, svg, ...) in a new tab. This handles transparent elements covering images or images via CSS background-image.
javascript:void(!function(){function e(e){for(var t=null,n=null,r=null,l=0;l<e.length;++l){var i=e[l].srcset;if(i){i=i.split(",");for(var a=0;a<i.length;++a){var o=i[a].trim().split(/\s+/g),u=o[1];if(u){var c=/^(\d+(?:\.\d*)?)([a-z])$/i.exec(u),s=c[2],d=+c[1];(s!==t||d>n)&&(t=s,n=d,r=o[0])}else r||(r=o[0],n=t=null)}}}return r?{url:r}:null}function t(t){switch(t.tagName.toUpperCase()){case"IMG":return e([t])||(t.currentSrc?{url:t.currentSrc}:t.src?{url:t.src}:null);case"CANVAS":return{url:t.toDataURL(),html:'<img src="'+t.toDataURL()+'"/>'};case"VIDEO":case"AUDIO":return t.currentSrc?{url:t.currentSrc}:null;case"SVG":var n=t.outerHTML.replace(/^<svg\b/i,e=>e+' xmlns="http://www.w3.org/2000/svg"');return{html:n,url:"data:image/svg+xml,"+encodeURIComponent(n)};case"IFRAME":if(t.src&&"about:blank"!==t.src)return{url:t.src};case"PICTURE":return e(t.querySelectorAll("source"));case"IMAGE":if("http://www.w3.org/2000/svg"===t.namespaceURI)return{url:t.href.baseVal};default:var r=getComputedStyle(t,null).backgroundIma