OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
6 | 6 |
7 class MainCallStubGenerator { | 7 class MainCallStubGenerator { |
8 final Compiler compiler; | 8 final Compiler compiler; |
9 final JavaScriptBackend backend; | 9 final JavaScriptBackend backend; |
10 final CodeEmitterTask emitterTask; | 10 final CodeEmitterTask emitterTask; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 // This code finds the currently executing script by listening to the | 45 // This code finds the currently executing script by listening to the |
46 // onload event of all script tags and getting the first script which | 46 // onload event of all script tags and getting the first script which |
47 // finishes. Since onload is called immediately after execution this should | 47 // finishes. Since onload is called immediately after execution this should |
48 // not substantially change execution order. | 48 // not substantially change execution order. |
49 return js.statement(''' | 49 return js.statement(''' |
50 (function (callback) { | 50 (function (callback) { |
51 if (typeof document === "undefined") { | 51 if (typeof document === "undefined") { |
52 callback(null); | 52 callback(null); |
53 return; | 53 return; |
54 } | 54 } |
55 if (document.currentScript) { | 55 // When running as a content-script of a chrome-extension the |
| 56 // 'currentScript' is `null` (but not undefined). |
| 57 if (typeof document.currentScript != 'undefined') { |
56 callback(document.currentScript); | 58 callback(document.currentScript); |
57 return; | 59 return; |
58 } | 60 } |
59 | 61 |
60 var scripts = document.scripts; | 62 var scripts = document.scripts; |
61 function onLoad(event) { | 63 function onLoad(event) { |
62 for (var i = 0; i < scripts.length; ++i) { | 64 for (var i = 0; i < scripts.length; ++i) { |
63 scripts[i].removeEventListener("load", onLoad, false); | 65 scripts[i].removeEventListener("load", onLoad, false); |
64 } | 66 } |
65 callback(event.target); | 67 callback(event.target); |
66 } | 68 } |
67 for (var i = 0; i < scripts.length; ++i) { | 69 for (var i = 0; i < scripts.length; ++i) { |
68 scripts[i].addEventListener("load", onLoad, false); | 70 scripts[i].addEventListener("load", onLoad, false); |
69 } | 71 } |
70 })(function(currentScript) { | 72 })(function(currentScript) { |
71 #currentScript = currentScript; | 73 #currentScript = currentScript; |
72 | 74 |
73 if (typeof dartMainRunner === "function") { | 75 if (typeof dartMainRunner === "function") { |
74 dartMainRunner(#mainCallClosure, []); | 76 dartMainRunner(#mainCallClosure, []); |
75 } else { | 77 } else { |
76 #mainCallClosure([]); | 78 #mainCallClosure([]); |
77 } | 79 } |
78 })''', | 80 })''', |
79 {'currentScript': currentScriptAccess, | 81 {'currentScript': currentScriptAccess, |
80 'mainCallClosure': mainCallClosure}); | 82 'mainCallClosure': mainCallClosure}); |
81 } | 83 } |
82 } | 84 } |
OLD | NEW |