-
Notifications
You must be signed in to change notification settings - Fork 2
/
indieconfig.js
73 lines (56 loc) · 1.97 KB
/
indieconfig.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
window.loadIndieConfig = (function () {
'use strict';
// Indie-Config Loading script
// by Pelle Wessman, voxpelli.com
// MIT-licensed
// http://indiewebcamp.com/indie-config
var config, configFrame, configTimeout,
callbacks = [],
handleConfig, parseConfig;
// When the configuration has been loaded – deregister all loading mechanics and call all callbacks
handleConfig = function () {
config = config || {};
configFrame.parentNode.removeChild(configFrame);
configFrame = undefined;
window.removeEventListener('message', parseConfig);
clearTimeout(configTimeout);
while (callbacks[0]) {
callbacks.shift()(config);
}
};
// When we receive a message, check if the source is right and try to parse it
parseConfig = function (message) {
var correctSource = (configFrame && message.source === configFrame.contentWindow);
if (correctSource && config === undefined) {
try {
config = JSON.parse(message.data);
} catch (ignore) {}
handleConfig();
}
};
if (!window.navigator.registerProtocolHandler) {
config = {};
}
return function (callback) {
// If the config is already loaded, call callback right away
if (config) {
callback(config);
return;
}
// Otherwise add the callback to the queue
callbacks.push(callback);
// Are we already trying to load the Indie-Config, then wait
if (configFrame) {
return;
}
// Create the iframe that will load the Indie-Config
configFrame = document.createElement('iframe');
configFrame.src = 'web+action:load';
document.getElementsByTagName('body')[0].appendChild(configFrame);
configFrame.style.display = 'none';
// Listen for messages so we will catch the Indie-Config message
window.addEventListener('message', parseConfig);
// And if no such Indie-Config message has been loaded in a while, abort the loading
configTimeout = setTimeout(handleConfig, 3000);
};
}());