[Greasemonkey] Long literal strings of html

Edward Lee edilee at gmail.com
Wed Apr 20 23:24:22 EDT 2005

Here's another function to add to each each of your scripts if you
want to use long string literals. At least for me, I'll have the
"overlay" function, and this one.. and maybe xPath even though the
overlay covers that functionality already. [Btw, is there an easier
way of getting in helper functions that you use across scripts into
some central script?]

var GM_getString = (function() {
  var hash = {}, script = GM_getSelf2();
  var match, re = /\/\*=\s*([\w\$][\w\d\$]+)\r?\n?([\s\S]+?)\r?\n?=\*\//g;

// this uses /*= var ... =*/ style literals
// for steve_k style ==String:var==...==/String==
// /==String:([\w\$][\w\d\$]+)==\r?\n?([\s\S]+?)\r?\n?==\/String==/g

  while (match = re.exec(script)) {
    hash[match[1]] = match[2];

  return function(key) {
    return hash[key];
}) ();

On 4/20/05, Julien Couvreur <julien.couvreur at gmail.com> wrote:
> Also in the other proposed solution, /*= can appear by accident if you
> format a comment with /*===============

Not quite.. it matches /*= followed by any number of spaces followed
by a valid javascript identifier. So /*=== would fail to match.

Note to Steve, I added \r?\n? to strip out at most one newline which
will most likely come from the newline after String:..==, but for
people who want the newline, just stick another blank line after ==.
Also, the ==String method doesn't really check if it's inside a
comment block, but most likely it won't be anywhere else ;) Maybe
except for a string..


More information about the Greasemonkey mailing list