Skip to content

Instantly share code, notes, and snippets.

@berzniz
berzniz / jshipster_and_and.js
Last active May 22, 2022 23:15
Some small javascript hacks for hipsters
// Boring
if (isThisAwesome) {
alert('yes'); // it's not
}
// Awesome
isThisAwesome && alert('yes');
// Also cool for guarding your code
var aCoolFunction = undefined;
@ssddi456
ssddi456 / gist:5566866
Last active December 17, 2015 06:39
for browser gbkescape
define([],function() {
var iframe=document.createElement("iframe");
iframe.src="about:blank";
iframe.setAttribute("style","display:none;visibility:hidden;");
document.body.appendChild(iframe);
var d=iframe.contentWindow.document;
d.charset=d.characterSet="GBK";
function getGBKEscape(s) {
d.write("<body><a href='?"+s+"'>X</a></body>");
d.close();
@ssddi456
ssddi456 / autocomplete.less
Last active December 15, 2015 10:09
autoComplete for ace usage: autocomplete( aceediter )
.ace-autocomplete-dialog{
position:absolute;
top:0;left:0;
min-width:200px;
max-height:196px;
overflow:hidden;
font-size:12px;
display:none;
box-shadow:0 0 5px 1px rgba(0, 0, 0, 0.3);
background:#E8E8E8;
@supersha
supersha / ace-autocomplete-plugin-usage.js
Last active December 15, 2015 08:29
给ACE编辑器创建的一个AutoComplete编码功能,方便编写代码。
// 初始化autocomplete dialog的各项功能
// items参数就是complete框默认可见多少项
// editor参数是ACE的实例化对象
var autocomplete = new AceAutoComplete(editor,{
items : 10
});
@fireblue
fireblue / UIWebView+Screenshot.m
Last active November 1, 2016 02:28
Find a certain element in UIWebView and make a screenshot. :)
-(UIImage *)screenShot
{
CGRect originalFrame = self.frame;
CGPoint originalOffset = self.scrollView.contentOffset;
CGSize entireSize = [self sizeThatFits:CGSizeZero];
[self setFrame: CGRectMake(0, 0, entireSize.width, entireSize.height)];
CGRect rect = [self positionOfElementWithId:@"post1"];
//如果没有找到这个元素,就取整个页面
if (rect.origin.y != 0) {
@bshamric
bshamric / cache.js
Last active January 24, 2021 12:08
I like phantomjs, but it doesn't directly support getting images from webpages without requesting them separately like in casperjs. I went through QTNetworking code in the phantomjs repo until I figured out where the cache was. To use this, have all three files in the same directory. Then modify test.js for whatever you need. Call phantom js wit…
var fs = require('fs');
//this is the path that QTNetwork classes uses for caching files for it's http client
//the path should be the one that has 16 folders labeled 0,1,2,3,...,F
exports.cachePath = '/path/to/phantomjs/cache/data/folder';
//this is the extension used for files in the cache path
exports.cacheExtension = "d";
//the resources that are to be saved
@jed
jed / rendering_templates_obsolete.md
Created October 19, 2012 05:07
Rendering templates obsolete

(tl;dr DOM builders like [domo][domo] trump HTML templates on the client.)

Like all web developers, I've used a lot of template engines. Like most, I've also written a few of them, some of which even [fit in a tweet][140].

The first open-source code I ever wrote was also one of the the first template engines for node.js, [a port][node-tmpl] of the mother of all JavaScript template engines, [John Resig][jresig]'s [micro-templates][tmpl]. Of course, these days you can't swing a dead cat without hitting a template engine; one in eight packages on npm ([2,220][npm templates] of 16,226 as of 10/19) involve templates.

John's implementation has since evolved and [lives on in Underscore.js][underscore], which means it's the default choice for templating in Backbone.js. And for a while, it's all I would ever use when building a client-side app.

But I can't really see the value in client-side HTML templates anymore.

var lastUsedHeap = 0; // remember the heap size
function checkMemory()
{
// check if the heap size is this cycle is LESS than what we had last
// cycle; if so, then the garbage collector has kicked in
if (window.performance.memory.usedJSHeapSize < lastUsedHeap)
console.log('Garbage collected!');
lastUsedHeap = window.performance.memory.usedJSHeapSize;
@cuppster
cuppster / node-express-cors-middleware.js
Created April 9, 2012 16:02
express.js middleware to support CORS pre-flight requests
app.use(express.methodOverride());
// ## CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
@endtwist
endtwist / gist:1630834
Created January 18, 2012 03:57
Paste event in Google Chrome
$( 'body' ).bind( 'paste', function( evt ) {
var items = evt.originalEvent.clipboardData.items
, paste;
// Items have a "kind" (string, file) and a MIME type
console.log( 'First item "kind":', items[0].kind );
console.log( 'First item MIME type:', items[0].type );
// If a user pastes image data, there are 2 items: the file name (at index 0) and the file (at index 1)
// but if the user pastes plain text or HTML, index 0 is the data with markup and index 1 is the plain, unadorned text.