Skip to content

Instantly share code, notes, and snippets.

View BryanYang's full-sized avatar

Bryan Yang BryanYang

View GitHub Profile
@BryanYang
BryanYang / loop_label.js
Created January 25, 2019 02:59
the javascript code you don't know.
function gogogo() {
firstLoop:
for (let outer = 0; outer < 4; outer++) {
secondLoop:
for (let inner = 0; inner < 5; inner++) {
if (inner === 3) {
// Use continue to avoid runs 4 and 5
continue firstLoop;
}
console.warn(`outer: ${outer}; inner: ${inner}`);
@BryanYang
BryanYang / prefetch.js
Created January 23, 2019 06:27
动态prefetch下个页面的资源,提升用户首次体验。
requestIdleCallback(() => {
fetch("xxx/a.html").then(res => res.text()).then(html => {
const reg = /target*.js/gi;
const scripts = html.match(reg);
if(scripts && scripts.length) {
scripts.forEach(src => {
const link = document.createElement("link");
link.rel = "prefetch";
link.href = src;
document.head.appendChild(link);
@BryanYang
BryanYang / koa-proxy.ts
Created December 4, 2018 11:56
koa2 http-proxy
import Koa from 'koa';
const mnsUtil = require('@mtfe/mns-util');
const httpProxy = require('http-proxy');
import config = require('config');
const proxy = httpProxy.createProxyServer({
headers: {
Host: 'alitest.e.fanxiaojian.cn',
},
});
/**
* 把平铺的 array 转成 树
*
* 比如原始数列:
* [
* {id: "13300", parentId: "13300", name: "蔬菜", status: 1},
* {id: "13301", parentId: "13301", name: "饮料", status: 1},
* {id: "13413", parentId: "13413", name: "青菜类", status: 1},
* {id: "13696", parentId: "13696", name: "肉类", status: 1},
// 快递地址从百度获取。(百度首页输入快递,查看请求url)
let url = "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=zhongtong&nu=541247425507&vcode=&token=&_=1538102135046"
let req = new Request(url)
let json = await req.loadJSON()
let items = json.data.info.context
let table = new UITable()
for (item of items) {
let row = new UITableRow()
let time = item.time
let title = decode(item.desc)
@BryanYang
BryanYang / mutation-observer.html
Last active September 13, 2018 02:50
record user action with MutationObserver
<html>
<head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<style>
.cls1 {
color: black;
}
.flex {
// 测量字符串长度
export function measureText(pText: string, pFontSize: number) {
let lDiv = document.createElement('div');
document.body.appendChild(lDiv);
lDiv.style.fontSize = '' + pFontSize + 'px';
lDiv.style.position = 'absolute';
lDiv.style.visibility = 'hidden';
lDiv.innerHTML = pText;