Skip to content

Instantly share code, notes, and snippets.

View berdosi's full-sized avatar

Bálint Erdősi berdosi

View GitHub Profile
@berdosi
berdosi / replaceTooLongTabs.js
Created May 22, 2018 16:58
replace 8 character long tabs in github wit shorter lines
[... document.querySelectorAll(".file td")]
.map(td =>
td.firstChild
? td.firstChild.textContent
= td.firstChild.textContent
.replace(
/^(\t+)/,
(match, p1) =>
" ".repeat(4).repeat(p1.length))
: "")
@berdosi
berdosi / makeUnique.js
Created May 23, 2018 15:49
Remove duplicates from an array (to be used in Array.reduce()). If array.length may be 1, an emty array needs to be supplied as an initial value.
function makeUnique(prev, current, index) {
return (index === 1
? [prev].concat(prev !== current ? current : []) // handle when first two items are identical
: (prev.indexOf(current) > -1
? prev
: (prev.push(current), prev)))
}
@berdosi
berdosi / getJaccardSimilarity.js
Last active May 23, 2018 15:52
Calculate the Jaccard similarity index between two strings. Strings are treated as sets of words, and duplicate words are removed. (https://en.wikipedia.org/wiki/Jaccard_index)
function getJaccardSimilarity(item, otherItem) {
function makeUnique(prev, current, index) {
return (index === 1
? [prev].concat(prev !== current ? current : []) // handle when first two items are identical
: (prev.indexOf(current) > -1
? prev
: (prev.push(current), prev)))
}
const union = [].concat(item.split(/\s+/)).concat(otherItem.split(/\s+/)).reduce(makeUnique);
const otherUnique = otherItem.split(/\s+/).reduce(makeUnique);
@berdosi
berdosi / tar.ts
Last active October 12, 2018 18:21
Minimal implementation for the tar file format, based on https://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5
/** Minimal implementation of the TAR file format.
* Reference:
* https://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5
*/
export class Tar {
private data: Map<string, Blob> = new Map();
/** Add a file to the archive.
* @param {string} fileName - file name (including directory structure)
* @param {Blob} contents
@berdosi
berdosi / noapppreference.user.js
Created April 14, 2019 21:59
Userscript to disable opening youtube app when visiting the link from the browser.
// ==UserScript==
// @name removeappreference
// @namespace numen
// @include https://www.youtube.com/*
// @include https://youtube.com/*
// @include http://www.youtube.com/*
// @include http://youtube.com/*
// @include https://m.youtube.com/*
// @include http://m.youtube.com/*
// @version 1
@berdosi
berdosi / checkFacebookMessages.js
Created April 14, 2019 22:02
Check Facebook messages and do stuff with them. Code from 2015-07-31, probably defunct.
(new MutationObserver(function (mutation) { // create a mutationobserver for checking any new nodes added to the chats
mutation.forEach(function (m) { // each mutation shall be iterated.
for (var node of m.addedNodes) { // each item may contain the added nodes
message = node.textContent;
if (( message.substr(0, 4) == 'jscr') && confirm("evaluate?")) { // if message matches some conditions
// do stuff with the message
console.log(message.substr(4, 40));
}
}
})
@berdosi
berdosi / showOnce.php
Created April 14, 2019 22:09
Create single-use links for files. Code from 2012-10-08. There are probably more robust solutions than this: it is okay to read, it is less okay to use.
<?php
/* Result of some quick night-coding, so don't expect much.
There should be a show.db sqlite3 database with a schema like
in line 22.
If there isn't a new one is created with helloworld as a default password.
New passwords can be created, they are stored as sha1() hashes.
One can create a new link on show.php?mode=admin; the file names are paths on the web server.
The links are going to look like this: show.php?session=SESSION
<Multi_key> <colon> <parenright> : "☺" # Compose : )
<Multi_key> <minus> <less> : "←" U2190 # Compose - <
<Multi_key> <minus> <greater> : "→" U2192 # Compose ->
<Multi_key> <o> <o> : "…" U2026 # Compose
<Multi_key> <asterisk> <asterisk> : "★"
<Multi_key> <asciitilde> <asciitilde> : "≈" U2248
<Multi_key> <s> <n> <o> : "☃" # Snowman!
@berdosi
berdosi / makeIndex.sh
Last active May 29, 2019 21:19
generate a very basic overview of the pictures in a folder.
#!/bin/bash
# make a quick overview of the pictures in a folder
# I'm using this for Amazon S3, to have preview of the items I have in Deep Glacier
# create an empty folder: index/
# make folder: index/thumbnails
# generate thumbnails for files
# generate index.html
# - generate metadata.js
# - add thumbnails
# - add metadata (todo)
import time
def donothingDecorator(f):
""" Make a function returning some string return it prepended with the string's length. """
def wrapper(*args, **kwargs):
#print("ogwell")
returnvalue = str(f(*args, **kwargs))
return str(len(returnvalue)) + " " + returnvalue
return wrapper