Skip to content

Instantly share code, notes, and snippets.

@dannvix
dannvix / v8-embedder.cpp
Created April 2, 2019 15:16
Cross-context JavaScript function invocation with embedded Google V8
#include "v8/libplatform/libplatform.h"
#include "v8/v8.h"
#include <iostream>
#pragma comment(lib, "v8_monolith")
int main() {
auto platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
@dannvix
dannvix / NetflixSmallerSubtitles.js
Created February 4, 2018 09:06
Make the SVG-based subtitles smaller on Netflix web player
// Only applied for "image-based timed text" (i.e. SVG-based, like Chinese, Japanese, ...)
// For DFXP-based subtitles (e.g., English), try the hidden settings: https://www.netflix.com/SubtitlePreferences
(() => {
const installScaler = () => {
const subRectElem = document.querySelector('rect.image-based-subtitles-rect');
if (subRectElem) {
const subWrapperElem = subRectElem.parentNode;
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
const config = {attributes: true, childList: true, characterData: true, };
@dannvix
dannvix / mydft.py
Last active October 23, 2024 07:18
Intuitive impementation of discrete Fourier transform (and inverse DFT) in Python (without numpy)
#!/usr/bin/env python3
import math
# >> Discrete Fourier transform for sampled signals
# x [in]: sampled signals, a list of magnitudes (real numbers)
# yr [out]: real parts of the sinusoids
# yi [out]: imaginary parts of the sinusoids
def dft(x):
N, yr, yi = len(x), [], []
@dannvix
dannvix / thread_pool.hpp
Created March 21, 2016 14:55
Thread pool using std::thread and boost::lockfree::queue
#include <boost\lockfree\queue.hpp>
#include <iostream>
#include <thread>
#include <atomic>
#include <vector>
#include <memory>
#include <condition_variable>
#include <functional>
template<typename ItemType, unsigned long ulQueueCapacity>
@dannvix
dannvix / c99-heap.c
Created October 8, 2015 14:58
Simple std::priority_queue-like container implemented in C99, without error handling and thread-safety
/*
* c99-heap.c
* - Description: Simple std::priority_queue-like container implemented in C99, without error handling and thread-safety
* - Author: Shao-Chung Chen
* - License: CC0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
@dannvix
dannvix / c99-vector.c
Created October 8, 2015 14:57
Simple std::vector-like container implemented in C99, without error handling and thread-safety
/*
* c99-vector.c
* - Description: Simple std::vector-like container implemented in C99, without error handling and thread-safety
* - Author: Shao-Chung Chen
* - License: CC0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@dannvix
dannvix / replace-fonts-bookmarklet.js
Created August 1, 2015 15:09
Bookmarklet that replaces all fonts with “Hiragino Kaku Gothic Pro” on current webpage
/*
* Pack below script via http://jscompress.com/ to generate bookmarklet
*/
(function(rootNode, fontFamily) {
var hasChildTextNode = function(parentNode) {
return [].some.call(parentNode.childNodes, function(node) {
if (node.nodeType == Node.TEXT_NODE) {
console.log(node.nodeValue);
}
return node.nodeType == Node.TEXT_NODE;
@dannvix
dannvix / main-conditional-recusrion.cpp
Created July 31, 2015 14:44
Print 1 to 1000 without loop and conditional statements in C++11
#include <iostream>
#include <vector>
int main(int n) {
static int(*f[])(int) = {main, [](int)->int{exit(0);}};
std::cout << n << std::endl;
return f[n/1000](n+1);
}
@dannvix
dannvix / es6-tagged-template.js
Created July 13, 2015 14:22
Python-like template string in ECMAScript 6
// Python-like template string in ECMAScript 6 "tagged template"
// inspired from http://stackoverflow.com/a/22619256
function formatter(literals, ...substitutions) {
return {
format: function() {
let out = [], i = 0, k = 0;
for (i,k; i < literals.length; i++) {
out[k++] = literals[i];
out[k++] = Number.isInteger(substitutions[i]) ?
arguments[substitutions[i]] :
@dannvix
dannvix / RainbowEverything.js
Created June 27, 2015 04:05
Make all text on webpage looks Rainbow!
/*rainbowEverything=*/(function(window) {
var parseRgbColorStrToHsl = function(rgbColorStr) {
// input: "rgb(12, 34, 56)";
var rgbValues = rgbColorStr.substring(4).replace(/\s/g, "").split(","),
r = parseFloat(rgbValues[0])/255,
g = parseFloat(rgbValues[1])/255,
b = parseFloat(rgbValues[2])/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h = 0, s = 0, l = (max + min) / 2;
if (max == min) {