-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonp-fu.js
279 lines (230 loc) · 6.1 KB
/
jsonp-fu.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*global window */
var jsonpfu = {},
jfu = jsonpfu; // shortcut
(function (window) {
// define mah vars
var script, log,
info, error,
path = window.location.pathname,
libs = {},
lib_opts = {},
exception,
ready_callbacks = {};
if (path.substr(path.length - 1) !== '/' && path !== '') {
path += '/';
}
/*
* Make my life easier while debugging and
* avoid issues with IE.
*/
if (!('console' in window)) {
window.console = {
log: function (msg) { },
error: function (msg) { },
info: function (msg) { }
};
}
log = function (text) {
if (typeof(text) === 'string') {
console.log('JSONP-Fu: ' + text);
} else {
console.log(text);
}
};
info = function (text) {
if (typeof(text) === 'string') {
console.info('JSONP-Fu: ' + text);
} else {
console.info(text);
}
};
error = function (text) {
console.error('JSONP-Fu: ' + text);
};
exception = function (text) {
throw 'JSONP-Fu: ' + text;
};
/*
* Utility functions to include and remove scripts from
* the page header.
*/
script = (function () {
var head = document.getElementsByTagName('head')[0],
scripts = {};
return {
/*
* If given a name, we can store the script object
* in a private variable so we can remove the script
* from the header later if we want *easily*
*/
include: function (url, callback, name) {
var new_script = document.createElement('script');
if (typeof(callback) !== 'function') {
callback = function () {};
}
new_script.type = 'text/javascript';
new_script.src = url;
new_script.onload = function () {
if (name) {
scripts[name] = new_script;
}
callback();
};
head.appendChild(new_script);
},
/*
* If the script was included with a name, then
* we can easily remove it by doing this.
*/
remove: function (name) {
if (scripts[name]) {
head.removeChild(scripts[name]);
delete scripts[name];
}
},
include_lib: function (lib, callback) {
this.include(path + 'lib/' + lib + ".js", function () {
info(lib + " library loaded");
callback();
}, lib);
},
/*
* Makes a JSONP query, automatically generates a callback
* function (and name, if needed), and calls the callback function.
*/
jsonp_query: function (user_opts) {
var options = {
url: '/',
data: {},
callback: "JSONPFuCallback" + new Date().getTime(),
callback_param: 'callback',
success: function (data) {}
}, i, query_string = '', that;
for (i in options) {
if (options.hasOwnProperty(i)) {
if (user_opts[i]) {
options[i] = user_opts[i];
}
}
}
/* Register JSONP callback function */
// user specified custom callback name so we have to
// wait and keep trying over and over until we are clear
if (user_opts.callback && (options.callback in window)) {
that = this;
setTimeout(function () {
that.jsonp_query(user_opts);
}, 400);
return;
}
while (options.callback in window) {
options.callback += Math.floor(Math.random() * 9999);
}
if (!(options.callback in window)) {
window[options.callback] = function (jsonp_data) {
options.success(jsonp_data);
// cleanup the global window variable
delete window[options.callback];
};
}
options.url += '?';
for (i in options.data) {
if (options.data.hasOwnProperty(i)) {
query_string += '&' + encodeURIComponent(i) + '=' + encodeURIComponent(options.data[i]);
}
}
options.url += query_string.substr(1);
if (query_string.length > 0) {
options.url += '&';
}
options.url += options.callback_param + '=' + options.callback;
log('Query\n' + options.url);
/*
* Make JSONP call, then remove script from header once
* it loads for cleanup purposes.
*/
this.include(options.url, function () {
script.remove(options.callback);
}, options.callback);
}
};
}());
/*
* Used when including JSONP-Fu library files. Need to know
* the correct absolute path to JSONP-Fu.
*
* e.g. JSONP-Fu is in http://meltingice.net/js/jsonp-fu/, so we run:
* jsonpfu.set_path('/js/jsonp-fu/');
*/
jsonpfu.set_path = function (new_path) {
// Remove trailing slash if present
if (new_path.substr(new_path.length - 1) !== '/' && new_path !== '') {
new_path += '/';
}
path = new_path;
};
/*
* Loads a JSONP-Fu library in 1 of 2 possible ways, either
* one at a time, or all at once:
*
* 1: load({ flickr: { opt: val }, twitpic: { opt: val } });
* 2: load('flickr', { opt: val });
*/
jsonpfu.load = function (opt1, opt2) {
var lib, i,
load_lib = function (lib, opts) {
lib_opts[lib] = opts;
script.include_lib(lib, function () {
// Library requires manual ready state (e.g. Facebook)
if (!libs[lib]) {
return;
}
if (ready_callbacks[lib]) {
for (i = 0; i < ready_callbacks[lib].length; i++) {
ready_callbacks[lib].pop().call(libs[lib]);
}
}
});
};
if (typeof(opt1) === 'object') {
for (lib in opt1) {
if (opt1.hasOwnProperty(lib)) {
load_lib(lib, opt1[lib]);
}
}
} else if (typeof(opt1) === 'string') {
load_lib(opt1, opt2);
}
};
jsonpfu.manual_ready = function (lib, obj) {
var i;
libs[lib] = obj;
if (ready_callbacks[lib]) {
for (i = 0; i < ready_callbacks[lib].length; i++) {
ready_callbacks[lib].pop().call(libs[lib]);
}
}
};
jsonpfu.extend = function (name, lib) {
libs[name] = lib.call(this, lib_opts[name], script);
};
jsonpfu.lib = function (lib) {
if (!libs[lib]) {
exception('attempt to access non-loaded library ' + lib);
}
return libs[lib];
};
jsonpfu.ready = function (lib, callback) {
if (!callback || typeof(callback) !== 'function') {
return;
}
if (libs.hasOwnProperty(lib)) {
callback.call(libs[lib]);
} else {
if (!ready_callbacks[lib]) {
ready_callbacks[lib] = [];
}
ready_callbacks[lib].push(callback);
}
};
}(window));