wx
Version:
W(ei)X(in) -- (minimalist) WeChat Middleware for Express.js
108 lines (104 loc) • 3.49 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var $,
slice = [].slice;
$ = jQuery;
$.fn.scan = function(handler, options) {
if (!arguments.length) {
return this.trigger('scan');
}
return this.each(function() {
var continuous, error, expires, img, load, msg_ids, new_src, src;
msg_ids = {};
if (!(src = $(this).attr('src'))) {
return;
}
continuous = $(this).attr('continuous') != null;
img = this;
expires = 1800 * 1000;
$(this).on('scan', null, null, function() {
var args, event;
event = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return handler != null ? handler.apply(img, args) : void 0;
});
new_src = function() {
var w_query;
w_query = src.indexOf('?') !== -1;
return "" + src + (w_query ? '&' : '?') + "t=" + (Date.now());
};
load = function() {
var permanent, polling, refresh;
$(img).show();
(polling = (function(_this) {
return function() {
var timeout;
if (!$(img).is(':visible')) {
return;
}
timeout = setTimeout(polling, 25000);
return (function() {
var scan_url, start;
start = Date.now();
scan_url = encodeURI(decodeURI(src.replace(/(.*)(\/qrcode)($|((\?|\/).*))/i, '$1/scan$3')));
scan_url = scan_url.indexOf('?') !== -1 ? scan_url + ("&t=" + (Date.now())) : scan_url + ("?t=" + (Date.now()));
return $.ajax({
url: scan_url,
xhrFields: {
withCredentials: true
}
}).success(function(params) {
var msg_id, ref;
clearTimeout(timeout);
$(img).addClass('scanned');
ref = params, msg_id = ref[0], params = 2 <= ref.length ? slice.call(ref, 1) : [];
if (msg_ids[msg_id]) {
return;
}
msg_ids[msg_id] = 1;
if (handler != null) {
handler.apply(img, params);
}
if (continuous) {
return polling();
}
}).fail(function(xhr, status) {
if (xhr.status === 404) {
return;
}
clearTimeout(timeout);
return polling();
});
})();
};
})(this))();
if (permanent = src.match(/\/qrcode\/\d+(\?|$)/)) {
return;
}
return setTimeout(refresh = (function(_this) {
return function() {
if (!$(img).is(':visible')) {
return;
}
return $(img).clone().attr('src', new_src()).hide().appendTo(document.body).load(function() {
if (!$(img).is(':visible')) {
return;
}
img = $(this).replaceAll(img).show().get(0);
return setTimeout(refresh, expires);
}).error(function() {
$(this).remove();
return refresh();
});
};
})(this), expires);
};
error = function() {
return $(this).attr('src', new_src());
};
$(this).load(load).error(error);
if (this.complete) {
return $(this).load();
}
});
};
}).call(this);