-
Notifications
You must be signed in to change notification settings - Fork 31
/
jquery.getimagedata.js
97 lines (80 loc) · 4.16 KB
/
jquery.getimagedata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
*
* jQuery $.getImageData Plugin 0.3
* http://www.maxnov.com/getimagedata
*
* Written by Max Novakovic (http://www.maxnov.com/)
* Date: Thu Jan 13 2011
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Includes jQuery JSONP Core Plugin 2.4.0 (2012-08-21)
* https://github.com/jaubourg/jquery-jsonp
* Copyright 2012, Julian Aubourg
* Released under the MIT License.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2011, Max Novakovic
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.maxnov.com/getimagedata/#license
*
*/
// jQuery JSONP
(function(d){function U(){}function V(a){r=[a]}function e(a,d,e){return a&&a.apply(d.context||d,e)}function g(a){function g(b){l++||(m(),n&&(t[c]={s:[b]}),A&&(b=A.apply(a,[b])),e(u,a,[b,B,a]),e(C,a,[a,B]))}function s(b){l++||(m(),n&&b!=D&&(t[c]=b),e(v,a,[a,b]),e(C,a,[a,b]))}a=d.extend({},E,a);var u=a.success,v=a.error,C=a.complete,A=a.dataFilter,p=a.callbackParameter,F=a.callback,W=a.cache,n=a.pageCache,G=a.charset,c=a.url,f=a.data,H=a.timeout,q,l=0,m=U,b,h,w;I&&I(function(a){a.done(u).fail(v);u=
a.resolve;v=a.reject}).promise(a);a.abort=function(){!l++&&m()};if(!1===e(a.beforeSend,a,[a])||l)return a;c=c||x;f=f?"string"==typeof f?f:d.param(f,a.traditional):x;c+=f?(/\?/.test(c)?"&":"?")+f:x;p&&(c+=(/\?/.test(c)?"&":"?")+encodeURIComponent(p)+"=?");W||n||(c+=(/\?/.test(c)?"&":"?")+"_"+(new Date).getTime()+"=");c=c.replace(/=\?(&|$)/,"="+F+"$1");n&&(q=t[c])?q.s?g(q.s[0]):s(q):(J[F]=V,b=d(K)[0],b.id=L+X++,G&&(b[Y]=G),M&&11.6>M.version()?(h=d(K)[0]).text="document.getElementById('"+b.id+"')."+
y+"()":b[N]=N,Z&&(b.htmlFor=b.id,b.event=z),b[O]=b[y]=b[P]=function(a){if(!b[Q]||!/i/.test(b[Q])){try{b[z]&&b[z]()}catch(c){}a=r;r=0;a?g(a[0]):s(R)}},b.src=c,m=function(a){w&&clearTimeout(w);b[P]=b[O]=b[y]=null;k[S](b);h&&k[S](h)},k[T](b,p=k.firstChild),h&&k[T](h,p),w=0<H&&setTimeout(function(){s(D)},H));return a}var N="async",Y="charset",x="",R="error",T="insertBefore",L="_jqjsp",z="onclick",y="on"+R,O="onload",P="onreadystatechange",Q="readyState",S="removeChild",K="<script>",B="success",D="timeout",
J=window,I=d.Deferred,k=d("head")[0]||document.documentElement,t={},X=0,r,E={callback:L,url:location.href},M=J.opera,Z=!!d("<div>").html("\x3c!--[if IE]><i><![endif]--\x3e").find("i").length;g.setup=function(a){d.extend(E,a)};d.jsonp=g})(jQuery);
(function( $ ){
// jQuery getImageData Plugin
$.getImageData = function(args) {
var regex_url_test = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
// If a URL has been specified
if(args.url) {
// Ensure no problems when using http or http
var is_secure = location.protocol === "https:";
var server_url = "";
// If url specified and is a url + if server is secure when image or user page is
if(args.server && regex_url_test.test(args.server) && !(is_secure && args.server.indexOf('http:') == 0)) {
server_url = args.server;
} else server_url = "//img-to-json.appspot.com/";
server_url += "?callback=?";
// Using jquery-jsonp (http://code.google.com/p/jquery-jsonp/) for the request
// so that errors can be handled
$.jsonp({
url: server_url,
data: { url: escape(args.url) },
dataType: 'jsonp',
timeout: args.timeout || 10000,
// It worked!
success: function(data, status) {
// Create new, empty image
var return_image = new Image();
// When the image has loaded
$(return_image).load(function(){
// Set image dimensions
this.width = data.width;
this.height = data.height;
// Return the image
if(typeof(args.success) == typeof(Function)) {
args.success(this);
}
// Put the base64 encoded image into the src to start the load
}).attr('src', data.data);
},
// Something went wrong..
error: function(xhr, text_status){
// Return the error(s)
if(typeof(args.error) == typeof(Function)) {
args.error(xhr, text_status);
}
}
});
// No URL specified so error
} else {
if(typeof(args.error) == typeof(Function)) {
args.error(null, "no_url");
}
}
};
})(jQuery);