[Greasemonkey] access to injected scripts?

Aaron Boodman zboogs at gmail.com
Fri May 27 20:30:15 EDT 2005


We specifically remove the <script> tags responsible for injecting
greasemonkey scripts after they are evaluated because we don't want
them to show up in applications that are introspective on the DOM.

The most common case of this is the rich text areas that are in lots
of CMS's. They work by allowing you to visually modify the in memory
DOM, and then serializing that to a string. Greasemonkey <script> tags
should not show up in that DOM, nor should they show up in the
resultant strings. So we just strip out the tags right after they are
evaluated. You can see the details in the runBrowserScript function of
browser.xul here:

  function runBrowserScript(doc, jscode) {
    if(doc && doc.body) {
      var elm = doc.createElement("script");
      elm.appendChild(doc.createTextNode(jscode));
      doc.body.appendChild(elm);
      doc.body.removeChild(elm);
    }
  }

http://www.mozdev.org/source/browse/greasemonkey/src/content/browser.js?rev=1.1&content-type=text/x-cvsweb-markup

-- 
Aaron 

-- 
Aaron

On 5/27/05, Bill Donnelly <donnelly at snowcrest.net> wrote:
> I'm confused about the comments in this previous comment.
> (below) Specifically this line:
> 
>    gm_content = document.documentElement.innerHTML;
> 
> When I execute that, I don't get the injected scripts, so
> I was wondering what's going on. afaik, there is no way
> to get to the injected scripts from outside Gm. We were
> discussing this on another thread, and I lost track of it.
> 
> Anyway, anyone have any clarifications?
> 
> (I was also wondering about Jeremy's comment about
> "The script tags added by GM are immediately removed."
> and what that means and how it is done and why and what
> that means regards accessing them "after the fact" ---
> such as, let's say, wanting to write a GmScript that lists
> all of the loaded GmScripts for a page, and/or maybe
> displays their source)
> 
>  > On 5/25/05, Jeremy Dunck <jdunck at gmail.com> wrote:
>  >
>  > >> The script tags added by GM are immediately removed.  You shouldn't
>  > >> see them, unless you're handling a mutation event on their injection.
>  >
>  > I'm doing something like this:
>  >
>  >    gm_content = document.documentElement.innerHTML;
>  >
>  > Now gm_content has the document *and* all the various GM scripts. So,
>  > basically what I need is the content of the page after all GM scripts
>  > have done their cool jobs, but *without* the scripts themselves.
>  >
>  > Like Mark said, I could probably use DOMParser to play around, but
>  > then that's not going to be a perfect solution, would it? Prolly
>  > there's some easier way?
>  >
>  > -- Premshree Pillai http://www.livejournal.com/users/premshree/
> 
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list