Skip to content

Instantly share code, notes, and snippets.

@josephrocca
josephrocca / reddit-comment-stream.js
Last active March 26, 2023 15:47
Reddit Comment Stream (Node.js)
module.exports = function() {
let request = require("request-promise");
let stopFlag;
async function start() {
if(!this.oncomment) {
console.error("You must attach an oncomment handler (onerror handler is optional).");
return;
}
@josephrocca
josephrocca / createSparkline.js
Last active March 16, 2021 17:53
Simple sparklines with HTML5 canvas
function createSparkline(values, width=200, height=80, color='rgb(120,120,120)', maxValue) {
maxValue = maxValue || Math.max(...values);
values = values.map(v => (v/maxValue)); //normalise
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
ctx.fillStyle = color;
ctx.moveTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
@josephrocca
josephrocca / generate.js
Last active November 5, 2023 16:06
Generate map tiles from a large image with nodejs
// Given a large input image, this script generates map tiles of 256*256px
// at power-of-2 zoom levels. It's not super efficient or anything, just a
// quickly hacked together script.
//Use the tiles in leaflet.js like this:
/*
<div id='map' style='height:100%;'></div>
<script>
var map = L.map('map', {
attributionControl: false
@josephrocca
josephrocca / BigMap.js
Last active April 24, 2024 13:34
BigMap - wrapper to get past the ~16 million key limit on JavaScript Maps
// only covers a small subset of the Map api!
// haven't debugged yet!
class BigMap {
constructor(iterable) {
if(iterable) throw new Error("haven't implemented construction with iterable yet");
this._maps = [new Map()];
this._perMapSizeLimit = 14000000;
this.size = 0;
}
@josephrocca
josephrocca / background.js
Last active July 27, 2023 04:37
Chrome extension to force-enable CORS based on request's *source* url (i.e. the url of the browser tab) rather than the target url
// Notes: we need the `sourceTabUrl &&` before the URL check because chromebooks make weird requests that don't come from "real" tabs.
let accessHeaders = new Map();
let tabIdToUrlMap = new Map();
let requestListener = function (details) {
const accessControlRequestHeader = details.requestHeaders.find(elem => elem.name.toLowerCase() === "access-control-request-headers");
if(accessControlRequestHeader) {
accessHeaders.set(details.requestId, accessControlRequestHeader.value);
}
@josephrocca
josephrocca / getImageData.js
Last active February 25, 2018 06:04
Get image data (pixel RGB array, width, height) by URL
// Only works for images hosted on the same domain unless CORs headers of image allow you to use it.
// Usage:
// let data = await getImageData("http://example.com/image.png");
// Put it in a try/catch to handle errors or use await getImageData("...").catch(e => ...);
function getImageData(url) {
return new Promise((resolve, reject) => {
let img = new Image();
img.onload = function() {
let canvas = document.createElement("canvas");
canvas.width = img.width;
@josephrocca
josephrocca / vivagraph-0.10.0-webglImage-fixed.js
Created February 26, 2018 05:58
Vivagraph 0.10.0 webglImageNodeProgram weird mirroring bug fixed
// Fixed by @weequ: https://github.com/anvaka/VivaGraphJS/pull/204/files
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Viva=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* This is an entry point for global namespace. If you want to use separate
* modules individually - you are more than welcome to do so.
*/
@josephrocca
josephrocca / massive-phrase-list.json
Created June 13, 2018 12:51
A massive list of phrases scraped from the web
This file has been truncated, but you can view the full file.
[{"phrase":"A bad excuse is better than none","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A bad penny always turns up","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A bad tree does not yield good apples","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A bad workman blames his tools","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A barking dog seldom bites","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A bird in hand is worth two in a bush","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A black plum is as sweet as a white","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A book holds a house of gold","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A broken friendship may be soldered but will never be sound","source":"https://www.learn-english-today.com/proverbs"},{"phrase":"A burden of one's own choice is not felt","source":"https://www.learn-e
@josephrocca
josephrocca / streaming-decompress-bz2.html
Created July 13, 2018 09:21
Streaming bz2 decompression in browser (newline delimited JSON)
<script src="https://unpkg.com/unbzip2-stream@1.2.5/dist/unbzip2-stream.min.js"></script>
<script>
async function loadBzipJson(url, cb) {
let text = "";
let bz2 = window.unbzip2Stream();
bz2.on('data', (d) => {
text += new TextDecoder('utf-8').decode(d);
while(text.includes("\n")) {
cb(JSON.parse(text.slice(0, text.indexOf("\n"))));
text = text.slice(text.indexOf("\n")+1);
@josephrocca
josephrocca / frequencies-of-pairs-of-letters-in-english.json
Last active July 31, 2018 03:17
frequencies of pairs of letters in english [a-z0-9'\-], using reddit comments as corpus
{"10":9331,"11":3452,"12":3099,"13":1741,"14":1919,"15":3362,"16":4020,"17":4212,"18":1621,"19":1884,"20":12579,"21":1041,"22":1105,"23":1044,"24":1336,"25":2064,"26":797,"27":881,"28":844,"29":718,"30":3417,"31":1005,"32":855,"33":736,"34":671,"35":1088,"36":870,"37":594,"38":611,"39":487,"40":2369,"41":572,"42":588,"43":516,"44":655,"45":921,"46":496,"47":541,"48":762,"49":542,"50":4149,"51":584,"52":713,"53":535,"54":471,"55":694,"56":601,"57":501,"58":430,"59":482,"60":2207,"61":544,"62":408,"63":431,"64":627,"65":807,"66":570,"67":447,"68":468,"69":463,"70":1433,"71":420,"72":532,"73":376,"74":432,"75":862,"76":450,"77":463,"78":460,"79":372,"80":2025,"81":495,"82":443,"83":493,"84":416,"85":606,"86":445,"87":386,"88":493,"89":444,"90":1408,"91":521,"92":503,"93":460,"94":451,"95":600,"96":587,"97":549,"98":645,"99":8867,"be":129751,"ei":31624,"il":83287,"le":165234,"id":63569,"d ":470592," k":43419,"ki":45808,"iw":377,"wi":84647,"i ":154196," f":221481,"fe":45633,"er":346337,"rn":20542,"ns":64040,"se":1