Skip to content

Instantly share code, notes, and snippets.

@yuqianma
yuqianma / ajax.js
Last active January 26, 2017 16:06
function fetchData(url, cb) {
var xhr = new XMLHttpRequest() // 新建一个请求对象,如果需要文档,查XMLHttpRequest
xhr.open('GET', url) // get类型, url;注意此时请求还没有发送
xhr.onload = function() {cb(xhr.responseText)} // 写好callback回调函数,因为js是异步的,只用通过回调函数接收参数来获取。
xhr.send() // 发送请求,这里才是真的开始请求,请求成功的话回调就成功了;post在这里发送数据
}
(function () {
var callback = function (str) {
var data = JSON.parse(str) // JSON.parse, JSON.stringify 互相转换
function [varargout]=rp_sliceviewer(AOperation, varargin)
% Show a brain's slice. "rp_sliceviewer" can be opened more than one instance like MRIcro, and supports multi-slice, overlay and so on. by Xiao-Wei Song
%Usage: hFig =rp_sliceviewer('ShowImage', AFilename, CallBack);
% rp_sliceviewer('Delete', AFigHandle);
%Detailed usage is the code file "rp_sliceviewer.m"
%------------------------------------------------------------------------------------------------------------------------------
% Copyright(c) 2007~2012
% State Key Laboratory of Cognitive Neuroscience and Learning, Beijing Normal University
% Written by Xiao-Wei Song
% http://www.restfmri.net
const fs = require('fs')
function rmPath(path, cb) {
fs.lstat(path, (err, stats) => {
//console.log('retrieve:', path)
if (stats.isDirectory()) {
fs.readdir(path, (err, files) => {
let len = files.length + 1
let checkDir = () => {
//console.log(path + ' left:', len-1)
@yuqianma
yuqianma / svg-save-png.js
Created January 21, 2017 08:52
save png from svg with canvas
var svgString = new XMLSerializer().serializeToString(root);
var width = root.clientWidth, height = root.clientHeight;
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
var DOMURL = self.URL || self.webkitURL || self;
var img = new Image();
@yuqianma
yuqianma / phantomjs-process.js
Created March 30, 2017 19:18
phantomjs child process exp
var system = require('system'),
fs = require('fs'),
process = require("child_process");
var children = {};
var port = system.args[1];
var isParent = system.args[2];
console.log('[process] port:', port);
@yuqianma
yuqianma / parseUrl.js
Last active April 7, 2017 07:15
[parse url] hash search url
// parseSearch('?gws_rd=ssl')
// {gws_rd: "ssl"}
function parseSearch (str) {
str = str.split('?').pop()
var search = {}
str && str.replace(
/([^?=&]+)(=([^&]*))?/g,
function ($0, $1, $2, $3) {
search[ $1 ] = $3
@yuqianma
yuqianma / flask_crossdomain.py
Created April 29, 2017 08:50
Flask crossdomain
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper
# replaced 'basestring' width 'str' for python3
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
@yuqianma
yuqianma / noCircularJson.js
Created May 8, 2017 08:07
[noCircularJson.js]
function jsonStringify (obj) {
var cache = []
function filter (key, value) {
if (typeof value === 'object' && value != null) {
if (cache.includes(value)) {
return // may circular
}
cache.push(value)
}
return value
@yuqianma
yuqianma / piTemp.py
Last active September 9, 2017 14:30
raspberry pi temperature
#!/usr/bin/env python
import os
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=",""))
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
@yuqianma
yuqianma / README.md
Last active December 8, 2017 18:14 — forked from pbogden/README.md
Clustered Force Layout v4