Created
May 7, 2013 16:53
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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