-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page.js
90 lines (78 loc) · 2.77 KB
/
Page.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
define("ajaxer/Page", [], function() {
"use strict";
$.Class.extend("ajaxer.Page", {
debug: false
}, {
id: null,
body: "",
head: {},
title: "",
styles: [],
scripts: [],
embeddedScripts: [],
metas: [],
url: "",
callback: null,
init: function(url, callback) {
this.url = url;
this.callback = callback;
if (this.constructor.debug) console.log("AjaxerPage: getting page");
$.get(url + "?" + Math.random(), this.proxy("gotPage"), "html");
},
gotPage: function(data) {
if (this.constructor.debug) console.log("AjaxerPage: got page");
var id = this.url;
var body = data.replace(/[\r\n]/g, '').match(/<body.*?>(.*?)<\/body>/i)[1];
var head = data.replace(/[\r\n]/g, '').match(/<head.*?>(.*?)<\/head>/i)[1];
var title = head.match(/<title.*?>(.*?)<\/title>/i)[1];
var scripts = [];
$.merge(scripts, head.match(/<script.*? src=".+?".*?>.*?<\/script>/ig) || []);
//$.merge(scripts, head.match(/<script.*?\/>/ig) || []);
//$.merge(scripts, head.match(/<script.*?>.*?<\/script>/ig) || []);
for (var i in scripts) {
scripts[i] = [$(scripts[i])[0], 0]
}
var embeddedScripts = head.match(/<script>.*?<\/script>/ig) || [];
var styles = [];
$.merge(styles, head.match(/<link.*? rel="stylesheet".*?\/>/ig) || []);
$.merge(styles, head.match(/<link.*? rel="stylesheet".*?>.*?<\/link>/ig) || []);
$.merge(styles, head.match(/<style.*?>.*?<\/style>/ig) || []);
for (var i in styles) {
styles[i] = [styles[i], 0]
}
var metas = [];
$.merge(metas, head.match(/<meta.*?\/>/ig) || []);
$.merge(metas, head.match(/<meta.*?>.*?<\/meta>/ig) || []);
this.id = id;
this.title = title;
this.scripts = scripts;
this.embeddedScripts = embeddedScripts;
this.styles = styles;
this.metas = metas;
this.body = body;
this.callback(this);
},
getBody: function() {
return this.body;
},
getStyles: function() {
return this.styles;
},
getScripts: function() {
return this.scripts;
},
getEmbeddedScripts: function() {
return this.embeddedScripts;
},
getId: function() {
return this.id;
},
getUrl: function() {
return this.url;
},
getTitle: function() {
return this.title;
}
});
return ajaxer.Page;
});