Created
August 27, 2012 13:42
-
-
Save Gozala/3488498 to your computer and use it in GitHub Desktop.
SDK API for registering new resource URIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint asi:true globalstrict:true*/ | |
'use strict'; | |
let { Cc, Ci } = require('chrome') | |
let ioService = Cc['@mozilla.org/network/io-service;1']. | |
getService(Ci.nsIIOService) | |
let resourceHandler = ioService.getProtocolHandler('resource'). | |
QueryInterface(Ci.nsIResProtocolHandler) | |
function get(root) { | |
/** | |
Gets the substitution for the `root` key. | |
**/ | |
try { return resourceHandler.getSubstitution(root).spec } | |
catch (error) { return null } | |
} | |
exports.get = get | |
function has(root) { | |
/** | |
Returns `true` if the substitution exists and `false` otherwise. | |
**/ | |
return resourceHandler.hasSubstitution(root) | |
} | |
exports.get = get | |
function set(root, uri) { | |
/** | |
Sets the substitution for the root key: | |
resource://root/path ==> baseURI.resolve(path) | |
A `null` `uri` removes substitution. A root key should | |
always be lowercase. However, this may not be enforced. | |
**/ | |
uri = !uri ? null : | |
uri instanceof Ci.nsIURI ? uri : | |
ioService.newURI(uri, null, null) | |
resourceHandler.setSubstitution(root, uri) | |
} | |
exports.set = set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: require('resource').set('projectname', data.url('index.html'));
then navigate to "resource://projectname" equals "resource:///index.html"