Skip to content

Commit

Permalink
update to ver 0.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ververcpp committed Dec 2, 2014
1 parent 2cfc55f commit a8dde08
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 56 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ ChaZD 查字典,简洁易用的英汉字典扩展程序,支持划词哦:)

更新日志
-------
###ver 0.8.4
+ 优化了长文本的显示
+ 查词窗口更简洁
+ 同步了划词与弹出窗口的查询结果,想看更详细的翻译结果,划词后直接打开查词窗口就可以啦,还可以配合快捷键使用哦~
+ 使用shift键辅助,对之前无法划词的链接进行划词

###ver 0.8.3
+ 修改若干个BUG
+ 新的划词显示窗口
Expand Down
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TODO LIST
2. <del>在划词结果中显示音标(如果有的话)</del>
3. 在划词结果中支持发音
4. <del>改进划词结果显示的效果,整合jQuery</del>
5. 借鉴TransIt,针对链接的划词进行优化
5. <del>借鉴TransIt,针对链接的划词进行优化</del>
6. 准备引入英英翻译的功能,暂定为选用merriam-webster的API
7. ...
7. 设计一个新的插件图标
8. <del>修改popup页面的显示布局,使其更简洁</del>
35 changes: 28 additions & 7 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function ChaZD(queryWord, sendResponse) {
function ChaZD(queryWord, wordSource, sendResponse) {
this.wordSource = wordSource;
var url = urls.dict + queryWord;
console.log("Query url: " + url);
var queryResult = {};
Expand All @@ -17,7 +18,8 @@ function ChaZD(queryWord, sendResponse) {
ChaZD.prototype.checkErrorCode = function (errorCode) {
var response = {
"message": "",
"error": 0
"error": 0,
"errorCode": 0
};
switch (errorCode) {
case 0:
Expand All @@ -26,22 +28,27 @@ ChaZD.prototype.checkErrorCode = function (errorCode) {
case 20:
response["message"] = "要翻译的文本过长";
response["error"] = 1;
response["errorCode"] = 20;
break;
case 30:
response["message"] = "无法进行有效的翻译";
response["error"] = 1;
response["errorCode"] = 30;
break;
case 40:
response["message"] = "不支持的语言类型";
response["error"] = 1;
response["errorCode"] = 40;
break;
case 50:
response["message"] = "无效的key";
response["error"] = 1;
response["errorCode"] = 50;
break;
case 60:
response["message"] = "无辞典结果";
response["error"] = 1;
response["errorCode"] = 60;
break;
default:
}
Expand All @@ -68,8 +75,11 @@ ChaZD.prototype.parseResult = function (responseText) {
resultObj.haveWebTranslation = true;
resultObj.webBlock = webBlock;
}
} else {
resultObj.errorCode = validResult["errorCode"];
}
resultObj.validMessage = validResult["message"];

return resultObj;
}

Expand All @@ -82,15 +92,25 @@ ChaZD.prototype.initTitle = function (result) {
haveTranslation = false;

var voiceContainer = this.initVoice(queryWord);
console.log("word length:", queryWord.length);
console.log("word source:", this.wordSource);
queryWord = queryWord.length >= 50 && this.wordSource == "select" ? this.shortWord(queryWord) : queryWord;

console.log("word:", queryWord);
var titleWord = fmt(frames.titleWord, queryWord, voiceContainer);
var titleTranslation = fmt(frames.titleTranslation, translation.toString());


return {
titleBlock : fmt(frames.titleContainer, titleWord, titleTranslation),
titleBlock : fmt(frames.titleContainer, titleWord, titleTranslation, queryWord.length >=50 ? "long-text" : ""),
haveTranslation : haveTranslation
};
}

ChaZD.prototype.shortWord = function (longWord) {
return longWord.slice(0, longWord.lastIndexOf(" ", 50)).concat(" ...");
}

ChaZD.prototype.parseBasicResult = function (result) {
var basic = result["basic"];
var queryWord = result["query"];
Expand Down Expand Up @@ -253,8 +273,9 @@ chrome.runtime.onInstalled.addListener(
console.log("[ChaZD] update from version " + details.previousVersion);
//alert("New version has updated!");
showNotification({
title : "ChaZD 更新到0.8.3版啦!",
content : "又发现了一个bug,赶紧修改了一下,以及新的划词显示效果~ 新增划词结果发音功能!" +
title : "ChaZD 更新到0.8.4版啦!",
content : "优化了长文本的显示;简化了按钮窗口;同步了划词与弹出窗口的查询结果;使用shift键辅助,对之前无法划词的链接进行划词..." +
"更多更新内容点击查看更新日志~" +
"感谢大家的支持,下个正式版本会添加更多新的功能,敬请期待:)"
})
}
Expand All @@ -267,7 +288,7 @@ chrome.storage.sync.get(null,function (items) {
console.log("storage 是空的");
chrome.storage.sync.set(settings);
} else {
console.log("[ChaZD][Current Settings]")
console.log("[ChaZD][Current Settings]");
for (var key in items) {
if (settings[key] === undefined) {
chrome.storage.sync.remove(key);
Expand All @@ -284,7 +305,7 @@ 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);
new ChaZD(message.queryWord, message.source, sendResponse);

return true;
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,

"name": "ChaZD",
"version": "0.8.3",
"version": "0.8.4",
"description": "ChaZD 查字典,简洁易用的英汉字典扩展程序,支持划词哦:)",

"permissions": [
Expand Down
8 changes: 7 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>
<body>
<div class="input-container">
<input id="query-word" type="text" name="" placeholder="输入查找的单词或语句" value="" class="input-area">
<input id="query-word" type="text" name="" placeholder="" value="" class="input-area">
<button id="search" class="query-button"></button>
</div>

Expand All @@ -22,6 +22,12 @@
</div>

<div class="setting-block unshow" id="settings">
<div class="top-menu">
<input type="checkbox" name="linkQuery" id="linkQuery"><label for="linkQuery">使用Shift辅助链接划词</label>
<br>
<a href="#" title="如果想对某个链接上的单词进行划词,为避免无法取词的问题,可以按住shift键,此时链接会变成可取词的状态,这样就可以轻松划词查词啦:)" class="link-query-help">(鼠标悬停查看说明)</a>
</div>
<hr class="carved">
<div class="top-menu">
<input type="radio" name="selectMode" id="mouseSelect"><label for="mouseSelect">始终开启划词翻译</label>
</div>
Expand Down
107 changes: 70 additions & 37 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,66 @@ var $queryResultContainer = $("#query-result");
if (-1 !== window.navigator.platform.toLowerCase().indexOf("mac")) {
$("#ctrl-option").html("Command");
}
if (!$tipsContainer.hasClass("unshow")){
$tipsContainer.addClass("unshow");
}


function queryInPopup() {
if (!$tipsContainer.hasClass("unshow"))
$tipsContainer.addClass("unshow");
function queryInPopup(queryText) {
$input.select();
if ($queryResultContainer.hasClass("unshow"))
if ($queryResultContainer.hasClass("unshow")){
$queryResultContainer.removeClass("unshow");
}
$queryResultContainer.html("ψ(._. )>词典君正在翻译。。。");
console.log("input value: " + $input.val());
chrome.extension.sendMessage({queryWord: $input.val()}, function (response) {
//alert("response from xhr: " + JSON.stringify(response));
var resultObj = response;
var resultBlock = "";
if (resultObj.validMessage == "query success") {
resultBlock += resultObj.titleBlock;
if (resultObj.basicBlock !== undefined) {
resultBlock += resultObj.basicBlock;
}
if (resultObj.webBlock !== undefined) {
resultBlock += resultObj.webBlock;
}
$queryResultContainer.html(resultBlock);
var voiceCollection = $(".voice-container");
//console.log("voiceCollection length: " + voiceCollection.length);
voiceCollection.each(function(index, el) {
var src = $(this).attr("data-src");
//console.log(src);
var audioBlock = document.createElement("audio");
audioBlock.setAttribute("src", src);
//$.get();
audioBlock.addEventListener("ended", function (event) {
this.load();
})
$(this).click(function (event) {
audioBlock.play();
})
});
} else {
$queryResultContainer.html(resultObj.validMessage + "<br>词典君崩溃了(┬-┬)");
}
});
console.log("quertText: " + queryText);
if (queryText) {
$input.val(queryText);
chrome.extension.sendMessage({queryWord: queryText, source: "popup"}, buildResult);
}
else
chrome.extension.sendMessage({queryWord: $input.val(), source: "popup"}, buildResult);
}

var buildResult = function(response) {
//alert("response from xhr: " + JSON.stringify(response));
var resultObj = response;
var resultBlock = "";
if (resultObj.validMessage == "query success") {
resultBlock += resultObj.titleBlock;
if (resultObj.basicBlock !== undefined) {
resultBlock += resultObj.basicBlock;
}
if (resultObj.webBlock !== undefined) {
resultBlock += resultObj.webBlock;
}
$queryResultContainer.html(resultBlock);
var voiceCollection = $(".voice-container");
//console.log("voiceCollection length: " + voiceCollection.length);
voiceCollection.each(function(index, el) {
var src = $(this).attr("data-src");
//console.log(src);
var audioBlock = document.createElement("audio");
audioBlock.setAttribute("src", src);
//$.get();
audioBlock.addEventListener("ended", function (event) {
this.load();
})
$(this).click(function (event) {
audioBlock.play();
})
});
} else {
if (resultObj.errorCode == 20) {
$queryResultContainer.html("<p>这段文字太长,词典君无能为力了(┬_┬) <br><br>试试短一点的吧~</p>");
} else if (resultObj.errorCode == 40) {
$queryResultContainer.html("<p>对不起,这段文字太高深了,请饶过词典君吧(┬_┬)</p>");
} else {
$queryResultContainer.html("<p>词典君罢工啦(┬_┬)<br><br> 是不是网络不太好?<br><br> 稍后再试一次吧</p>");
}
}
};

$button.click(function (event) {
queryInPopup();
});
Expand Down Expand Up @@ -87,6 +104,7 @@ $("#setting-button").click(function (event) {
}
});

var linkQuery = $("#linkQuery");
var mouseSelect = $("#mouseSelect");
var useCtrl = $("#useCtrl");
var showPositionSide = $("#showPositionSide");
Expand All @@ -98,6 +116,14 @@ var tips = $("#tips");
var toggleKey = $("#toggle-key");

chrome.storage.sync.get(null, function (items) {
if(items.currentWord !== "") {
queryInPopup(items.currentWord);
}
if(items.linkQuery === true) {
linkQuery.attr("checked", true);
} else {
linkQuery.attr("checked", false);
}
if (items.selectMode === "mouseSelect") {
mouseSelect.attr("checked", true);
toggleKey.prop('disabled', 'disabled');
Expand All @@ -124,12 +150,19 @@ chrome.storage.sync.get(null, function (items) {
//currentDuration.innerHTML = showDuration.value = items["duration"];
});

linkQuery.click(function (event) {
var currentLinkQuery = $("#linkQuery:checked").length > 0;
chrome.storage.sync.set({"linkQuery": currentLinkQuery}, function() {
console.log("[ChaZD] Success update settings linkQuery = " + currentLinkQuery);
});
});

turnOffTips.click(function (event) {
tips.addClass("unshow");
chrome.storage.sync.set({"showTips": false}, function() {
console.log("[ChaZD] Success update settings showTips = false");
});
})
});

mouseSelect.click(function (event) {
toggleKey.prop('disabled', 'disabled');
Expand Down
Loading

0 comments on commit a8dde08

Please sign in to comment.