1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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() { (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) { if (typeof process !== "undefined" && process !== null) {
path = require('path'); path = require('path');
Lexer = require('./lexer').Lexer; Lexer = require('./lexer').Lexer;
@ -59,22 +59,45 @@
return ""; 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() { processScripts = function() {
var _a, _b, _c, _d, tag; var _a, _b, _c, script;
_a = []; _c = document.getElementsByTagName('script'); _b = document.getElementsByTagName('script');
for (_b = 0, _d = _c.length; _b < _d; _b++) { for (_a = 0, _c = _b.length; _a < _c; _a++) {
tag = _c[_b]; script = _b[_a];
if (tag.type === 'text/coffeescript') { if (script.type === 'text/coffeescript') {
_a.push(eval(exports.compile(tag.innerHTML))); if (script.src) {
grindRemote(script.src);
} else {
grind(script.innerHTML);
} }
} }
return _a; }
return null;
}; };
if (window.addEventListener) { if (window.addEventListener) {
window.addEventListener('load', processScripts, false); addEventListener('DOMContentLoaded', processScripts, false);
} else if (window.attachEvent) { } else {
window.attachEvent('onload', processScripts); attachEvent('onload', processScripts);
} }
} }
})(); })();

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

@ -71,14 +71,27 @@ parser.lexer =
upcomingInput: -> "" upcomingInput: -> ""
# Activate CoffeeScript in the browser by having it compile and evaluate # Activate CoffeeScript in the browser by having it compile and evaluate
# all script tags with a content-type of `text/coffeescript`. This happens # all script tags with a content-type of `text/coffeescript`.
# on page load. Unfortunately, the text contents of remote scripts cannot be # This happens on page load.
# accessed from the browser, so only inline script tags will work. if document?.getElementsByTagName
if document? and 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 = -> processScripts = ->
for tag in document.getElementsByTagName('script') when tag.type is 'text/coffeescript' for script in document.getElementsByTagName 'script'
eval exports.compile tag.innerHTML if script.type is 'text/coffeescript'
if script.src
grindRemote script.src
else
grind script.innerHTML
null
if window.addEventListener if window.addEventListener
window.addEventListener 'load', processScripts, false addEventListener 'DOMContentLoaded', processScripts, false
else if window.attachEvent else
window.attachEvent 'onload', processScripts attachEvent 'onload', processScripts