opera.defineMagicFunction()

By Opera Software

From Opera 15 onward, Opera 11 & 12’s extension format is no longer supported, and instead, we’ve switched to Chromium’s extension model. Check out our new documentation for developing extensions for Opera 15 and higher and start building your own extensions.

Description:

This method can be used to override global functions defined by regular scripts in a page.

Parameters:

  • name: String giving the name of the function to be overridden.
  • implementation: Function to be run in place of the function defined by the page. The function will be passed the following parameters.
    • A reference to the real function defined by the page.
    • The object that would have been referred to by the this keyword in the real function.
    • Any parameters that would have been passed to the real function (each is passed as a separate parameter to the magic function).

Syntax:

void defineMagicFunction (<DOMString> name, <Function> implementation)

Example:

window.opera.defineMagicFunction('getLayer', function (oRealFunc, oThis, oParam1, oParam2) {
  return oParam1.getElementById(oParam2).style;
});

// This example overrides a function f. If the this object refers to the window object, it returns false.
// Otherwise it just runs the real function instead:
window.opera.defineMagicFunction('f', function (real, thisObject) {
  if (thisObject == window) {
    return false;
  } else {
    return real.apply(thisObject, Array.prototype.slice.call(arguments, 2));
  }
});

This article is licensed under a Creative Commons Attribution 3.0 Unported license.

Comments

  • photo

    Matthias N

    Wednesday, August 8, 2012

    Calling a magic function will fail, if this call happens a second time by recursion.
    It doen't matter how deep this recursion is nested.
    As there exists no possibility to undefine a magic function, please do not use it on abovementioned functions.

    For a more detailed, but still unresolved discussion, check:
    http://my.opera.com/community/forums/topic.dml?id=1429632
No new comments accepted.