Skip to content

Instantly share code, notes, and snippets.

View petamoriken's full-sized avatar
🌐
Proceeding with the Web Standards

Kenta Moriuchi petamoriken

🌐
Proceeding with the Web Standards
View GitHub Profile
@petamoriken
petamoriken / malloc.html
Created August 9, 2016 07:50
Wasm malloc テスト
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>malloc test</title>
<script>
fetch("malloc.wasm").then(res => res.arrayBuffer())
.then(buffer => {
const binarray = new Uint8Array(buffer);
@petamoriken
petamoriken / patch.js
Last active October 28, 2016 10:28
某フリーゲームみたいにスペースキーを押してるときに背景が速く流れるパッチ https://splashworks.jp/
(function() {
"use strict";
var pJSDOM = window.pJSDom[window.pJSDom.length - 1];
var pJS = pJSDOM.pJS;
var $document = $(document);
var spaceCode = " ".charCodeAt(0);
var isSpaceKeyPressed = false;
@petamoriken
petamoriken / hfround.js
Created April 11, 2017 06:57
half precision float round function
const buffer = new ArrayBuffer(4);
const floatView = new Float32Array(buffer);
const uint32View = new Uint32Array(buffer);
const baseTable = new Uint32Array(512);
const shiftTable = new Uint32Array(512);
for(let i = 0; i < 256; ++i) {
const e = i - 127;
@petamoriken
petamoriken / recursion_readdir.js
Last active February 3, 2018 22:00
再帰的にディレクトリのファイル一覧を取得する
const util = require("util");
const fs = require("fs");
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
async function walk(dir, filelist = []) {
const files = await readdir(dir);
await Promise.all(files.map(async file => {
const filename = `${ dir }/${ file }`;
/**
* @file Fix https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/106663/
*/
const prototype = IDBObjectStore.prototype;
const { add, put, count } = prototype;
prototype.add = function(value, key) {
return key != null ? add.call(this, value, key) : add.call(this, value);
};
@petamoriken
petamoriken / createImageBlob.d.ts
Last active November 28, 2018 11:16
Create Blob like window.createImageBitmap
type ImageBlobOptions = ImageBitmapOptions & { type?: string, quality?: number };
export declare function createImageBlob(source: ImageBitmapSource, options?: ImageBlobOptions): Promise<Blob>;
export declare function createImageBlob(source: ImageBitmapSource, sx: number, sy: number, sw: number, sh: number, options?: ImageBlobOptions): Promise<Blob>;
@petamoriken
petamoriken / getRandomBigInt.mjs
Last active January 1, 2019 17:27
Get random bigint value in range.
/**
* @param {number} byteLength A QuotaExceededError DOMException is thrown if the requested length is greater than 65536 bytes.
* @returns {bigint}
*/
function getRandomBigIntByByteLength(byteLength) {
const buffer = new Uint8Array(byteLength);
crypto.getRandomValues(buffer);
return buffer.reduce((prev, current) => (prev << 8n) + BigInt(current), 0n);
}
class StatelessRegExp extends RegExp {
* exec(str, index = 0) {
let lastIndex = this.lastIndex = index;
let match;
while ((match = super.exec(str)) !== null) {
lastIndex = this.lastIndex;
yield match;
this.lastIndex = lastIndex;
/**
* Node を順に列挙する
*
* @param {Node} root
* @param {?{ whatToShow?: number, filter?: NodeFilter, direction?: "capture" | "bubbling" }} options
* @param {"capture" | "bubbling"} options.direction - "capture" では現れる順に列挙し、 "bubbling" では子を持つ Node は子から列挙する
* @return {Generator<Node>}
*/
function* createNodeIterator(root, options = {}) {
const { whatToShow, filter } = options;
@petamoriken
petamoriken / searchText.js
Last active October 23, 2019 07:56
HTML 内の文字列の検索
/** @see https://html.spec.whatwg.org/multipage/syntax.html#void-elements */
const VOID_ELEMENTS = Object.freeze([
"AREA",
"BASE",
"BR",
"COL",
"EMBED",
"HR",
"IMG",
"INPUT",