| 1 | var PLUGIN_INFO = |
|---|
| 2 | <VimperatorPlugin> |
|---|
| 3 | <name>AppLauncher</name> |
|---|
| 4 | <name lang='ja'>アプリケーションランチャー</name> |
|---|
| 5 | <description>Launch defined application</description> |
|---|
| 6 | <description lang='ja'>アプリケーションを起動します</description> |
|---|
| 7 | <version>0.12</version> |
|---|
| 8 | <author>pekepeke</author> |
|---|
| 9 | <minVersion>2.0pre</minVersion> |
|---|
| 10 | <maxVersion>2.0pre</maxVersion> |
|---|
| 11 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/applauncher.js</updateURL> |
|---|
| 12 | <detail lang='ja'><![CDATA[ |
|---|
| 13 | == Commands == |
|---|
| 14 | :applaunch [name]: |
|---|
| 15 | :runapp [name]: |
|---|
| 16 | [name] で指定されたアプリケーションを起動します。 |
|---|
| 17 | == .vimperatorrc example == |
|---|
| 18 | >|| |
|---|
| 19 | js <<EOM |
|---|
| 20 | liberator.globalVariables.applauncher_list = [ |
|---|
| 21 | [ 'name', 'application path', ['arguments', '%URL%', '%SEL%']], |
|---|
| 22 | [ 'Internet Explorer', 'C:\\Program Files\\Internet Explorer\\iexplore.exe', '%URL%'], |
|---|
| 23 | [ 'Internet Explorer(Search)', 'C:\\Program Files\\Internet Explorer\\iexplore.exe', '%SEL%'], |
|---|
| 24 | ]; |
|---|
| 25 | liberator.globalVariables.applauncher_charset = 'Shift_JIS'; |
|---|
| 26 | EOM |
|---|
| 27 | ||< |
|---|
| 28 | %URL% は実行時に選択中のリンクURL、もしくは開いているページのURLに置き換えられます。 |
|---|
| 29 | %SEL% は選択中の文字列に置き換えられます。 |
|---|
| 30 | %TITLE% はページのタイトルに置き換えられます。 |
|---|
| 31 | 引数を複数指定する場合は配列形式で指定してください。 |
|---|
| 32 | applauncher_charset を指定すると、渡される文字列が指定の文字セットに変換されます。 |
|---|
| 33 | ]]></detail> |
|---|
| 34 | </VimperatorPlugin> |
|---|
| 35 | |
|---|
| 36 | liberator.plugins.AppLauncher = (function(){ |
|---|
| 37 | const UConv = Cc['@mozilla.org/intl/scriptableunicodeconverter'].getService(Ci.nsIScriptableUnicodeConverter); |
|---|
| 38 | const AppName = 'AppLauncher'; |
|---|
| 39 | |
|---|
| 40 | var global = liberator.globalVariables; |
|---|
| 41 | var settings = global.applauncher_list || []; |
|---|
| 42 | var defaultCharset = global.applauncher_charset; |
|---|
| 43 | if (!settings || settings.length <= 0) return; |
|---|
| 44 | var completer = settings.map( function([name, app, args]) [name, args ? app + ' ' + args.toString(): app] ); |
|---|
| 45 | |
|---|
| 46 | var Class = function() function(){ this.initialize.apply(this, arguments); }; |
|---|
| 47 | var AppLauncher = new Class(); |
|---|
| 48 | |
|---|
| 49 | AppLauncher.prototype = { |
|---|
| 50 | initialize: function(){ |
|---|
| 51 | this.buildMenu(); |
|---|
| 52 | this.registerCommand(); |
|---|
| 53 | }, |
|---|
| 54 | registerCommand: function(){ |
|---|
| 55 | var self = this; |
|---|
| 56 | commands.addUserCommand(['applaunch', 'runapp'], 'Run Defined Application', |
|---|
| 57 | function(arg){ |
|---|
| 58 | arg = (typeof arg.string == 'undefined' ? arg : arg.string); |
|---|
| 59 | self.launch(arg); |
|---|
| 60 | }, { |
|---|
| 61 | completer: function(context, arg){ |
|---|
| 62 | var filter = context.filter; |
|---|
| 63 | context.title = [ 'Name', 'Description']; |
|---|
| 64 | if (!filter){ |
|---|
| 65 | context.completions = completer; |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | filter = filter.toLowerCase(); |
|---|
| 69 | context.completions = completer.filter( function(el) el[0].toLowerCase().indexOf(filter) == 0); |
|---|
| 70 | } |
|---|
| 71 | }); |
|---|
| 72 | }, |
|---|
| 73 | buildMenu: function(){ |
|---|
| 74 | var self = this; |
|---|
| 75 | var menu = document.getElementById('contentAreaContextMenu') |
|---|
| 76 | .appendChild(document.createElement('menu')); |
|---|
| 77 | menu.setAttribute('id', AppName + 'Context'); |
|---|
| 78 | menu.setAttribute('label', AppName); |
|---|
| 79 | menu.setAttribute('accesskey', 'L'); |
|---|
| 80 | |
|---|
| 81 | var menupopup = menu.appendChild(document.createElement('menupopup')); |
|---|
| 82 | menupopup.setAttribute('id', AppName + 'ContextMenu'); |
|---|
| 83 | for (let i=0, l=settings.length; i<l; i++){ |
|---|
| 84 | let [name, app, args] = settings[i]; |
|---|
| 85 | let menuitem = menupopup.appendChild(document.createElement('menuitem')); |
|---|
| 86 | menuitem.setAttribute('id', AppName + i); |
|---|
| 87 | menuitem.setAttribute('label', name + '\u3092\u8D77\u52D5'); |
|---|
| 88 | menuitem.addEventListener('command', function() self.launch(name), false); |
|---|
| 89 | } |
|---|
| 90 | }, |
|---|
| 91 | variables: { |
|---|
| 92 | __noSuchMethod__: function(name) name, |
|---|
| 93 | URL: function() gContextMenu && gContextMenu.onLink ? gContextMenu.getLinkURL() : buffer.URL, |
|---|
| 94 | SEL: function(){ |
|---|
| 95 | var selection = window.content.window.getSelection(); |
|---|
| 96 | var sel = ''; |
|---|
| 97 | for (let i=0, l=selection.rangeCount; i<l; i++) sel+=selection.getRangeAt(i).toString(); |
|---|
| 98 | return sel; |
|---|
| 99 | }, |
|---|
| 100 | TITLE: function() buffer.title |
|---|
| 101 | }, |
|---|
| 102 | launch: function(appName){ |
|---|
| 103 | var self = this; |
|---|
| 104 | appName = appName.replace(/\\+/g, ''); // fix commandline input ' ' -> '\ ' |
|---|
| 105 | settings.some( function([name, app, args]){ |
|---|
| 106 | args = args instanceof Array ? args : args ? [args] : []; |
|---|
| 107 | args = args.map( function( val ) val.replace(/%([A-Z]+)%/g, function( _, name ) self.variables[name]()) ); |
|---|
| 108 | if (defaultCharset){ |
|---|
| 109 | UConv.charset = defaultCharset; |
|---|
| 110 | args = args.map( function( val ) UConv.ConvertFromUnicode(val) ); |
|---|
| 111 | } |
|---|
| 112 | if (appName == name){ |
|---|
| 113 | io.run(app, args); |
|---|
| 114 | return true; |
|---|
| 115 | } |
|---|
| 116 | return false; |
|---|
| 117 | }); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | return new AppLauncher(); |
|---|
| 121 | })(); |
|---|