forked from curtislacy/GMail-Chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
80 lines (62 loc) · 2.93 KB
/
bootstrap.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
// Bootstrap
$(function() {
console.log( 'Starting local bootstrap.' );
//Chrome specific storage for User data. Get()
chrome.storage.local.get('engine_api_url', function(items) {
// If there's nothing in there, default to the default production version.
API_URL = items.engine_api_url || "https://apps.engine.co";
// If there's no protocol specified, use https by default.
if( API_URL.indexOf( "http" ) < 0 )
API_URL = "https://" + API_URL;
// Make a get() request to our API_URL to serve our extension.
$.get( API_URL + '/GMail-Chrome/Injects.json', null,
function( injects, textStatus, jqXHR ) {
// { injects } is the object holding our data, { injects.css } are the css
// files specified in Injects.json.
injects.css.forEach( function( item ) {
// Make another get() request to the server to serve up original .css files.
$.get( API_URL + '/GMail-Chrome/' + item, null,
function( original, textStatus, jqXHR ) {
// Replaces some of the image references in the .css with downloadable
// images from our server.
var data = original.replace( /@@API_URL/g, API_URL + '/GMail-Chrome' );
// Create DOM element that we will attach our .css to.
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = data;
document.getElementsByTagName("head")[0].appendChild( style );
console.log( 'CSS Loaded from: ' + item );
}
).fail( function() {
alert( 'Could not load CSS from ' + API_URL + '/GMail-Chrome/' + item + '. Please refresh this page. If the problem persists, please contact [email protected] for help.' );
});
});
// Using the declaration of .js in Injects.json above, we execute a forEachSeries
// for each of the js files in Injects.json.
async.forEachSeries( injects.js,
function( item, done ) {
//Get request for each item in injects.js
$.get( API_URL + '/GMail-Chrome/' + item, null,
// eval() is executed for each .js file automatically
// due to the nature of the get() request (jQuery)
function( data, textStatus, jqXHR ) {
console.log( 'Loaded ' + item );
// Call done() to end the request callback.
done();
}
).fail( function() {
alert( 'Could not load Engine extension code from ' + API_URL + '/GMail-Chrome/' + item + '. Please refresh this page. If the problem persists, please contact [email protected] for help.' );
done();
});
},
// After all the async calls run, bootstrap() is executed to launch the
// extension.
function( error ) {
bootstrap();
}
);
}).fail( function() {
alert( 'An error has occured communicating with ' + API_URL + '. Please refresh this page. If the problem persists, please contact [email protected] for help.');
});
});
});