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

Add script source to GM_info #1738

Closed
wants to merge 1 commit into from
Closed

Add script source to GM_info #1738

wants to merge 1 commit into from

Conversation

aidanhs
Copy link
Contributor

@aidanhs aidanhs commented May 2, 2013

Consider issue #1529, where the proposed solution is to bootstrap their coffeescript compilation with a few lines of JS. However, when uploading to userscripts.org it feels neater to have things in one file but there's (currently) only one way to do this for #1529, and that's to turn the coffeescript into a JS string.

As JS does not have any multiline strings this either means one massively long line or ugly splits at wrapping boundaries. This is not an acceptable solution, as it makes inline editing near-impossible.

This proposal allows direct access to the script source meaning one could define a 'custom metadata' block (really just a block comment with some searchable strings) like

/*
COFFEE_START
alert "I knew it!" if true?
COFFEE_END
*/

which you can then extract with a regular expression (or String.indexOf) and pass to an included compiler. You now allow inline editing.

This additionally makes it easier to have CSS styles and HTML fragments inline in the userscript.
HTML fragments are particularly important if (for example) wanting to use an MVC framework like angularjs or a templating engine like mustache/handlebars - it's tiresome having to build a template with JS to then convert it to HTML and makes editing more difficult. This would allow you to have html fragments in your userscript to be loaded as strings at initialisation, an inline getResourceText if you like to think of it like that.

(thinking about it, the inline getResourceText is an interesting idea but I consider this a more fundamental and flexible idea)

The chosen name of the property (scriptSource) is compatible with tampermonkey.

@aidanhs
Copy link
Contributor Author

aidanhs commented May 2, 2013

Related issues (but never discussed this exact feature): #1452, #1512

@pjeby
Copy link

pjeby commented May 3, 2013

Note that if you want inline resource text, you can already use XML literals with CDATA, e.g.:

var myresource = (<><![CDATA[
... anything you want, as long as it never has two "]" followed by ">" in it ...
]]></>).toString();

This also works to do CoffeeScript, as demonstrated in this gist: https://gist.github.com/saihoooooooo/3447066 (though that example's not using CDATA).

(Note: Github's syntax highlighter and indeed most JavaScript syntax highlighters aren't smart enough to handle XML literals, let alone CDATA, but I promise this is valid JavaScript nonetheless. If you're lucky enough to have an editor whose highlighting is buggy in just the right way, you might get valid highlighting of the contents of the literal.)

@arantius
Copy link
Collaborator

arantius commented May 3, 2013

No, you can't. https://developer.mozilla.org/en-US/docs/E4X

"Warning: E4X is obsolete. It has been disabled by default for webpages (content) in Firefox 17, disabled by default for chrome in Firefox 20, and has been removed in Firefox 21."

E4X Is what made XML inside JS work. It no longer exists in Firefox.

@pjeby
Copy link

pjeby commented May 3, 2013

Well, darn. I learn something new every day... but not always something good. ;-)

@aidanhs
Copy link
Contributor Author

aidanhs commented May 7, 2013

Example of something this makes prettier - https://gist.github.com/aidanhs/5534196

Works fine in tampermonkey and greasemonkey with this patch applied.

@marnen
Copy link

marnen commented May 10, 2013

@aidanhs Oh, that looks sexy.

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

Successfully merging this pull request may close these issues.

None yet

4 participants