opera.addEventListener()

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:

Registers a listener for an event specific to the injected script side. The listener needs to use the standard EventListener interface - a function with an Event object as its first argment, e.g. var myListener = function(event) {alert(event.type)};

Parameters:

  • type: The type of event to listen to; see the list of event types below.
  • listener: The function that will be called.
  • useCapture: Boolean. If true, the event listener is only added for the capture phase and target.

Syntax:

void addEventListener (<DOMString> type, <EventListener> listener, useCapture)

Example:

opera.addEventListener('BeforeScript', function(userJSEvent) {
  userJSEvent.element.text = userJSEvent.element.text.replace(/function\s+window\.onload\(/g, 'window.onload = function(');
}, false);

Event types:

BeforeExternalScript

Fired when a script element with a src attribute is encountered. The script element is available in the element attribute of the UserJSEvent. If cancelled, the external source is not loaded and the script element is not executed. In addition, if it is cancelled, the BeforeScript event will not fire.

BeforeScript

Fired before a script element is executed. The script element is available in the element attribute of the UserJSEvent. The content of the script is available in the text property of the script element, and is also writable:

UserJSEvent.element.text = UserJSEvent.element.text.replace(/!=\s*null/,'');

The BeforeScript event is fired for inline scripts as well as external scripts, including scripts with a type that Opera normally does not execute (such as VBScript). If cancelled, the script element is not executed.

AfterScript

Fired after a script element has finished executing. The script element is available in the element attribute of the UserJSEvent. Modifying the text property of the event object has no effect, unlike the BeforeScript event.

BeforeEvent

Fired before a regular event is fired, regardless of whether or not an event handler has been registered for that event. The regular event is available in the event attribute of the UserJSEvent. If cancelled, the regular event is not fired, its default action is performed, and any associated BeforeEventListener events are not fired.

BeforeEvent.{type}

Like BeforeEvent, but fired only for events of the specified type (e.g., BeforeEvent.click). As shown by the example, the {type} needs to be substituted for the type of event you want to listen for. See the Event handlers on elements, Document objects, and Window objects of the HTML5 specificaton for a complete list of events. Note: at present, not all event types are supported.

AfterEvent

Fired after a regular event has been fired and handled but before its default action has been performed. The regular event is available in the event attribute of the UserJSEvent. If cancelled, any attempts by a regular event handler to cancel the regular event will be ignored. The UserJSEvent object will also have a eventCancelled property, which will be set to true if any regular event handlers have cancelled the event.

AfterEvent.{type}

Like AfterEvent, but fired only for events of the specified type (e.g., AfterEvent.click). As shown by the example, the {type} needs to be substituted for the type of event you want to listen for. See the Event handlers on elements, Document objects, and Window objects of the HTML5 specificaton for a complete list of events you can listen for. Note: at present, not all event types are supported.

BeforeEventListener

Fired before a listener for a regular event is called. The regular event is available in the event attribute of the UserJSEvent, and its regular listener is available in the listener attribute of the UserJSEvent. If cancelled, the regular event listener will not be called.

BeforeEventListener.{type}

Like BeforeEventListener, but fired only for events of the specified type (for example, BeforeEventListener.click).

AfterEventListener

Fired after a listener for regular events is called. The UserJSEvent object contains references to the original event and listener that were defined on the page. If cancelled, any attempts by a regular event handler to cancel the regular event propagation will be ignored. The UserJSEvent object will also have the propagationStopped property, which will be set to true if any regular event handlers have cancelled the event propagation.

AfterEventListener.{type}

Like AfterEventListener, but fired only for events of the specified type (for example, AfterEventListener.click). As shown by the example, the {type} needs to be substituted for the type of event you want to listen for. See the Event handlers on elements, Document objects, and Window objects of the HTML5 specificaton for a complete list of events. Note: At present, not all event types are supported.

BeforeJavascriptURL

Fired before a javascript: URL is executed. The JavaScript code to be executed (everything after the javascript: in the URL) is available in the source attribute of the UserJSEvent, and is also writable. If cancelled, the javascript: URL is not executed.

AfterJavascriptURL

Fired after a javascript: URL is executed. The JavaScript code that was executed (everything after the javascript: in the URL) is available in the source attribute of the UserJSEvent, and any value returned by that code is available in the returnValue attribute of the UserJSEvent. The returnValue is also writable. If cancelled, any returned value will not be used as the source of a new page.

BeforeCSS

Fired before a CSS style sheet is parsed. The element attribute of the UserJSEvent refers either to the <style> or the <link> elements that include the styles in the document. For style sheets imported using a CSS import rule, a placeholder <link> element is returned, which provides access to the style sheet url, media, and other properties.

The content of the style sheet is available in the cssText property of the UserJSEvent object, and is also writable. Usage example follows:

opera.addEventListener('BeforeCSS', function(userJSEvent){
  userJSEvent.cssText = userJSEvent.cssText
    .replace(/-(moz|ms|webkit|o)-(border|text-overflow)/g,'$2')
    .replace(/-(moz|ms|webkit)-(gradient|transform|transition)/g,'-o-$2');
}, false);

If canceled, the style sheet element is not parsed and its styles are not applied to the page. While a BeforeCSS event is being processed for a given style sheet, changes to that style sheet — like changing textContent on the style element, will only be applied after the BeforeCSS event, if it is not canceled.

AfterCSS

Fired after a CSS style sheet is parsed, if the respective BeforeCSS event was not canceled. The element attribute of the UserJSEvent refers either to the <style> or the <link> elements that include the styles in the document. For style sheets imported using a CSS import rule, a placeholder <link> element is returned, which provides access to the style sheet url, media, and other properties.

The content of the style sheet is available in the cssText property of the UserJSEvent object. Modifying the cssText property of the event object triggers a new BeforeCSS event.

pluginInitialized

Fired after a plug-in instance has been initialized. The element which represents the plug-in instance is available in the element attribute of the UserJSEvent.

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

Comments

  • photo

    Martin Tournoij

    Saturday, May 12, 2012

    Perhaps you could link to the multi-page version of the HTML5 spec. My browser (yes, Opera ;-) ) had some serious trouble with the size of the single-page version ...

    Here's the link:
    http://dev.w3.org/html5/spec/webappapis.html#event-handlers-on-elements-document-objects-and-window-objects
  • photo

    QuHno

    Tuesday, July 10, 2012

    opera.addEventListener('BeforeEventListener.click', someFunctionFoo , false);

    throws:
    "Uncaught exception: TypeError: 'opera.addEventListener' is not a function"
    when used in the injected script of an extension.

    I used
    window.opera.addEventListener(...) 

    instead and it seems to work so far...
  • photo

    Dale Zhang

    Sunday, December 2, 2012

    But why 'DOMContentLoaded' is not listed here?
No new comments accepted.