Skip to content

Instantly share code, notes, and snippets.

@naeramarth7
Created February 28, 2014 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naeramarth7/9268770 to your computer and use it in GitHub Desktop.
Save naeramarth7/9268770 to your computer and use it in GitHub Desktop.
JS / Dart: Load alternative JS file from /js/*.js instead of /dart/*.dart.js
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
(function() {
// Bootstrap support for Dart scripts on the page as this script.
if (navigator.userAgent.indexOf('(Dart)') === -1) {
// TODO:
// - Support in-browser compilation.
// - Handle inline Dart scripts.
// Fall back to compiled JS. Run through all the scripts and
// replace them if they have a type that indicate that they source
// in Dart code (type="application/dart").
var scripts = document.getElementsByTagName("script");
var length = scripts.length;
for (var i = 0; i < length; ++i) {
if (scripts[i].type == "application/dart") {
// Remap foo.dart to foo.dart.js.
if (scripts[i].src && scripts[i].src !== '') {
var script = document.createElement('script');
script.src = scripts[i].src
.replace(/\/dart\//, '/js/')
.replace(/\.dart(?=\?|$)/, '.js');
var parent = scripts[i].parentNode;
// TODO(vsm): Find a solution for issue 8455 that works with more
// than one script.
document.currentScript = script;
parent.replaceChild(script, scripts[i]);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment