f8g

WSHでFast Look up Alc

http://userscripts.org/scripts/show/12024
HTMLパーサを使おうと思ったけど、やり方がわからなかったので諦めました。保存するようなのにしたい(いつか)。

/*************************
 * tsu ka i ka ta
 * > cscript alc.js word
 *************************/

if (!WSH.Arguments.length)
	WSH.Quit();

var word = WSH.Arguments(0);

if (!word || !word.match(/^[a-z\s]+$/i) || word.match(/^\s+$/))
	WSH.Quit();

XmlhttpRequest({
	method: 'get',
	url: "http://eow.alc.co.jp/" + word + "/",
	onload : function (req) {
		var text = req.responseText;
		var indx = '<span class="midashi">';
		var start = text.indexOf(indx);
		var end = text.indexOf(indx, start + indx.length);
		var mean = text.substring(start, end).replace(/(<[^<a]+>)+/g, "\n").replace(/(<[^<]+>|\uFF5B[^\uFF5B]+\uFF5D)+/g, "");
		log(mean);
	}
});

function XmlhttpRequest(params){
	var xmlhttp = WSH.CreateObject("MSXML2.XMLHTTP");

	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4)
			params.onload(xmlhttp);
	};

	xmlhttp.open(params.method, params.url, false);
	xmlhttp.send(null);
}

function log () {
	WSH.Echo(Array.prototype.slice.call(arguments, 0));
}

追記: ファイルをがんがん作って保存するやつ。

var dirName = "dic";

if (!WSH.Arguments.length)
	WSH.Quit();

var word = WSH.Arguments(0);

if (!word || !word.match(/^[a-z\s]+$/i) || word.match(/^\s+$/))
	WSH.Quit();

var fso = WSH.CreateObject("Scripting.FileSystemObject");

getMean(word);

function XmlhttpRequest(params){
	var xmlhttp = WSH.CreateObject("MSXML2.XMLHTTP");

	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4)
			params.onload(xmlhttp);
	};

	xmlhttp.open(params.method, params.url, false);
	xmlhttp.send(null);
}

function log () {
	WSH.Echo(Array.prototype.slice.call(arguments, 0));
}

function getMean(word){
	var filePath = dirName + "\\" + word;
	if(!fso.FolderExists(dirName))
		fso.CreateFolder(dirName);

	if(!fso.FileExists(filePath)){
		XmlhttpRequest({
			method: 'get',
			url: "http://eow.alc.co.jp/" + word + "/",
			onload : function (req) {
				var text = req.responseText;
				var indx = '<span class="midashi">';
				var start = text.indexOf(indx);
				var end = text.indexOf(indx, start + indx.length);
				var mean = text.substring(start, end).replace(/(<[^<a]+>)+/g, "\n").replace(/(<[^<]+>|\uFF5B[^\uFF5B]+\uFF5D)+/g, "").replace(/(\n\n)+/g, "\n");

				createFile(word, mean);
				log(mean);
			}
		});

		return;
	}

	log(fso.OpenTextFile(filePath).ReadAll());
}

function createFile(path, mean){
	var file = fso.CreateTextFile(dirName + "\\" + word);
	file.Write(mean);
	file.Close();
}