Skip to content

Instantly share code, notes, and snippets.

@aidanhs
Created May 7, 2013 16:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidanhs/5534196 to your computer and use it in GitHub Desktop.
Save aidanhs/5534196 to your computer and use it in GitHub Desktop.
Why scriptSource is useful in greasemonkey (https://github.com/greasemonkey/greasemonkey/pull/1738)
// ==UserScript==
// @name CoffeeTest
// @namespace test
// @version 0.1
// @grant none
// @require http://jashkenas.github.io/coffee-script/extras/coffee-script.js
// @match http://example.iana.org/
// ==/UserScript==
/*
======================INLINE_RESOURCE_BEGIN======================
***********RESOURCE_START=COFFEE*************
make_css = (css) ->
elt = document.createElement 'style'
elt.type = 'text/css'
elt.textContent = css
elt
myStyle = make_css resource['someCSS']
document.head.appendChild myStyle
*************RESOURCE_END*************
*************RESOURCE_START=someCSS*************
h1 {
color: red;
}
a {
font-size: 20px;
}
*************RESOURCE_END*************
======================INLINE_RESOURCE_END======================
*/
function getInlineResources() {
var resource = {}, len, match, resourceBlocks,
inlineResourcesMatch = (/^=+INLINE_RESOURCE_BEGIN=+$([\s\S]*?)^=+INLINE_RESOURCE_END=+$/m).exec(GM_info.scriptSource);
resourceBlocks = (inlineResourcesMatch && inlineResourcesMatch[1].match(/^\**RESOURCE_START[\s\S]*?^\**RESOURCE_END\**$/mg)) || null;
len = (resourceBlocks && resourceBlocks.length) || 0;
for (var i = 0; i < len; i++) {
match = (/^\**RESOURCE_START=(.*?)\**$\s*^([\s\S]*)^\**RESOURCE_END\**$/m).exec(resourceBlocks[i]);
resource[match[1]] = match[2];
}
return resource;
}
var resource = getInlineResources();
this.CoffeeScript.eval(resource['COFFEE']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment