View headlesschrome-puppeteer-example.js
const puppeteer = require('puppeteer');
const evaluateFn = async() => {
// const title = document.title; // by pure js
const title = $("title").text(); // by jquery
return {
title,
// width: document.documentElement.clientWidth,
// height: document.documentElement.clientHeight,
View read-write-transform-stream-example.js
const fs = require("fs");
const { Transform } = require('stream');
// upperCase transformer:
const upperCaseTr = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
View markdown-for-blogger-monokai-like-code-highlight.html
<html>
...
<!-- for markdown -->
<script src="//cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel='stylesheet' href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/monokai.min.css" />
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js'></script>
<script
src="//code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
View async-await-sleep-loop.js
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec));
async function hoge() {
var ret = "";
for (let len = 2, i = 0; i < len; i++) {
console.log("hoge loop ...");
ret += await foo();
}
return "HOGE!" + ret;
}
View localforage.example.js
<script src="node_modules/localforage/dist/localforage.nopromises.min.js">
// $ npm install --save localforage
</script>
<script>
const dbname = "mydb";
const opts = {
driver: localforage.INDEXEDDB, // localforage.WEBSQL, or localforage.LOCALSTORAGE
name: dbname
};
console.log("creating instance with opts:", opts);
View example-client-session.js
const express = require('express');
const app = express();
/**
* clientside cookie session.
* it's faster than using any express-session-store, and not required any database server ;)
*/
const sessions = require("client-sessions");
app.use(sessions({
cookieName: 'mySession', // cookie name dictates the key name added to the request object
View example-create-shorthash.js
/**
* Create a short hash to **roughly** specify the accessed user's identity.
* The generated hash could be immutable only when either user's IP address and access date are same.
*
* CAUTION:
* NEVER use it for generate unique ID, it could NOT be unique!
*/
const crypto = require("crypto");
var text = "2018/04/01 xxx.xxx.xxx.xxx"; // "<Date> <IP>"
View example-read-write-mysql-blob.js
/*
DDL:
CREATE TABLE `bindata` (
`id` INT NOT NULL AUTO_INCREMENT,
`data` BLOB,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
*/
const fs = require("fs");
View example-data-uri-image-write-read.js
const fs = require('fs');
const dataUri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
const outputFilename = "output";
writeImage(dataUri, outputFilename);
readImage(outputFilename + ".png");
function writeImage(dataUri, outputFilename) {
const regex = /^data:.+\/(.+);base64,(.*)$/;
View generate-keypair-pem-by-openssl.md

Generate a private/public key pem file

# First, create a file as random seed, written as you like:
$ echo "<input_many_charactors_for_random_seed>" > random.txt

# Generate a private key:
$ openssl genrsa -rand random.txt 2048 > private.pem

# Generate a public key from above private key: