Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加了一个自定义apikey的功能 #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 18 additions & 2 deletions src/javascript/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
function ChaZD(queryWord, useHttps, wordSource, sendResponse) {
this.wordSource = wordSource;
this.useHttps = useHttps;
var url = (useHttps ? urls.dictHttps : urls.dict) + queryWord;
var basicurl = getbasicurl(useHttps);
//console.log(basicurl);
var url = basicurl + queryWord;
//console.log("Query url: " + url);
var queryResult = {};
var self = this;
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

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
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
26 changes: 23 additions & 3 deletions src/javascript/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function totalHeight(className) {
return sum + 10;
}

var blockHeight = totalHeight("top-menu") + totalHeight("sub-menu") + totalHeight("carved") + 32;
var blockHeight = totalHeight("top-menu") + totalHeight("sub-menu") + totalHeight("carved") + 32 + 56;
var linkQuery = document.querySelector("#linkQuery");
var noSelect = document.querySelector("#noSelect");
var mouseSelect = document.querySelector("#mouseSelect");
Expand All @@ -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);
});
});
10 changes: 9 additions & 1 deletion src/javascript/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
}
}