Skip to content

Instantly share code, notes, and snippets.

@yrq110
yrq110 / cdp-raw-websocket.js
Created December 18, 2019 11:09
use cdp and websocket to communicate with chromium
/**
* Copyright 2018 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@yrq110
yrq110 / curry.js
Created November 8, 2019 07:03
pretty curry function
const curry = fn =>
judge = (...args) =>
args.length === fn.length
? fn(...args)
: (arg) => judge(...args, arg)
@yrq110
yrq110 / downloadImage.js
Last active November 8, 2019 07:02
download image
const downloadImage = imagePath =>
new Promise((resolve, reject) => {
fetch(imagePath, {
headers: new Headers({
Origin: location.origin
}),
mode: "cors"
})
.then(res => res.blob())
.then(blob => {
set user [lindex $argv 0]
set host [lindex $argv 1]
set port [lindex $argv 2]
set password [lindex $argv 3]
set timeout -1
spawn ssh $user@$host -oStrictHostKeyChecking=no -oCheckHostIP=no -p $port
expect "*assword:*"
send "$password\r"
interact
@yrq110
yrq110 / custom-console.js
Last active August 28, 2019 02:03
better console usage
const { log, warn, info } = console;
const isDevMode = process.env.NODE_ENV === "development";
const color = c => isDevMode ? c : '';
Object.assign(global.console, {
log: (...args) => log('[log]', ...args),
warn: (...args) => warn(color('\x1b[33m%s\x1b[0m'), '[warn]', '[nodepress]', ...args),
info: (...args) => info(color('\x1b[34m%s\x1b[0m'), '[info]', '[nodepress]', ...args),
error: (...args) => info(color('\x1b[31m%s\x1b[0m'), '[error]', '[nodepress]', ...args),
});
@yrq110
yrq110 / program-case-styles.md
Last active August 4, 2019 08:22
program case styles

example: hello world

case name text
camel case helloWorld
snake case hello_world
kebab case hello-world
pascal case HelloWorld
upper snake case HELLO_WORLD
@yrq110
yrq110 / mixinInTS.ts
Created August 4, 2019 07:53
mixin in typescript
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
});
});
}
@yrq110
yrq110 / pasteEventHandler.js
Last active June 17, 2019 10:21
listen paste event to detect existed image file
bindPasteEventHandle() {
document.addEventListener("paste", this.pasteHandler);
}
async pasteHandler(event) {
var items = event.clipboardData && event.clipboardData.items;
if (!items) return;
let imageRegex = /^https?:\.*?\/.*?\.(jpg|jpeg|png)$/i;
let file = null;
let fileStr = null;
// https://stackoverflow.com/a/20732091
function humanFileSize(size) {
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};