That was an interesting one...
The part of the file you modified is responsible for computing the
'currentScript' property. On all browsers but IE this value is readily
available in the document.currentScript property. However, on IE this
property doesn't exist. Therefore, we have a work-around in place that
checks for the document.currentScript first, and if it is not there, we
attach an onload-listener on all scripts. Normally, the currently running
script will trigger that 'onLoad' handler first, thus giving us access to
the correct script (and thus the URI).
This mechanism worked against us when using a Dart script as a
content-script of a Chrome extension: for content-scripts the
'document.currentScript' is null, and we fell through to the IE
work-around. In many cases this still "works", because the web-page has
some kind of script, and Dart would just (wrongly) assume that the
web-pages script is the URI of the Dart script. However, in pages without
any script, or when the extension is triggered after all scripts have been
loaded the extension would simply not get any callback and not run main.
I have uploaded a patch that checks for 'document.currentScript'
differently, so that it also accepts 'null':
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
follows:
- we have a function (say 'computeCS') that computes the current script,
and then invokes a callback.
- we have a second function (say 'startMain') that invokes main.
the last lines basically are: computeCS(startMain);
computeCS was broken and never invoke its callback. With your change you
modified the script to become:
computeCS();
startMain();
Hope that helps.
Post by Janko ZelenkoHi Karl,
That would be great.
I'm using dart2js version 1.11.1.
I have attached the zipped version of the simple Chrome Extension in Dart
(only main() function with print).
Included is build folder which is what dart2js produces and build-working
folder with the changed code for content.dart.js which is working.
It is interesting that background.dart.js has the same code which is
working without the change.
Post by 'Karl Klose' via Dart MiscI would like to take a look at the problem, but I do not see it with a
simple test file I made. Can you share the code you are trying to compile
with me?
Do you know the version of dart2js you are using?
Post by Janko ZelenkoI did some debugging and I believe there is a bug in dart2js for the
chrome.dart.
At the end of the content.dart.js is the code that starts the main
function. I added *();* to both functions and the main function was
called after the change.
Before the change console.log calls from 1-5 were displayed, but not #6.
After the change all console.log calls from 1-6 are called and the print()
in main function is displayed.
// BEGIN invoke [main].
(function(callback) {
console.log('Begin invoke main 1');
if (typeof document === "undefined") {
callback(null);
return;
}
console.log('Begin invoke main 2');
if (document.currentScript) {
callback(document.currentScript);
return;
}
console.log('Begin invoke main 3');
var scripts = document.scripts;
function onLoad(event) {
for (var i = 0; i < scripts.length; ++i)
scripts[i].removeEventListener("load", onLoad, false);
callback(event.target);
}
console.log('Begin invoke main 4');
for (var i = 0; i < scripts.length; ++i)
scripts[i].addEventListener("load", onLoad, false);
console.log('Begin invoke main 5');
})*(); // <-- Added*
(function(currentScript) {
console.log('Begin invoke main 6');
init.currentScript = currentScript;
if (typeof dartMainRunner === "function")
dartMainRunner(T.main, []);
else
T.main([]);
})*(); // <-- Added*
// END invoke [main].
Post by Janko ZelenkoHi Chih Chiu,
Have you managed to run the content script dart file? Please share the
solution if you have found one.
I'm having the same problem as you. Background file runs, but not the
content script file.
It looks like all the examples are using background file and none uses
content_scripts section in the manifest.json.
Post by Chih ChiuI am trying to write a Chrome extension using Dart. So far everything
goes well except for the content script --- the "main" function in the
content script dart file does not seem to run.
To be more specific, first of all Dartium cannot be used since giving
a dart file in the "js" rule in the manifest caused Dartium to complain; I
next tried to compile the dart file (with csp: true) then make the manifest
to include the compiled js file directly --- then I'm stuck, it seems that
no matter what I try, the (compiled) "main" function just does not run.
Any suggestions?
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry
Pratchett
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.