Developers › Dev.Opera

opera.extension.getFile()

May 16, 2012 · by Opera Software

This functionality is currently in production, and only available to test in an experimental build of Opera desktop. To download the experimental build, see Desktop build with new extension APIs: Screenshot, Resource Loader and URL Filter.

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

About this article

Published inDev.Opera
DateMay 16, 2012
AuthorOpera Software
Tagsextensions API, file api, Resource Loader
OriginalInternet Archive