forked from dogescript/dogescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·110 lines (92 loc) · 2.57 KB
/
index.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
/**
* dogescript - wow so syntax such language
*
* Copyright (c) 2013-2014 Zach Bruggeman
*
* dogescript is licensed under the MIT License.
*
* @package dogescript
* @author Zach Bruggeman <talkto@zachbruggeman.me>
*/
var beautify = require('js-beautify').js_beautify;
var xhr = require('xhr');
var parser = require('./lib/parser');
function parse (file, beauty, dogeMode) {
if (dogeMode) var lines = file.split(/ {3,}|\r?\n/);
else var lines = file.split(/\r?\n/);
var script = '';
for (var i = 0; i < lines.length; i++) {
script += parser(lines[i]);
}
if (beauty) return beautify(script)
else return script;
}
exports.default = parse;
// TODO: Investigate
if (typeof window !== 'undefined' && window !== null) {
var queue = [];
var seen = [];
var stepQueue = function () {
while (queue.length > 0 && queue[0].ready) {
var script = queue.shift();
exec(script.text);
}
}
var exec = function (source) {
var js = ';\n' + parse(source);
if (js) {
// TODO: Evaluate me and if i work
// with (window) {
eval(js);
// }
}
}
var getLoadEval = function (script) {
var res = {
type: 'load',
ready: false,
text: ''
};
xhr(script.getAttribute('src'), function (err, resp, body) {
if (err) {
throw err;
}
res.ready = true;
if (body) {
res.text = body;
}
stepQueue();
});
return res;
}
var getInlineEval = function (script) {
return {
type: 'inline',
ready: true,
text: script.innerHTML
};
}
var processTags = function () {
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
if (seen.indexOf(script) > -1) {
continue;
}
seen.push(script);
if (script.getAttribute('type') === 'text/dogescript') {
if (script.getAttribute('src')) {
queue.push(getLoadEval(script));
} else {
queue.push(getInlineEval(script));
}
}
}
stepQueue();
}
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', processTags);
} else {
window.attachEvent('onload', processTags);
}
}