Skip to content

Instantly share code, notes, and snippets.

@panzi
panzi / portable_endian.h
Last active Aug 19, 2020
This provides the endian conversion functions form endian.h on Windows, Linux, *BSD, and Mac OS X. You still need to use -std=gnu99 instead of -std=c99 for gcc. The functions might actually be macros. Functions: htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh. License: I hereby put "port…
View portable_endian.h
// "License": Public Domain
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
// an example on how to get the endian conversion functions on different platforms.
#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
@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');
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 / 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)
@panzi
panzi / 10_object_url.js
Created Nov 1, 2012
Save/download data generated in JavaScript (2)
View 10_object_url.js
if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) {
  mimetype = "application/octet-stream";
 }
 
 blob = builder.getBlob(mimetype);
 url = URL.createObjectURL(blob);
 window.open(url, '_blank', '');
}
@panzi
panzi / gist:7551381
Created Nov 19, 2013
Download currently playing HTML5 audio/video bookmarklet.
View gist:7551381
Create a new bookmark and set it's URL to this:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Breturn%20window.open(xs%5Bi%5D.currentSrc)%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
When media is playing using HTML5 audio/video you can click this bookmark to open a new tab/window with the media. Then to download it use the context menu action "Save As..." (right mouse button -> Save As...).
If you use Chrome/Safari/recent Opera (WebKit/Blink) you can do better(!) and immediately download the file using this bookmarklet instead:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Bvar%20a%3Ddocument.createElement('a')%3Ba.target%3D'_blank'%3Ba.download%3D''%3Ba.href%3Dxs%5Bi%5D.currentSrc%3Ba.click()%3Breturn%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
@panzi
panzi / escapeXml.js
Created Feb 18, 2012 — forked from atesgoral/escapeXml.js
Escape XML in JavaScript.
View escapeXml.js
var XML_CHAR_MAP = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
'"': '&quot;',
"'": '&apos;'
};
function escapeXml (s) {
return s.replace(/[<>&"']/g, function (ch) {
@panzi
panzi / getMedium.js
Last active Jan 14, 2020
Open clicked medium (image, video, audio, svg, ...) in a new tab. This handles transparent elements covering images or images via CSS background-image.
View getMedium.js
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
@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 / bitfield.js
Created Oct 4, 2014
JavaScript bit field class with Base64 serialization.
View bitfield.js
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);
}
You can’t perform that action at this time.