装逼说明
不喜欢安装有道词典的应用程序,因为太耗资源还一堆广告,但是用浏览器打开dict.youdao.com
也挺麻烦的,不是吗?快来试试有道词典命令行工具吧!
看图装逼
自已装逼
安装
npm install yddict -g
使用说明
yd <要查询的单词>
在/usr/local/lib/node_modules/yddict/
目录下,可以进行一些自定义,或者更改查询的来源,
主要源码 index.js:
#!/usr/bin/env node
const request = require('request');
const cheerio = require('cheerio');
const chalk = require('chalk');
const fs = require('fs');
const Spinner = require('cli-spinner').Spinner;
const isChinese = require('is-chinese')
const urlencode = require('urlencode');
const spinner = new Spinner('努力查询中... %s');
const home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
const configFile = home + "/config.json";
let color = 'gray';//更改查询结果的文字颜色
spinner.setSpinnerString('|/-\\');
spinner.start();
const readFile = (filename, encoding) => {
try {
return fs.readFileSync(filename).toString(encoding);
}
catch (e) {
return null;
}
};
const config = JSON.parse(readFile(configFile,"utf8"));
const input = process.argv.slice(2)
const word = input.join(' ')
const isCn = isChinese(word);
const URL = isCn ? `http://dict.youdao.com/w/eng/${urlencode(word)}`:`http://dict.youdao.com/w/${urlencode(word)}`
const options = {
'url':URL
};
if(config){
if(config.proxy){
options.proxy = config.proxy;
}
if(config.color){
color = config.color;
}
}
const color_output = chalk.keyword(color);
request(options,(error, response, body)=>{
const $ = cheerio.load(body, {
ignoreWhitespace: true,
xmlMode: true
});
let result = '';
spinner.stop(true);
if(isCn){
$('div.trans-container > ul').find('p.wordGroup').each(function(i,elm){
result = $(this).text().replace(/\s+/g," ");
});
}else{
result = $('div#phrsListTab > div.trans-container > ul').text();
}
// phrase
if (result === '') {
result = $('div#webPhrase > p.wordGroup').text();
}
// sentence
if (result === '') {
result = $('div#fanyiToggle > div.trans-container > p:nth-child(2)').text();
}
console.log(color_output(result));
});
装逼指导
注:本文首发于 iHTCboy's blog,如若转载,请注明来源。