Skip to content

Instantly share code, notes, and snippets.

View lushijie's full-sized avatar

Shijie Lu lushijie

  • Meituan && Qihoo 360
  • Beijing
View GitHub Profile
function interceptNetworkRequests(ee) {
const open = XMLHttpRequest.prototype.open;
const send = XMLHttpRequest.prototype.send;
const isRegularXHR = open.toString().indexOf('native code') !== -1;
// don't hijack if already hijacked - this will mess up with frameworks like Angular with zones
// we work if we load first there which we can.
if (isRegularXHR) {
Function.prototype.bind2 = function(context) {
const self = this;
const args = Array.prototype.slice.call(arguments, 1);
return function() {
const args2 = Array.prototype.slice.call(arguments);
self.apply(context, args.concat(args2));
}
}
@lushijie
lushijie / node-or-browser.js
Created December 6, 2018 11:21 — forked from rhysburnie/node-or-browser.js
Detect node or browser
// determine if in-browser or using node.js
// thruthy
var _nodejs = (
typeof process !== 'undefined' && process.versions && process.versions.node);
if (_nodejs) {
_nodejs = {
version: process.versions.node
};
}
@lushijie
lushijie / cubeDebug.js
Last active November 1, 2018 03:43
storage 调试
function datetime(date = new Date(), format) {
if (date && typeof date === 'string') {
const dateString = date;
date = new Date(Date.parse(date));
if (isNaN(date.getTime()) && !format) {
format = dateString;
date = new Date();
}
}
format = format || 'YYYY-MM-DD HH:mm:ss';
@lushijie
lushijie / dom-to-json.js
Created May 6, 2018 02:41 — forked from sstur/dom-to-json.js
Stringify DOM nodes using JSON (and revive again)
function toJSON(node) {
node = node || this;
var obj = {
nodeType: node.nodeType
};
if (node.tagName) {
obj.tagName = node.tagName.toLowerCase();
} else
if (node.nodeName) {
obj.nodeName = node.nodeName;
@lushijie
lushijie / encrypt.js
Created March 7, 2018 02:19
加解密
// http://lib.baomitu.com/crypto-js/3.1.9-1/crypto-js.js
function encrypt(content, key) {
var encryptResult = CryptoJS.AES.encrypt(
content,
key,
{
//iv: key,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
@lushijie
lushijie / fileMd5.js
Created February 9, 2018 03:56
计算文件md5
let fs = require('fs');
let crypto = require('crypto');
let path = '/Users/lushijie/Pictures/day1.jpg';
let start = new Date().getTime();
let md5sum = crypto.createHash('md5');
let stream = fs.createReadStream(path);
stream.on('data', function(chunk) {
md5sum.update(chunk);
});
@lushijie
lushijie / runAtOnce.js
Created December 29, 2017 08:37
第一次声明执行并绑定this
let articleRequest = () => {
let f = function() {
// todo
}.bind(this)();
return f;
};
articleRequest();
let articleRequest2 = function f() {
// todo
@lushijie
lushijie / declareAndRun.js
Created December 28, 2017 10:12
声明一个函数并接着执行一次
let func = (function f(a) {
console.log(a);
return f;
})('hello world');
@lushijie
lushijie / toUnicodeSequence.js
Created December 26, 2017 08:05
转化为unicode
function toUnicodeSequence(str) {
for(var i = str.length; i--;) {
str = str.slice(0, i) + '\\u'
+ ('000' + str.charCodeAt(i).toString(16)).slice(-4)
+ str.slice(i + 1);
}
return str;
}