Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<div class="sub-menu">
<input type="radio" name="showPosition" value="near" id="showPositionNear"><label for="showPositionNear">翻译结果显示在单词附近</label>
</div>
<div>
<input type="text" placeholder="key" id="userkey">
<input type="text" placeholder="keyfrom" id="userkeyfrom">
</div>
<div class="sub-menu">
<input type="checkbox" name="autoHide" id="autoHide"><label for="showDuration"><b id="currentDuration"></b>秒后隐藏结果</label>
<input type="range" name="showDuration" min="3" max="8" id="showDuration">
Expand Down
22 changes: 19 additions & 3 deletions src/javascript/background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
function ChaZD(queryWord, useHttps, wordSource, sendResponse) {
this.wordSource = wordSource;
this.useHttps = useHttps;
var url = (useHttps ? urls.dictHttps : urls.dict) + queryWord;
//console.log("Query url: " + url);
var basicurl = getbasicurl(useHttps);
console.log(basicurl);
var url = basicurl + queryWord;
console.log("Query url: " + url);
var queryResult = {};
var self = this;
var xhr = new XMLHttpRequest();
Expand All @@ -22,7 +24,21 @@ function ChaZD(queryWord, useHttps, wordSource, sendResponse) {
};
xhr.send();
}

function getbasicurl(useHttps) {
var thisurl = (useHttps ? urls.dictHttps : urls.dict);
var chazduserkey = localStorage.getItem("chazduserkey");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用chrome.storage.sync.get()吧,能在不同的设备间同步

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉没啥必要,而且用chrome.sync是异步调用,现在写的这个逻辑不能用

var chazduserkeyfrom = localStorage.getItem("chazduserkeyfrom");
console.log(chazduserkey,chazduserkeyfrom);
if(chazduserkey && chazduserkey !== "" && chazduserkeyfrom && chazduserkeyfrom !== "") {
if (useHttps) {
thisurl = fmt(templateUrls.dictHttps, chazduserkey, chazduserkeyfrom);
} else {
thisurl = fmt(templateUrls.dict, chazduserkey, chazduserkeyfrom);
}
}

return thisurl;
}
ChaZD.prototype.checkErrorCode = function (errorCode) {
var response = {
"message": "",
Expand Down
24 changes: 22 additions & 2 deletions src/javascript/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ var tips = document.querySelector("#tips");
var toggleKey = document.querySelector("#toggle-key");
var useHttps = document.querySelector("#useHttps");
var useHttpsValue = false;
var userkey = document.querySelector("#userkey");
var userkeyfrom = document.querySelector("#userkeyfrom");

chrome.storage.sync.get(null, function (items) {
if(items.currentWord !== "") {
Expand Down Expand Up @@ -240,6 +242,12 @@ chrome.storage.sync.get(null, function (items) {
autoHide.nextSibling.classList.add("unactive");
showDuration.disabled = true;
}
if (localStorage.getItem("userkey")) {
userkey.value = localStorage.getItem("userkey");
}
if (localStorage.getItem("userkeyfrom")) {
userkeyfrom.value = localStorage.getItem("userkeyfrom");
}
currentDuration.innerHTML = showDuration.value = items.showDuration;
});

Expand Down Expand Up @@ -379,8 +387,20 @@ toggleKey.onchange = function (event) {
// })

//在popup页内 Enter键 查询选中部分
document.addEventListener('keyup',function(e){
document.addEventListener("keyup",function(e){
if(document.activeElement.tagName=="BODY" && e.which==13){
queryInPopup(window.getSelection().toString());
}
});
});
userkey.addEventListener("change", function(event){
localStorage.setItem("chazduserkey",this.value);
chrome.storage.local.set({"userkey" : this.value}, function() {
console.log("[ChaZD] Success update settings userkey = " + this.value);
});
});
userkeyfrom.addEventListener("change", function(event){
localStorage.setItem("chazduserkeyfrom",this.value);
chrome.storage.local.set({"userkeyfrom" : this.value}, function() {
console.log("[ChaZD] Success update settings userkeyfrom = " + this.value);
});
});
14 changes: 11 additions & 3 deletions src/javascript/utility.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var api = {
key: 1116151381,
keyfrom : "youdaocidian"
key: 2078867566,
keyfrom : "lytofbblogger"
};

var urls = {
Expand All @@ -10,6 +10,12 @@ var urls = {
voiceHttps : "https://dict.youdao.com/dictvoice?audio=",
};

var templateUrls = {
dict : "http://fanyi.youdao.com/openapi.do?keyfrom=#{2}&key=#{1}&type=data&doctype=json&version=1.1&q=",
voice : "http://dict.youdao.com/dictvoice?audio=",
dictHttps : "https://fanyi.youdao.com/openapi.do?keyfrom=#{2}&key=#{1}&type=data&doctype=json&version=1.1&q=",
voiceHttps : "https://dict.youdao.com/dictvoice?audio=",
};
var settings = {
selectMode : "mouseSelect", //划词的形式:直接划词 or Ctrl + 划词
showPosition : "near", //划词翻译结果显示的位置
Expand All @@ -22,6 +28,8 @@ var settings = {
showDuration: 3, //翻译结果显示持续时间
defaultVoice: 1, //划词默认发音:1--英音;2--美音
useHttps: false, //是否使用 HTTPS 的接口
userkey:undefined, //用户自定义key
userkeyfrom:undefined //用户自定义keyfrom
};

var frame = {
Expand Down Expand Up @@ -79,4 +87,4 @@ function fmt() {

function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
}