opera.extension.getFile()

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:

Gets a file within an extension package.

Parameters:

  • path: The path of the file to retrieve, within the extension package. Paths beginning with a forward slash (/) are absolute paths and refer to the extension's root directory.

Syntax:

File getFile (<String> path)

Example:

This demonstrates how a JavaScript library—jQuery in this case—can be added to a page for an injected script to use.

//
// An injected script (e.g. includes/injected.js)
//

window.addEventListener('DOMContentLoaded', function() {
    // Path to the library:
    var path = '/scripts/jquery.min.js';

    function addLibrary(path, callback) {
        // Get the library resource
        var fileObj = opera.extension.getFile(path);

        if (fileObj) {
            // Read out the File object as a Data URI:
            var fr = new FileReader();
            fr.onload = function() {
                // Load the library
                var libScript = document.createElement("script");
                libScript.textContent = fr.result;
                document.body.appendChild(libScript);

                // Load the callback function
                var mainScript = document.createElement("script");
                mainScript.textContent = "(" + callback.toString() + ")();";
                document.body.appendChild(mainScript);
            };
            fr.readAsText(fileObj);
        }
    }

    function main() {
        // Your script that uses the library goes here
    }

    addLibrary(path, main);
}, false);

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

Comments

  • photo

    geomorillo

    Wednesday, November 7, 2012

    TypeError: 'opera.extension.getFile' is not a function
  • photo

    Unrealmirakulix

    Wednesday, November 14, 2012

    doesn't work. Please help
No new comments accepted.