forked from ververcpp/ChaZD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,158 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
function ChaZD(queryWord, sendResponse) { | ||
var url = urls.dict + queryWord; | ||
console.log("Query url: " + url); | ||
var queryResult = {}; | ||
var self = this; | ||
var xhr = new XMLHttpRequest(); | ||
|
||
xhr.open("GET", url, true); | ||
xhr.onreadystatechange = function() { | ||
if (xhr.readyState != 4) return; | ||
var resultObj = self.parseResult.call(self, xhr.responseText); | ||
sendResponse(resultObj); | ||
} | ||
xhr.send(); | ||
} | ||
|
||
ChaZD.prototype.checkErrorCode = function (errorCode) { | ||
var response = { | ||
"message": "", | ||
"error": 0 | ||
}; | ||
switch (errorCode) { | ||
case 0: | ||
response["message"] = "query success"; | ||
break; | ||
case 20: | ||
response["message"] = "要翻译的文本过长"; | ||
response["error"] = 1; | ||
break; | ||
case 30: | ||
response["message"] = "无法进行有效的翻译"; | ||
response["error"] = 1; | ||
break; | ||
case 40: | ||
response["message"] = "不支持的语言类型"; | ||
response["error"] = 1; | ||
break; | ||
case 50: | ||
response["message"] = "无效的key"; | ||
response["error"] = 1; | ||
break; | ||
case 60: | ||
response["message"] = "无辞典结果"; | ||
response["error"] = 1; | ||
break; | ||
default: | ||
} | ||
return response; | ||
} | ||
|
||
ChaZD.prototype.parseResult = function (responseText) { | ||
var result = JSON.parse(responseText); | ||
var resultObj = {}; | ||
var validResult = this.checkErrorCode(result["errorCode"]); | ||
if (!validResult["error"]) { | ||
var titleBlock = this.initTitle(result); | ||
resultObj.titleBlock = titleBlock; | ||
if (result["basic"] !== undefined) { | ||
var basicBlock = this.parseBasicResult(result); | ||
resultObj.basicBlock = basicBlock | ||
} | ||
|
||
if (result["web"] !== undefined) { | ||
var webBlock = this.parseWebResult(result); | ||
resultObj.webBlock = webBlock; | ||
} | ||
} | ||
resultObj.validMessage = validResult["message"]; | ||
return resultObj; | ||
} | ||
|
||
ChaZD.prototype.initTitle = function (result) { | ||
var translation = result["translation"]; | ||
console.log(translation + " " + result["translation"] ); | ||
var queryWord = result["query"]; | ||
|
||
var voiceContainer = this.initVoice(queryWord); | ||
var titleWord = fmt(frames.titleWord, queryWord, voiceContainer); | ||
var titleTranslation = fmt(frames.titleTranslation, translation.toString()); | ||
|
||
return fmt(frames.titleContainer, titleWord, titleTranslation); | ||
} | ||
|
||
ChaZD.prototype.parseBasicResult = function (result) { | ||
var basic = result["basic"]; | ||
var queryWord = result["query"]; | ||
|
||
var phoneticBlock = this.parseBasicPhonetic(basic, queryWord); | ||
var explainsBlock = this.parseBasicExplains(basic, queryWord); | ||
|
||
var basicContainer = fmt(frames.basicContainer, phoneticBlock, explainsBlock); | ||
return basicContainer; | ||
} | ||
|
||
ChaZD.prototype.parseBasicPhonetic = function (basic, queryWord) { | ||
var ukPhonetic = basic["uk-phonetic"]; | ||
var usPhonetic = basic["us-phonetic"]; | ||
|
||
if (ukPhonetic !== undefined && usPhonetic !== undefined) { | ||
var ukVoice = this.initVoice(queryWord, 1); | ||
var ukPhoneticContainer = fmt(frames.ukPhoneticContainer, "[" + ukPhonetic + "]" + ukVoice); | ||
|
||
var usVoice = this.initVoice(queryWord, 2); | ||
var usPhoneticContainer = fmt(frames.usPhoneticContainer, "[" + usPhonetic + "]" + usVoice); | ||
|
||
return fmt(frames.phoneticContainer, ukPhoneticContainer, usPhoneticContainer); | ||
} | ||
|
||
return fmt(frames.phoneticContainer, "", ""); | ||
} | ||
|
||
ChaZD.prototype.initVoice = function (queryWord, type) { | ||
var src = urls.voice + queryWord; | ||
if(type !== undefined) | ||
src = src + "&type=" + type; | ||
var title = ""; | ||
if(type === 1){ | ||
title = "英音"; | ||
} else if (type === 2){ | ||
title = "美音"; | ||
} else { | ||
title = "真人发音"; | ||
} | ||
|
||
return fmt(frames.voiceContainer, src, title); | ||
} | ||
|
||
ChaZD.prototype.parseBasicExplains = function (basic, queryWord) { | ||
var explains = basic["explains"]; | ||
var i; | ||
var explainsContent = ""; | ||
for (i = 0; i < explains.length; i++) { | ||
var currentExplain = explains[i]; | ||
|
||
var haveProperty = currentExplain.indexOf(" "); | ||
var property = (haveProperty !== -1) ? currentExplain.slice(0, haveProperty) : ""; | ||
var propertyTitle = this.parseProperty(property); | ||
var propertyContainer = fmt(frames.propertyContainer, propertyTitle, property); | ||
var explainText = (haveProperty !== -1) ? currentExplain.slice(haveProperty) : currentExplain; | ||
|
||
var explain = fmt(frames.explain, propertyContainer, explainText); | ||
explainsContent += explain; | ||
} | ||
|
||
return fmt(frames.explainsContainer, fmt(frames.explainsList, explainsContent)); | ||
} | ||
|
||
ChaZD.prototype.parseProperty = function (property) { | ||
var propertyText = ""; | ||
switch (property) { | ||
case "adj." : | ||
propertyText = "形容词"; | ||
break; | ||
case "adv." : | ||
propertyText = "副词"; | ||
break; | ||
case "n." : | ||
propertyText = "名词"; | ||
break; | ||
case "vi." : | ||
propertyText = "不及物动词"; | ||
break; | ||
case "vt." : | ||
propertyText = "及物动词"; | ||
break; | ||
case "prep." : | ||
propertyText = "介词"; | ||
break; | ||
case "conj." : | ||
propertyText = "连词"; | ||
break; | ||
case "int." : | ||
propertyText = "感叹词"; | ||
break; | ||
case "abbr." : | ||
propertyText = "代词"; | ||
break; | ||
case "pron." : | ||
propertyText = ""; | ||
break; | ||
default : | ||
} | ||
|
||
return propertyText; | ||
} | ||
|
||
|
||
|
||
ChaZD.prototype.parseWebResult = function (result) { | ||
var web = result["web"]; | ||
var webExplainsContent = ""; | ||
var i; | ||
for (i = 0; i < web.length ; i++) { | ||
var webEplain = fmt(frames.webEplain, web[i].key + web[i].value); | ||
webExplainsContent += webEplain; | ||
} | ||
|
||
return fmt(frames.webExplainsContainer, fmt(frames.webEplainsList, webExplainsContent)); | ||
} | ||
|
||
/* | ||
ChaZD.prototype.parsePhrase = function (queryWord, key) { | ||
var words = []; | ||
words = queryWord.split(/\s+/); | ||
} | ||
*/ | ||
|
||
chrome.runtime.onMessage.addListener( | ||
function(message, sender, sendResponse){ | ||
console.log("message from sender:" + JSON.stringify(message)); | ||
console.log("sender is " + JSON.stringify(sender)); | ||
new ChaZD(message.queryWord, sendResponse); | ||
|
||
return true; | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"manifest_version": 2, | ||
|
||
"name": "ChaZD", | ||
"version": "0.6.0", | ||
"description": "ChaZD 查字典,简洁易用的英汉字典扩展程序,支持划词哦:)", | ||
|
||
"permissions": [ | ||
"http://dict.youdao.com/*", | ||
"http://fanyi.youdao.com/*", | ||
"contextMenus", | ||
"tabs", | ||
"storage" | ||
], | ||
|
||
"background": { | ||
"persistent": true, | ||
"scripts": [ "utility.js", "background.js" ] | ||
}, | ||
|
||
"content_scripts": [ { | ||
"all_frames": true, | ||
"css": ["style/inPage.css", "style/fonts/YDdict-Icon.css"], | ||
"js": ["utility.js", "selection.js"], | ||
"matches": ["<all_urls>"] | ||
|
||
} ], | ||
|
||
"browser_action": { | ||
"default_popup": "popup.html", | ||
"default_icon": { | ||
"38": "icons/icon38.png", | ||
"19": "icons/icon19.png" | ||
} | ||
}, | ||
|
||
"icons": { | ||
"16": "icons/icon16.png", | ||
"48": "icons/icon48.png", | ||
"128": "icons/icon128.png" | ||
}, | ||
|
||
"commands": { | ||
"_execute_browser_action": { | ||
"suggested_key": { | ||
"default": "Ctrl+Shift+F", | ||
"mac": "MacCtrl+Shift+F" | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Youdao Dictionary</title> | ||
<link rel="stylesheet" type="text/css" href="style/style.css"> | ||
</head> | ||
<body> | ||
<div class="input_container"> | ||
<button id="search" class="query_button"></button> | ||
<input id="query_word" type="text" name="" placeholder="输入查找的单词或语句" class="input_area"> | ||
</div> | ||
|
||
<div id="query_result" class="result_container unshow"></div> | ||
|
||
<div class="tips_container unshow" id="tips"> | ||
<p> | ||
Tips: 你可以通过“Ctrl+Shift+F”快速启动词典, | ||
也可以在<span id="key_set" class="link">这里</span>自定义快捷键哦:) | ||
</p> | ||
<div id="turn_off_tips">不再显示Tips</div> | ||
</div> | ||
|
||
<div class="setting_block unshow" id="settings"> | ||
<div> | ||
<input type="checkbox" name="mouseSelect" id="mouseSelect"><label for="mouseSelect">启用划词翻译</label> | ||
</div> | ||
<div> | ||
<input type="radio" name="showPosition" value="side" id="showPositionSide"><label for="showSide">翻译结果显示在浏览器边缘</label> | ||
</div> | ||
<div> | ||
<input type="radio" name="showPosition" value="near" id="showPositionNear"><label for="showNear">翻译结果显示在单词附近</label> | ||
</div> | ||
<div> | ||
<label for="showDuration"><b id="currentDuration"></b>秒后结果消失</label> | ||
<input type="range" name="showDuration" min="3" max="10" value="5" id="showDuration"> | ||
</div> | ||
</div> | ||
|
||
<footer> | ||
<div class="setting_button link" id="setting_button"></div> | ||
Powered By <img src="http://www.youdao.com/help/fanyiapi/brand/002.gif" alt="有道翻译"> | ||
<div class="contact_info"> | ||
<span id="email" class="link" title="如有问题可以发邮件"></span> | ||
<span id="issue" class="link" title="在Github上提出问题或建议"></span> | ||
<span id="source" class="link" title="源码在这里,欢迎Fork"></span> | ||
</div> | ||
</footer> | ||
<script src="utility.js"></script> | ||
<script src="popup.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.