Created
May 23, 2015 11:11
-
-
Save takuya/95c1e7d4f8dfd926ef9d to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env node | |
var profile_dir = "/Users/takuya/Library/Application\ Support/Firefox/Profiles/agcb362v.default/searchplugins/" | |
var url = require('url') | |
var querystring = require('querystring') | |
var fs = require('fs'); | |
process.argv.parse = function(settings,object ){ | |
// console.log(process.argv) | |
//引数処理 | |
var flag_options = process.argv.filter(function(arg){return arg.match(/^-[^-]+/);}) | |
var kwrd_options = process.argv.filter(function(arg){return arg.match(/^--/);}) | |
flag_options.forEach(function(e){process.argv.splice(process.argv.indexOf(e), 1)}) | |
kwrd_options.forEach(function(e){process.argv.splice(process.argv.indexOf(e), 1)}) | |
//-h,--help | |
if(kwrd_options.join(",").match(/--help/) || flag_options.join("").match(/h/)){ | |
Object.keys(settings).forEach(function(e){ | |
console.log( "\t" + e + ":" +settings[e]["name"] + " " + settings[e]["desc"]) | |
}) | |
process.exit(1); | |
} | |
//default 値 | |
Object.keys(settings).forEach(function(e){ | |
object[settings[e]["name"]] = settings[e]["default"]; | |
}) | |
//整形 | |
Array.prototype.flatten = function(){ | |
return this.reduce(function(v,e){ if(e instanceof Array){ return e.flatten() } else{ return v.concat(e)} },[] ) | |
} | |
flag_options=flag_options.map(function(e){ | |
if(e.length!=2){ | |
return e.replace(/^-/,"").split("").map(function(e){return "-"+e;}) | |
}else{ | |
return e | |
} | |
}).flatten() | |
Array.prototype.flatten = undefined | |
//フラグ処理 | |
Object.keys(settings).forEach(function(e){ | |
if(flag_options.indexOf(e) > -1 ){ | |
object[settings[e]["name"]] = true; | |
} | |
}) | |
//値つき引数処理 | |
kwrd_options = kwrd_options.map(function(e){return e.split(/=/)}).reduce(function(v,e){v[e[0]]=e[1];return v },{}) | |
Object.keys(settings).filter(function(e){return e.match(/^--/);}).forEach(function(e){ | |
if(Object.keys(kwrd_options).indexOf(e) > -1 ){ | |
object[settings[e]["name"]] = kwrd_options[e] | |
} | |
}) | |
} | |
//以下の引数を探し、globalに設定を入れる。 | |
process.argv.parse( { | |
"-a" : { name:"save_to_firefox","default": false, desc:"Firefoxフォルダに保存"}, | |
"-o" : { name:"save_file_override","default": false, desc:"既存の上書き"}, | |
"--name" : { name:"output_name","default": false, desc:"出力設定名称"} | |
},global) | |
// console.log(process.argv) | |
// //console.log(global) | |
// process.exit(1); | |
// URLの処理 | |
u = process.argv[2]; | |
if(u == undefined){ | |
console.log(" Usage: " +process.argv[1] +" URL \n\n" + | |
"サンプル " +process.argv[1] +" http://example.jp/?query=%s " | |
) | |
process.exit(1); | |
} | |
console.log(u) | |
a = url.parse(u) | |
params = querystring.parse(a.query) | |
Object.keys(params).forEach(function(k,i){ | |
if( params[k] == '%s' ){ params[k] = "{searchTerms}";} | |
}) | |
f_name = global["output_name"] ? global["output_name"] : a.hostname | |
search = | |
[ | |
'<?xml version="1.0" encoding="UTF-8"?>', | |
'<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">', | |
' <ShortName>'+f_name+'</ShortName>', | |
' <Description>'+f_name+'</Description>', | |
' <InputEncoding>UTF-8</InputEncoding>', | |
' <Image width="16" height="16" type="image/x-icon">data:image/x-icon;base64,http:/'+a.hostname+'/favicon.ico</Image>', | |
' <Url ', | |
' type="text/html"', | |
' method="GET"', | |
' template="'+a.protocol+a.hostname+a.pathname.replace(/%s/, "{searchTerms}")+'">', | |
(function(){ | |
return Object.keys(params).map(function(k,i){ | |
v = params[k]; | |
return ' <Param name="'+k+'" value="'+v+'"/>'; | |
}).join("\n") | |
})(), | |
' </Url>', | |
'</OpenSearchDescription>' | |
].join("\n") | |
if(save_to_firefox){ | |
save_path = profile_dir + f_name +".xml" | |
opt = save_file_override ? "w" : "wx"; | |
fd = fs.openSync( save_path,opt) | |
fs.writeSync(fd,search) | |
console.log("save to " + save_path) | |
}else{ | |
console.log(search) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment