Skip to content

Instantly share code, notes, and snippets.

@MSakamaki
Last active August 29, 2015 14:10
Show Gist options
  • Save MSakamaki/4c96b4160c93234970f8 to your computer and use it in GitHub Desktop.
Save MSakamaki/4c96b4160c93234970f8 to your computer and use it in GitHub Desktop.
Firefox add-on Memo
var myExtension = function(){
const { Cu, Cc, Ci } = require("chrome");
let file = Cc["@mozilla.org/file/local;1"].createInstance(getLocalFileApi());
file.initWithPath("C:\\windows\\notepad.exe");
file.launch();
// 引数が必要なケース
//var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
//process.init(file);
//var args = ['-a', 'safari'];
//process.run(false, args, args.length);
function getLocalFileApi() {
if ("nsILocalFile" in Ci) {
return Ci.nsILocalFile;
}
else
return Ci.nsIFile;
}
}
var pageModScripts = function(){
window.addEventListener('openexec', function(event) {
self.port.emit('loaded');
});
};
var pageMod = require('sdk/page-mod').PageMod({
include: ['*'], // domain指定で動作するタブを縛れる 例:http://localhost:8000/
contentScript: pageModScripts.toSource() + "()",
onAttach: function(worker) {
worker.port.on('loaded', function() {
myExtension();
});
}
});
// テスト用
var tabs = require("sdk/tabs");
tabs.open("http://localhost:8000/");
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick="memoopen();">サファリ起動(MAC限定)</button>
<script>
var memoopen ;
window.onload = function()
{
console.log('start');
memoopen = function(){
var evt = document.createEvent("Events");
evt.initEvent("openexec", true, false);
document.dispatchEvent(evt);
}
};
</script>
</body>
</html>
var myExtension = function(){
const { Cu, Cc, Ci } = require("chrome");
let file = Cc["@mozilla.org/file/local;1"].createInstance(getLocalFileApi());
file.initWithPath("C:\\windows\\notepad.exe");
file.launch();
// 引数が必要なケース
//var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
//process.init(file);
//var args = ['-a', 'safari'];
//process.run(false, args, args.length);
function getLocalFileApi() {
if ("nsILocalFile" in Ci) {
return Ci.nsILocalFile;
}
else
return Ci.nsIFile;
}
}
var pageModScripts = function(){
window.addEventListener('openexec', function(event) {
self.port.emit('loaded');
});
};
var pageMod = require('sdk/page-mod').PageMod({
include: ['*'], // domain指定で動作するタブを縛れる 例:http://localhost:8000/
contentScript: pageModScripts.toSource() + "()",
onAttach: function(worker) {
worker.port.on('loaded', function() {
myExtension();
});
}
});
// MSゴシックでなかったらMSゴシックにする処理
var pageMod = require("sdk/page-mod").PageMod({
include: "*.org",
onAttach: function(worker) {
const { Cu, Cc, Ci } = require("chrome");
var _prefService =
Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch)
var pref = _prefService.getCharPref("font.name.sans-serif.ja");
//console.log('pref:',pref)
if (pref !== 'MS Gothic'){
_prefService.setCharPref("font.name.sans-serif.ja", "MS Gothic");
}
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick="memoopen();">引数渡して叩く</button>
<script>
var memoopen ;
window.onload = function()
{
console.log('start');
memoopen = function(){
// 引数を使う
var event = new (window.CustomEvent)('openexec', {
"detail":{
"params": [0,1,2,3,4,5,6,7,8,9]
}
});
window.dispatchEvent(event);
}
};
</script>
</body>
</html>
var myExtension = function(params){
const { Cu, Cc, Ci } = require("chrome");
let file = Cc["@mozilla.org/file/local;1"].createInstance(getLocalFileApi());
//file.initWithPath("C:\\windows\\notepad.exe");
file.initWithPath("/bin/sh");
//file.launch();
// 引数が必要なケース
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
var args = params;
args.unshift("動かすShellファイルの引数");
process.run(false, args, args.length);
function getLocalFileApi() {
if ("nsILocalFile" in Ci) {
return Ci.nsILocalFile;
}
else
return Ci.nsIFile;
}
}
var pageModScripts = function(){
window.addEventListener('openexec', function(event) {
self.port.emit('loaded',event.detail.params);
});
};
var pageMod = require('sdk/page-mod').PageMod({
include: ['*'], // domain指定で動作するタブを縛れる 例:http://localhost:8000/
contentScript: pageModScripts.toSource() + "()",
onAttach: function(worker) {
worker.port.on('loaded', function(params) {
myExtension(params);
});
}
});
const { Cu, Cc, Ci } = require("chrome");
let file = Cc["@mozilla.org/file/local;1"].createInstance(getLocalFileApi());
// 実行するファイル
file.initWithPath("/Applications/Safari.app/Contents/MacOS/Safari");
// 実行
file.launch();
// temporary solution for removal of nsILocalFile
function getLocalFileApi() {
if ("nsILocalFile" in Ci) {
return Ci.nsILocalFile;
}
else
return Ci.nsIFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment