Basically I need to translate the following piece of JavaScript into CoffeeScript.
location.href = "javascript:(" + function() {
window.onbeforeunload = function() {
notifyBackground(collectData());
return undefined;
};
} + "){}";
The return “undefined” is important because the browser will ask the user to confirm that they want to leave the page if I remove it.
You might be wondering wtf I’m doing. Basically, it’s a location hack for Firefox extension development.
I’ve tried doing the following:
location.href = "javascript:(" + ->
window.onbeforeunload = ->
notifyBackground(collectData())
return undefined
+ ")()"
But that turns into:
location.href = "javascript:(" + function() {
return window.onbeforeunload = function(e) {
notifyBackground(collectData());
return void 0;
};
};
return +")()";
Using js2coffee.org gives me this:
location.href = "javascript:(" + ->
window.onbeforeunload = ->
notifyBackground collectData()
"undefined"
+ "){}"
If I run that through CoffeeScript I get this JS output (which is wrong).
location.href = "javascript:(" + function() {
return window.onbeforeunload = function() {
notifyBackground(collectData());
return "undefined";
};
};
return +"){}";
Is this close enough?
Compiles to:
Note the backticks around the undefined to avoid the
void 0, just in case that makes a difference (And I’m not sure if it does?)EDIT
In the Chrome console
undefined == void 0istrue, so maybe thecould simply be: