forked from jamesgpearce/confess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confess.js
197 lines (173 loc) · 6.83 KB
/
confess.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
var confess = {
run: function () {
this.settings = {};
if (!phantom.utils.processArgs(this.settings, [
{name:'url', def:"http://google.com", req:true, desc:"the URL of the app to cache"},
{name:'ua', def:phantom.userAgent, req:false, desc:"the user-agent used to request the app"},
{name:'task', def:'manifest', req:false, desc:"the task to be performed (currently only 'manifest')"}
])) {
phantom.exit();
return;
}
var task = this[this.settings.task];
phantom.utils.load(this.settings.url, this.settings.ua,
task.pre,
task.post,
this
);
},
manifest: {
pre: function () {
console.log('CACHE MANIFEST\n');
console.log('# This manifest was created by confess.js, http://github.com/jamesgpearce/confess');
console.log('#');
console.log('# Time: ' + new Date());
console.log('# URL: ' + this.settings.url);
console.log('# UA: ' + this.settings.ua);
console.log('#');
console.log('# Any console output generated by this page or app is shown immediately below. You\'ll need to remove this to create a valid manifest syntax.');
console.log('# [Start of console output]');
},
post: function () {
console.log('# [End of console output]');
console.log('\nCACHE:');
for (url in this.extractResources(this.settings.url)) {
console.log(url);
};
console.log('\nNETWORK:\n*');
}
},
extractResources: function (url) {
var
// resources referenced in DOM
// notable exceptions: iframes, rss, links
selectors = [
['script', 'src'],
['img', 'src'],
['link[rel="stylesheet"]', 'href']
],
// resources referenced in CSS
properties = [
'background-image',
'list-style-image',
'src' // This is for custom css rule like @font-face{ src: url(...)}
],
resources = {},
foreach = phantom.utils.foreach,
baseScheme = url.split("//")[0];
foreach(selectors, function (selectorPair) {
var selector = selectorPair[0];
var attribute = selectorPair[1];
var elements = document.querySelectorAll(selector);
foreach(elements, function(element) {
this.tallyResource(resources, element.getAttribute(attribute), baseScheme);
}, this);
}, this);
foreach (document.styleSheets, function (stylesheet) {
foreach (stylesheet.rules, function(rule) {
if (!rule['style']) { return; }
foreach (properties, function(property) {
var value = rule.style.getPropertyCSSValue(property);
if (value) {
switch (value.cssValueType) {
case CSSValue.CSS_PRIMITIVE_VALUE: // 1
if (value && value.primitiveType === CSSPrimitiveValue.CSS_URI) {
this.tallyResource(resources, value.getStringValue(), baseScheme);
}
break;
case CSSValue.CSS_VALUE_LIST: // 2
var self = this;
phantom.utils.parseSources(value.cssText, function(url) {
self.tallyResource(resources, url, baseScheme);
});
// Maybe we could break here but just to be sure let's go through all the CSSValueList array
foreach (value, function(cssValue) {
if (cssValue && cssValue.cssValueType === CSSValue.CSS_CUSTOM) {
phantom.utils.parseSources(value.cssText, function(url) {
self.tallyResource(resources, url, baseScheme);
});
}
}, this);
break;
}
}
}, this);
}, this);
}, this);
return resources;
},
tallyResource: function (resources, url, baseScheme) {
if (url && url.substr(0,5)!='data:') {
if (url.substr(0, 2)=='//') {
url = baseScheme + url;
}
if (!resources[url]) {
resources[url] = 0;
}
resources[url]++;
}
}
};
phantom.utils = {
load: function (url, ua, pre, post, scope) {
if (!phantom.state) {
phantom.userAgent = ua;
phantom.state = true;
pre.apply(scope);
phantom.open(url);
} else {
post.apply(scope);
phantom.exit();
}
},
processArgs: function (settings, contract) {
var a = 0;
this.foreach(contract, function(argument) {
if (a < phantom.args.length) {
settings[argument.name] = phantom.args[a];
} else {
if (argument.req) {
console.log('"' + argument.name + '" argument is required. This ' + argument.desc + '.');
return false;
}
settings[argument.name] = argument.def;
}
a++;
return true;
});
return (a > phantom.args.length);
},
parseUrl: function (url){
/*
Will parse any of the following:
url('http://html.net/font/letter_gothic_std_bold-webfont.ttf');
local("letter gothic std bold webfont.ttf");
url(html.net/font/letter_gothic_std_bold-webfont.ttf);
url ( html.net/font/letter_gothic_std_bold-webfont.ttf )
*/
var re = /(?:url|local)\W*\(\s*(.?)([\-\/\?\&a-zA-Z0-9_.: ]+)\1\s*\)/;
if (re.test(url)) {
m = url.match(re);
return m[2];
}
return null;
},
parseSources: function(sourcesText, callback, scope) {
var sources = sourcesText.split(',');
var self = this;
this.foreach(sources, function(source) {
var url = self.parseUrl(source);
callback.apply(scope, [url]);
});
},
foreach: function (collection, callback, scope) {
if (collection) {
for (var i = 0; i < collection.length; i++) {
if (callback.apply(scope, [collection[i]])===false) {
break;
};
};
}
}
}
confess.run();