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);
}
}
})();

31
src/coffee-script.coffee Normal file → Executable file
View File

@ -71,14 +71,27 @@ parser.lexer =
upcomingInput: -> ""
# Activate CoffeeScript in the browser by having it compile and evaluate
# all script tags with a content-type of `text/coffeescript`. This happens
# on page load. Unfortunately, the text contents of remote scripts cannot be
# accessed from the browser, so only inline script tags will work.
if document? and document.getElementsByTagName
# all script tags with a content-type of `text/coffeescript`.
# This happens on page load.
if document?.getElementsByTagName
grind = (coffee) ->
setTimeout exports.compile coffee, noWrap: true
grindRemote = (url) ->
xhr = new (window.ActiveXObject or XMLHttpRequest)('Microsoft.XMLHTTP')
xhr.open 'GET', url, true
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
xhr.onreadystatechange = ->
grind xhr.responseText if xhr.readyState is 4
xhr.send null
processScripts = ->
for tag in document.getElementsByTagName('script') when tag.type is 'text/coffeescript'
eval exports.compile tag.innerHTML
for script in document.getElementsByTagName 'script'
if script.type is 'text/coffeescript'
if script.src
grindRemote script.src
else
grind script.innerHTML
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