2

Is it possible to modify/remove an <body onload="..."> event via GreaseMonkey?

The code in question is this:

<body onload="if (document.body.scrollIntoView &amp;&amp; (window.location.href.indexOf('#') == -1 || window.location.href.indexOf('#post') &gt; -1)) { fetch_object('currentPost').scrollIntoView(true); }">

I would like to completely prevent it from executing (it is an annoying vBulletin feature to scroll to the post even if no hash is present in the URL which apparently also triggers when using the "back" button).

4

1 回答 1

2

Yes, you can intercept <body onload="..." ... by overwriting it as soon as the DOM is interactively available to Greasemonkey (Probably can be made to work in Chrome, too, but not tested).

This works:

// ==UserScript==
// @name        _Block inline onload function
// @namespace   _pc
// @include     http://YOUR_SERVER/YOUR_PATH/*
// @run-at      document-start
// ==/UserScript==

document.addEventListener ("readystatechange", FireWhenReady, true);

function FireWhenReady () {
    this.fired  = this.fired || false;

    if (    document.readyState != "uninitialized"
        &&  document.readyState != "loading"
        &&  ! this.fired
    ) {
        this.fired = true;

        document.body.onload  = function () {
            console.log ("body onload intercepted.");
        };
    }
}
于 2012-05-26T20:26:41.847 回答