-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
92 lines (82 loc) · 2.07 KB
/
application.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
var Script = Backbone.Model.extend({
load: function(callback) {
var self = this,
depIds = this.get('dependencies') || [],
collection = this.collection;
callback = callback || function() {};
if (this.get('loaded')) {
callback(this);
} else if (collection.getTabId()) {
// setup the attach script function to fire after all deps have been loaded
// (+1 for itself)
var attachScript = _.after(depIds.length + 1, function() {
chrome.tabs.sendRequest(collection.getTabId(), {
action: "attachScript",
params: self.get('src')
}, function() {
self.set('loaded', true);
callback(this);
});
});
// load all dependencies
_(depIds).each(function(depId) {
collection.get(depId).load(attachScript);
});
// and attach this script
attachScript();
}
}
}, {
create: function(attrs) {
if (!attrs.src) {
throw "Unable to create a script without a source";
}
if (!attrs.label) {
attrs.label = attrs.src.substring(attrs.src.lastIndexOf('/') + 1);
}
if (!attrs.id) {
attrs.id = attrs.label + (new Date()).getTime();
}
return new Script(attrs);
}
});
var Scripts = Backbone.Collection.extend({
model: Script,
url: 'scripts.json',
parse: function(response) {
var scripts = [], scriptData;
for (var id in response) {
scriptData = response[id];
scriptData.id = id;
scripts.push(scriptData);
}
return scripts;
},
setTabId: function(tabId) {
this.tabId = tabId;
},
getTabId: function() {
return this.tabId;
}
});
var CDNJSScripts = Scripts.extend({
url: 'http://api.cdnjs.com/libraries?fields=version,description,homepage',
parse: function(response) {
// add in xray.js
response.results.push({
name: 'xray.js',
latest: 'https://cdn.rawgit.com/janeklb/xray.js/master/lib/xray.js',
homepage: 'http://github.com/janeklb/xray.js'
});
return _.map(response.results, function(data) {
return {
id: data.name,
src: data.latest.replace('http:', ''),
label: data.name,
url: data.homepage,
description: data.description,
version: data.version
};
});
}
});