#title=Operaで検索 #tooltip=Operaに登録されている検索エンジンをポップアップメニューで選び、そのエンジンで選択文字列をインターネット検索します fso = new ActiveXObject("Scripting.FileSystemObject"); wshell = new ActiveXObject("WScript.Shell"); function ShowMenu(menuDef) { function Array2Menu(a, commands) { var menu = CreatePopupMenu(); for (var i = 0 ; i < a.length ; ++i) { if (a[i] == null) continue; if (a[i].submenu instanceof Array) { var submenu = Array2Menu(a[i].submenu, commands); menu.AddPopup(a[i].label, submenu); } else { var index = commands.length; if (index == 0) { index = 1; } if (a[i].label == "----") { menu.Add("", 0, eeMenuSeparator); } else { var flags = (a[i].checked ? eeMenuChecked : 0) | (a[i].grayed ? eeMenuGrayed : 0); menu.Add(a[i].label, index, flags); commands[index] = a[i]; } } } return menu; } var commands = []; var menu = Array2Menu(menuDef, commands); var commandIndex = menu.Track(/*eePosMouse*/); if (commandIndex > 0) { commands[commandIndex].command(); return true; } return false; } function EscapeEncodeURI(src, enc) { // 以下のBinaryクラスは // http://seantw.spaces.live.com/blog/cns!4D90655A95AC394E!121.entry // の Sean さんの情報によります。 // ここからBinaryクラス-> var scVB = new ActiveXObject("ScriptControl"); scVB.Language = "VBScript"; scVB.AddCode("Function vbBinary_getSize(text) : vbBinary_getSize = LenB(text) : End Function"); scVB.AddCode("Function vbBinary_At(text, index) : vbBinary_At = AscB(MidB(text, index + 1, 1)) : End Function"); // Binary クラスで VBScript を隠蔽 // Binary クラスには size プロパティと At() および charAt() メソッドを実装 function Binary(data) { this.data = data; this.size = scVB.Run("vbBinary_getSize", this.data); } function Binary_At(index) { if (index < 0 || index >= this.size) return 0; return scVB.Run("vbBinary_At", this.data, index); } function Binary_charAt(index) { if (index < 0 || index >= this.size) return ""; return String.fromCharCode(scVB.Run("vbBinary_At", this.data, index)); } Binary.prototype.At = Binary_At; Binary.prototype.charAt = Binary_charAt; // <- ここまでBinaryクラス var stream = new ActiveXObject("ADODB.Stream"); stream.Type = 2;//Text stream.Charset = enc; stream.Open(); stream.WriteText(src); stream.Position = 0;//BOM対策 stream.Type = 1;//Binary stream.Position = (enc.toLowerCase().indexOf("utf") == 0) ? 3 : 0;//BOM対策 //stream.Open(); var bin = new Binary(stream.Read()); stream.Close(); var result = ""; for (var i = 0 ; i < bin.size ; ++i) { var c = bin.At(i); if (c < 16) { result += "%0" + c.toString(16); } else { result += "%" + c.toString(16); } } return result; } function readTextFile(path) { var lines = new Array(); var stream = new ActiveXObject("ADODB.Stream"); stream.type = 2;//2 = テキストモード stream.charset = "UTF-8"; stream.open(); stream.loadFromFile(path); while (!stream.EOS) { lines.push(stream.readText(-2)); } stream.close(); return lines; } function readIni(lines) { var result = {}; var curProperties = new Object(); var curSection = ""; for (var i = 0 ; i < lines.length ; ++i) { var l = lines[i]; var comment = l.indexOf(";"); if (comment >= 0) l = l.substr(0, comment); if (l.match(/^([^=]+)=(.*)$/)) { curProperties[RegExp.$1] = RegExp.$2; } else if ((i == lines.length - 1) || l.match(/^\[([^\]]+)\]/)) { result[curSection] = curProperties; curSection = RegExp.$1; curProperties = {}; } } return result; } function getHostname(url) { if (url.match(/^https?:\/\/([^\/]+)/)) { return RegExp.$1; } return ""; } function genSearchMenuDef(exe, ini, word, result) { if (result == null) { result = []; } for (var section in ini) { if (section.match(/Search Engine (\d+)/)) { var index = parseInt(RegExp.$1); var props = ini[section]; if (props["Key"] == "") continue; if (props.URL == "") continue; //if (props["key"] == undefined) continue; var label = props.Name; if (label == "") { label = getHostname(props.URL); } if (label.indexOf("&") < 0) { label = "&" + props.Key + " " + label; } var url = props["URL"].replace(/%s/, EscapeEncodeURI(word, props["Encoding"])); if (url == "") continue; var item = { shell:('"' + exe + '"' + " " + url), label : label, command : function () { wshell.Run(this.shell); } }; result.push(item); } } return result; } function askWord() { var word = document.selection.Text; if (word == "") { document.selection.SelectWord(); word = document.selection.Text; } if (word == "") { word = prompt("検索キーワードを入力して下さい", ""); } return word; } function checkPath(path) { if (!fso.FileExists(path)) { alert(path + " が見つかりませんでした。"); Quit(); } } var wsenv = wshell.Environment("Process"); var operaSearchIniPath = wsenv("ProgramFiles")+"\\Opera\\search.ini"; var operaPath = wsenv("ProgramFiles")+"\\Opera\\Opera.exe"; var userSearchIniPath = wshell.SpecialFolders("Appdata")+"\\Opera\\Opera\\profile\\search.ini"; checkPath(operaSearchIniPath); checkPath(userSearchIniPath); var word = askWord(); var operaSearchIni = readIni(readTextFile(operaSearchIniPath)); var userSearchIni = readIni(readTextFile(userSearchIniPath)); var menuDef = genSearchMenuDef(operaPath, operaSearchIni, word, genSearchMenuDef(operaPath, userSearchIni, word)); ShowMenu(menuDef);