« Documentation Home

opera.extension.getFile()

This documentation relates to Opera's now deprecated .oex Extension API framework for Opera versions <= 12.15 and also provided by our OEX2NEX shim library.

For the latest Opera Extensions API documentation for Opera versions > 12.15 please consult the latest Opera Extensions API documentation online.

Description:

Gets a file within an extension package.

Parameters:

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);