Skip to content

Instantly share code, notes, and snippets.

@teramako
Created October 13, 2011 13:58
Show Gist options
  • Save teramako/1284280 to your computer and use it in GitHub Desktop.
Save teramako/1284280 to your computer and use it in GitHub Desktop.
Vimperator-Plugin: XULを開けるようにするプロトコルを作成する

_pseudo-chrome-protocol.js

  • 単にvimp-pluginという名のプロトコルを作るプラグイン
  • $runtimePath/chrome/ がルートとなる
    • 例: vimp-plugin://test.xul => $runtimePath/chrome/test.xul
  • Chrome特権を有するため XUL ファイルを開くことが可能
    • 他のプラグインと組み合わせてGUIの作成ができる

サンプル

$runtimePath/chrome/test.xul を作成して vimp-plugin://test.xul を開いてみよう。

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/"?>
<window title="XUL TEST"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <vbox style="-moz-box-pack: center; -moz-box-align: center;" flex="1">
    <label value="TEST" style="font-size: 50pt"/>
  </vbox>
</window>
/**
* Vimperator Plugin
* Chrome特権を有するプロトコルをつくり、XUL等を開けるようにするプラグイン
* @license MPL 1.1/GPL 2.0/LGPL 2.1
*
* vimp-plugin://.... => $runtimePath/chrome/....
*
* window.openDialog("vimp-plugin://test.xul", "", "chrome,all");
*
*/
// ProtocolSheme
var scheme = "vimp-plugin";
var rootFile = (function() {
var dirs = io.getRuntimeDirectories("chrome");
var rootDir;
if (dirs.length === 0) {
rootDir = io.File(File.getPathsFromPathList(options.runtimepath)[0]);
rootDir.append("chrome");
rootDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
} else {
rootDir = dirs[0];
}
return rootDir;
})();
var UUID = "{ab87dede-be05-462e-981d-22712d89e9df}";
var VimpPluginProtocolHandler;
installProtocolHandler(rootFile);
function installProtocolHandler (aRootFile) {
var rootSpec = window.Services.io.newFileURI(aRootFile).spec;
VimpPluginProtocolHandler = {
scheme: scheme,
defaultPort: -1,
protocolFlags: Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD |
Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
Ci.nsIProtocolHandler.URI_NORELATIVE |
Ci.nsIProtocolHandler.URI_NOAUTH,
get system_principal() {
delete this.system_principal;
return this.system_principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager).
getSystemPrincipal();
},
newURI: function VPPH_newURI(aSpec, aOriginCharset, aBaseUri) {
let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
uri.spec = aSpec;
return uri;
},
newChannel: function VPPH_newChannel(aURI) {
let url = rootSpec + aURI.path.substr(2);
let ch = window.Services.io.newChannel(url, null, null);
ch.originalURI = aURI;
ch.owner = this.system_principal;
return ch;
},
allowPort: function VPPH_allowPort(aPort, aScheme) {
return false;
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
classID: Components.ID(UUID),
factory: {
createInstance: function PC_createInstance(aOuter, aIID) {
if (aOuter) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
return VimpPluginProtocolHandler.QueryInterface(aIID);
},
lockFactory: function VPPH_lockFactory(aLock) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIFactory])
},
};
try {
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(Components.ID(UUID),
"VimpPluginProtocolHandler",
"@mozilla.org/network/protocol;1?name=" + scheme,
VimpPluginProtocolHandler.factory);
} catch(e) {
Cu.reportError(e);
}
}
function uninstallProtocolHandler () {
if ("@mozilla.org/network/protocol;1?name=" + scheme in Cc) {
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.unregisterFactory(Components.ID(UUID),
VimpPluginProtocolHandler.factory);
}
}
function onUnload () {
uninstallProtocolHandler();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment