[Greasemonkey] Removing a script

Stuart Langridge stuart.langridge at gmail.com
Thu Apr 14 19:20:56 EDT 2005


On 4/14/05, Jeremy Dunck <jdunck at gmail.com> wrote:
> On 4/14/05, Stuart Langridge <stuart.langridge at gmail.com> wrote:
> > It works. Have to have an explicit check inside the user script to
> > make sure that it doesn't run forever (not sure why it was doing
> > that). Now to remove all the script URLs and replace them with working
> > ones. It would actually probably be easier to just go and find the
> > developers who made this IE-only web app (and charged us a fortune for
> > it) and have them all boiled in oil, perhaps.
> 
> Yippee!  A new pattern.  ;-)  Where's the link?

My userscript looks like this. It's designed to run on the URL
http://selene/InterAction/home. It checks for that URL, since
otherwise it seems to get triggered again after the data: URL loads,
leading to an infinite refresh loop. The userscript removes a
troublesome script element, removes a troublesome included script src,
inserts a new replacement (which is on the machine "deltafour"),
inserts a base href element, and reloads the page as a data URL.

Immediate problem: I don't know why the infinite refresh happens, so I
don't know a more generalised fix than checking explicitly for a URL.
I did think about adding a flag to the HTML before grabbing it for the
data url (say, add a greasemonkey="yes" to the body tag, and check for
that) but then the Appalling Hack Police came round and beat me up, so
I didn't do it.

I should really replace the old script element with the new one,
rather than removing and then adding again, but I didn't for
historical reasons. :)

sil

(function() {
    if (location.href != 'http://selene/InterAction/home') return;

    // remove first fucked script
    var s = document.getElementsByTagName('script')[14];
    s.parentNode.removeChild(s);

    var scr = document.getElementsByTagName('script');
    for (var i=scr.length-1;i>=0;i--) {
      if (scr[i].src.match(/.*\/IA_SCRIPTS\/InterAction\.js$/)) {
        scr[i].parentNode.removeChild(scr[i]);
      }
    }
    // add reference to fixed script
    var s = document.createElement("script");
    s.src = "http://deltafour/apps/sil/random/InterAction.fixed.js";
    document.getElementsByTagName("head")[0].insertBefore(s,document.getElementsByTagName("head")[0].firstChild);

    // add base href
    var b = document.createElement("base");
    b.setAttribute("href","http://selene/interaction/");
    document.getElementsByTagName("head")[0].insertBefore(b,document.getElementsByTagName("head")[0].firstChild);

    // serialise into data url
    var htmlsrc =
'<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>';
    var dataurl = 'data:text/html;charset=utf-8,'+htmlsrc;
    location.href = dataurl;
})();


More information about the Greasemonkey mailing list