From db181e2a367deb46fbb35b4e9e7214319f8a2267 Mon Sep 17 00:00:00 2001 From: satyr Date: Wed, 18 Aug 2010 09:35:52 +0900 Subject: [PATCH] enabled remote scripts in browsers --- lib/coffee-script.js | 47 ++++++++++++++++++++++++++++++---------- src/coffee-script.coffee | 31 ++++++++++++++++++-------- 2 files changed, 57 insertions(+), 21 deletions(-) mode change 100644 => 100755 lib/coffee-script.js mode change 100644 => 100755 src/coffee-script.coffee diff --git a/lib/coffee-script.js b/lib/coffee-script.js old mode 100644 new mode 100755 index 010fdee9..96e7372a --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -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); } } })(); diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee old mode 100644 new mode 100755 index 3d727212..c82f1816 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -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