We write cross-browser UserJS. Example 2: redefine alert ()

    Beginning: Example No. 1: changing the Yandex logo. The

    last time I was asked: " Do you have access to page variables from UserJS? ".

    The answer is yes.

    I will show an example with the alert () function. alert - page variable, window method. Instead, alert can be any other variable.

    Opera and Safari GreaseKit

    window.alert = function () {...}


    window.wrappedJSObject.alert = function(){ ... }

    Remember window.wrappedJSObject from a previous article? Not? Then goto " Example No. 1: changing the Yandex logo ."

    Option working in Opera, Firefox and Safari GreaseKit:

    var w = window.wrappedJSObject || window;
    w.alert = function () {...}

    Nice alert , the script I made uses this exact code.

    Google chrome

    In Google Chrome 3, you could access the page window through contentWindow. Chrome 4 removed this for security reasons . They say write like this:

    location.href = "javascript: window.alert = function () {...}

    Note: I do not see any reason to write UserJS for Chrome. It is necessary to write .CRX extensions , they can update. This is the same UserJS, but in the archive (plus a trifle like manifest.json). Therefore, I will not write about Chrome yet.

    Continued: Example # 3: TwitPic thumbnails

    Also popular now: