[Greasemonkey] Long literal strings of html

Steve Krulewitz shooz at mm.st
Sun Apr 17 19:12:29 EDT 2005


And borrowing the magical [\s\S]+? from Edward's regular expression,
here is a version of this I wrote to work from within a script.  It
searches the injected script for blocks that looks like:

/*
==String:<name>==
line1
line2...
==/String==

==String:<name>==
line1
line2...
==/String==
*/

function getStrings() {
  var strings = {};
  var scripts = document.getElementsByTagName("script");
  for(var i = 0; i < scripts.length; i++) {
    var s = scripts[i].innerHTML;
    if(s.substring(0, 2) == "(f") {
      var re = new RegExp("==" + "String:(.*)==([\\s\\S]+?)==/String" +
"==", "g");
      var a = re.exec(s);
      while(a != null) {
        strings[a[1]] = a[2];
        a = re.exec(s);
      }
    }
  }
  return strings;
}

The way I delimited things are a bit different than what Edward did --
my version lets you group multiple blocks inside a single comment rather
than having a separate comment per block.  This has the downside of
problems when the delimiter text appears in the code (as I discovered
when writing this -- note how the regular expression string is broken up
to prevent this).

cheers,
-steve


More information about the Greasemonkey mailing list