Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@required scripts without final semicolon cause problems #1491

Closed
FinnE opened this issue Jan 3, 2012 · 6 comments
Closed

@required scripts without final semicolon cause problems #1491

FinnE opened this issue Jan 3, 2012 · 6 comments
Milestone

Comments

@FinnE
Copy link

FinnE commented Jan 3, 2012

Greasemonkey 0.9.13 on Firefox 8.0.1.

Speculation:

It appears that @required scripts that do not end in a semicolon can cause problems in the user script.

Steps to reproduce:

  1. Create a new user script which @includes *.
  2. @require the minified jQuery and jQuery-UI 1.8.17 (in that order) from Google APIs (http://code.google.com/apis/libraries/devguide.html#jquery).
  3. Put an alert call inside an anonymous wrapper function in the user script, e.g.: (function(){alert("Hello")})();
  4. Install and run the script.

Expected result:

  • Alert is called, producing a dialogue box.

Actual result:

  • Nothing happens.

Workaound:

  • Add a semicolon to the start of the user script.

Conclusion:

While not hard to work around, the problem is hard to identify, as it produces no errors or obvious side effects (besides the user script doing nothing).

@jerone
Copy link
Contributor

jerone commented Jan 3, 2012

You can use GitHub Gist to create an example UserScript.

@FinnE
Copy link
Author

FinnE commented Jan 4, 2012

Gisted here: https://gist.github.com/1558869

@arantius
Copy link
Collaborator

Can't reproduce. https://gist.github.com/1677686

Installs and runs both requires, none of the 3 files have a semicolon in them. So it's not just semicolons. It's something else about the contents of those two URLs, as the link you posted (on the 4th) definitely does exhibit some weird behavior. Except your description isn't perfectly correct. There's not "no effect". I see, in the error console:

Error: function (a, b) {...severely truncated...}(jQuery) is not a function
Source file: file:///.../gm_scripts/RequireExample/RequireExample.user.js
Line: 10

I also see everything work correctly when I remove the anonymous function wrapper. So: the real cause is that the statement that jquery-ui runs has a return value. The first non-whitespace non-comment character in your script is an open parenthesis, which javascript interprets in this context as a function call to the return value of the still-open statement (semicolon insertion logic fail!).

Since it should be universally safe to append semicolons to working requires, I think we'll just do that, to address this class of failure.

@arantius
Copy link
Collaborator

FYI: http://inimino.org/~inimino/blog/javascript_semicolons

Firstly, a semicolon is optional only where there is a line break, a closing brace, or the end of the program. Semicolons are not optional between statements appearing on the same line. Additionally, a semicolon is not implied at the end of a line if the first token of the subsequent line can be parsed as part of the same statement.

(Emphasis mine.) So, in this particular case, where there was only whitespace then an open paren, this is valid syntax, so the semicolon is not inserted. But it's not a valid value to be calling as a function, so the result is a runtime error.

@arantius
Copy link
Collaborator

Reporters: Please test and report whether this build fixes your issues:
https://github.com/downloads/arantius/greasemonkey/greasemonkey-0.9.15.xpi

@FinnE
Copy link
Author

FinnE commented Jan 26, 2012

Confirmed that 0.9.15 fixes the issue.

I do wonder why I didn't get any errors in my console though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants