enabled remote scripts in browsers

This commit is contained in:
satyr
2010-08-18 09:35:52 +09:00
parent a8c6a641d7
commit db181e2a36
2 changed files with 57 additions and 21 deletions

47
lib/coffee-script.js Normal file → Executable file
View File

@@ -1,5 +1,5 @@
(function() {
var Lexer, compile, helpers, lexer, parser, path, processScripts;
var Lexer, compile, grind, grindRemote, helpers, lexer, parser, path, processScripts;
if (typeof process !== "undefined" && process !== null) {
path = require('path');
Lexer = require('./lexer').Lexer;
@@ -59,22 +59,45 @@
return "";
}
};
if ((typeof document !== "undefined" && document !== null) && document.getElementsByTagName) {
if ((typeof document === "undefined" || document === null) ? undefined : document.getElementsByTagName) {
grind = function(coffee) {
return setTimeout(exports.compile(coffee, {
noWrap: true
}));
};
grindRemote = function(url) {
var xhr;
xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
return grind(xhr.responseText);
}
};
return xhr.send(null);
};
processScripts = function() {
var _a, _b, _c, _d, tag;
_a = []; _c = document.getElementsByTagName('script');
for (_b = 0, _d = _c.length; _b < _d; _b++) {
tag = _c[_b];
if (tag.type === 'text/coffeescript') {
_a.push(eval(exports.compile(tag.innerHTML)));
var _a, _b, _c, script;
_b = document.getElementsByTagName('script');
for (_a = 0, _c = _b.length; _a < _c; _a++) {
script = _b[_a];
if (script.type === 'text/coffeescript') {
if (script.src) {
grindRemote(script.src);
} else {
grind(script.innerHTML);
}
}
}
return _a;
return null;
};
if (window.addEventListener) {
window.addEventListener('load', processScripts, false);
} else if (window.attachEvent) {
window.attachEvent('onload', processScripts);
addEventListener('DOMContentLoaded', processScripts, false);
} else {
attachEvent('onload', processScripts);
}
}
})();