Discussion:
[dart-misc] Using Dart in Chrome extension content script does not run "main"?
Chih Chiu
2014-10-14 18:13:22 UTC
Permalink
I 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 an email to misc+unsubscribe-dYxm/***@public.gmane.org
Janko Zelenko
2015-07-13 11:56:53 UTC
Permalink
Hi 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 Chiu
I 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 an email to misc+***@dartlang.org.
Janko Zelenko
2015-07-14 08:10:00 UTC
Permalink
I 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 Zelenko
Hi 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 Chiu
I 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 an email to misc+***@dartlang.org.
'Karl Klose' via Dart Misc
2015-07-14 08:46:42 UTC
Permalink
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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 an
--
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.
Janko Zelenko
2015-07-14 09:48:51 UTC
Permalink
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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 an
--
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.
'Florian Loitsch' via Dart Misc
2015-07-15 13:28:14 UTC
Permalink
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 Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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.
Janko Zelenko
2015-07-16 08:31:46 UTC
Permalink
Hi Florian,

Thank you for the great explanation!
Post by 'Florian Loitsch' via Dart Misc
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'
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
- 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
computeCS();
startMain();
Hope that helps.
Post by Janko Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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.
properius
2015-07-29 10:46:26 UTC
Permalink
I used what you explained to write a temporary transformer for chrome
extensions in Dart. I hope your fix will be released in the next stable
version of the SDK.

After building the extension's content script main function is called. I'm
trying to use window.onLoad() and I get an error that looks like it is
related to the way currentScript is used in isolate_helper.dart (function
computeThisScript(); line 766). Could it be that this is specific to chrome
extensions in Dart like the problem with calling content script's main
function?

I get: "Uncaught TypeError: Cannot read property 'isWorker' of undefined"
when extension is run in Chrome.

main() {
print('Start: content-script');
window.onLoad.listen((_) => print('onLoad'));
}


When I comment out the window.onLoad line the print() message is displayed.
If window.onLoad line is executed the print() message is not displayed.
Only the exception message is shown.

Am I doing something wrong? I'm trying to port my existing JavaScript
extension to Dart.

I have attached sample extension and the code for chrome transformer fix if
somebody wants to use stable dart2js (1.11.1).
Post by 'Florian Loitsch' via Dart Misc
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'
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
- 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
computeCS();
startMain();
Hope that helps.
Post by Janko Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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.
properius
2015-07-30 13:51:16 UTC
Permalink
One possible solution is to use "run_at": "document_end" instead of
"document_start" in the manifest.json:

"content_scripts": [
{
"js": ["content.dart.js"],
"run_at": "document_end"
}
],


It would be great if

document.onLoad.listen();


could be used.

I'm I using it incorrectly or is this due to the way how dart2js prepares
the content script for the extension?
Post by properius
I used what you explained to write a temporary transformer for chrome
extensions in Dart. I hope your fix will be released in the next stable
version of the SDK.
After building the extension's content script main function is called. I'm
trying to use window.onLoad() and I get an error that looks like it is
related to the way currentScript is used in isolate_helper.dart (function
computeThisScript(); line 766). Could it be that this is specific to chrome
extensions in Dart like the problem with calling content script's main
function?
I get: "Uncaught TypeError: Cannot read property 'isWorker' of undefined"
when extension is run in Chrome.
main() {
print('Start: content-script');
window.onLoad.listen((_) => print('onLoad'));
}
When I comment out the window.onLoad line the print() message is
displayed. If window.onLoad line is executed the print() message is not
displayed. Only the exception message is shown.
Am I doing something wrong? I'm trying to port my existing JavaScript
extension to Dart.
I have attached sample extension and the code for chrome transformer fix
if somebody wants to use stable dart2js (1.11.1).
Post by 'Florian Loitsch' via Dart Misc
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'
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
- 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
computeCS();
startMain();
Hope that helps.
Post by Janko Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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
--
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.
'Florian Loitsch' via Dart Misc
2015-07-31 17:55:13 UTC
Permalink
Sorry about that. Extensions are hard to test, and don't behave like normal
programs. By changing the way the currentScript is computed we enabled a
new code-path (also needing async/isolate) which leads to a null-pointer
exception.

We are investigating the best solution to this problem (see [0] for a first
stab at it).
For now, an easy work-around is to change the argument that is passed to
the callback that starts main. Since you already change the line before, it
shouldn't take too much, to change the next one too.

Instead of:

if (typeof document.currentScript != 'undefined') {
callback(document.currentScript);
return;
}

change it to:

if (typeof document.currentScript != 'undefined') {
callback(document.currentScript || "extension");
return;
}

That should do the trick.
Again sorry, that you run into these issues.
// florian

[0] https://codereview.chromium.org/1264543004/
Post by properius
One possible solution is to use "run_at": "document_end" instead of
"content_scripts": [
{
"js": ["content.dart.js"],
"run_at": "document_end"
}
],
It would be great if
document.onLoad.listen();
could be used.
I'm I using it incorrectly or is this due to the way how dart2js prepares
the content script for the extension?
Post by properius
I used what you explained to write a temporary transformer for chrome
extensions in Dart. I hope your fix will be released in the next stable
version of the SDK.
After building the extension's content script main function is called.
I'm trying to use window.onLoad() and I get an error that looks like it is
related to the way currentScript is used in isolate_helper.dart (function
computeThisScript(); line 766). Could it be that this is specific to chrome
extensions in Dart like the problem with calling content script's main
function?
I get: "Uncaught TypeError: Cannot read property 'isWorker' of undefined"
when extension is run in Chrome.
main() {
print('Start: content-script');
window.onLoad.listen((_) => print('onLoad'));
}
When I comment out the window.onLoad line the print() message is
displayed. If window.onLoad line is executed the print() message is not
displayed. Only the exception message is shown.
Am I doing something wrong? I'm trying to port my existing JavaScript
extension to Dart.
I have attached sample extension and the code for chrome transformer fix
if somebody wants to use stable dart2js (1.11.1).
Post by 'Florian Loitsch' via Dart Misc
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'
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
- 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
computeCS();
startMain();
Hope that helps.
Post by Janko Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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,
--
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
--
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.
properius
2015-07-31 18:42:40 UTC
Permalink
Thank you Florian!!!

Your fix works. I got back my hopes to be able to write Chrome extensions
in Dart.

Don't apologize. I'm happy you know what is going on and you even provided
a workaround.

Thank you again (you apologized twice too :-)
Post by 'Florian Loitsch' via Dart Misc
Sorry about that. Extensions are hard to test, and don't behave like
normal programs. By changing the way the currentScript is computed we
enabled a new code-path (also needing async/isolate) which leads to a
null-pointer exception.
We are investigating the best solution to this problem (see [0] for a
first stab at it).
For now, an easy work-around is to change the argument that is passed to
the callback that starts main. Since you already change the line before, it
shouldn't take too much, to change the next one too.
if (typeof document.currentScript != 'undefined') {
callback(document.currentScript);
return;
}
if (typeof document.currentScript != 'undefined') {
callback(document.currentScript || "extension");
return;
}
That should do the trick.
Again sorry, that you run into these issues.
// florian
[0] https://codereview.chromium.org/1264543004/
Post by properius
One possible solution is to use "run_at": "document_end" instead of
"content_scripts": [
{
"js": ["content.dart.js"],
"run_at": "document_end"
}
],
It would be great if
document.onLoad.listen();
could be used.
I'm I using it incorrectly or is this due to the way how dart2js prepares
the content script for the extension?
Post by properius
I used what you explained to write a temporary transformer for chrome
extensions in Dart. I hope your fix will be released in the next stable
version of the SDK.
After building the extension's content script main function is called.
I'm trying to use window.onLoad() and I get an error that looks like it is
related to the way currentScript is used in isolate_helper.dart (function
computeThisScript(); line 766). Could it be that this is specific to chrome
extensions in Dart like the problem with calling content script's main
function?
I get: "Uncaught TypeError: Cannot read property 'isWorker' of
undefined" when extension is run in Chrome.
main() {
print('Start: content-script');
window.onLoad.listen((_) => print('onLoad'));
}
When I comment out the window.onLoad line the print() message is
displayed. If window.onLoad line is executed the print() message is not
displayed. Only the exception message is shown.
Am I doing something wrong? I'm trying to port my existing JavaScript
extension to Dart.
I have attached sample extension and the code for chrome transformer fix
if somebody wants to use stable dart2js (1.11.1).
Post by 'Florian Loitsch' via Dart Misc
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'
https://codereview.chromium.org/1224363004/
Small explanation why your patch worked: the way we start main is as
- 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
computeCS();
startMain();
Hope that helps.
Post by Janko Zelenko
Hi 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 Misc
I 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 Zelenko
I 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 Zelenko
Hi 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 Chiu
I 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,
--
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
--
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.
Loading...