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

Change API to free Google Translate API #2

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 1 addition & 24 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@
<!-- # 設定セーブ&ロードボタン ################## -->
 <button onclick="saveSettings()">設定ファイルをPCに保存</button> 
<button onclick="loadSettings()">設定ファイルをブラウザに読込</button> :設定が完成したら,設定ファイルを保存しておきましょう <BR>
<HR>

<!-- # API key text box ##################### -->
翻訳用 Google API-KEY: <input type="text" name="gas_key" id="gas_key" size="60" oninput="updateOptionValues();" /> キーの作成方法 → <a target="_blank" href="http://www.sayonari.com/trans_asr/index_asr.html">[LINK]</a><BR>
翻訳回数:<div id="displayTransCount" style="display: inline-block; _display: inline;"></div><font color="gray">(上限 無料:5,000回,有料:20,000回)←僕じゃなくてGoogle にお金払う</font><BR>
<HR>

<!-- # 背景の色 ############################## -->
Expand Down Expand Up @@ -422,7 +417,7 @@
base_url += "main.html?";

// config 値 一覧 ////////////////
config_values = ["gas_key", "textAlign", "v_align", "whiteSpace", "recog", "trans", "trans2", "bgcolor"
config_values = ["textAlign", "v_align", "whiteSpace", "recog", "trans", "trans2", "bgcolor"
, "size1", "weight1", "color1", "st_color1", "st_width1"
, "size2", "weight2", "color2", "st_color2", "st_width2"
, "size3", "weight3", "color3", "st_color3", "st_width3"
Expand Down Expand Up @@ -521,7 +516,6 @@

if(bouyomi == true) {new_url = new_url + "&bouyomi=" + bouyomi;}
if(anti_sexual == true) {new_url = new_url + "&anti_sexual=" + "false";}
if(gas_key != '') {new_url = new_url + "&gas_key=" + gas_key;}

// config の更新
for (const p of config_values) {
Expand Down Expand Up @@ -651,23 +645,6 @@

</script>

<!-- # 翻訳回数取得 ############################ -->
<script>
function getTranslationCount() {
var iframe = document.getElementById('asr_frame');
var transCount = iframe.contentWindow.document.getElementById('translationCount').innerHTML;
if(transCount == ''){
transCount = '[ココに回数出るよ!]';
}
return transCount;
}

// 1秒ごとに翻訳カウントを更新
setInterval(function() {
document.getElementById('displayTransCount').innerHTML = getTranslationCount();
}, 1000);
</script>

<!-- # iframeの高さを切り替える ############################ -->
<script>
function toggleIframeHeight() {
Expand Down
34 changes: 11 additions & 23 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@
var trans_sourcelang = 'ja';
var trans_destlang = 'en';
var trans2_destlang = '';

var gas_key = getParam('gas_key');

var TRANS_URL = 'https://script.google.com/macros/s/' + gas_key + '/exec';
var query = ''

// その他の設定 -----------------------
Expand Down Expand Up @@ -320,13 +317,14 @@
bouyomiChanClient.talk(recog_text);
}

if(gas_key != null){
if(true){

// 翻訳1言語目
// console.log('arg_trans:'+arg_trans)
if(arg_trans != null){
query = TRANS_URL + '?text=' + recog_text + '&source=' + trans_sourcelang + '&target=' + trans_destlang;
request.open('GET', query, true);
query = 'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t';
request.open('POST', query, true);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

request.onreadystatechange = function(){
if (request.readyState === 4 && request.status === 200){
Expand All @@ -336,12 +334,7 @@
// JSON形式であるかを確認し、JSONであればtranslatedTextを抽出
try {
var jsonResponse = JSON.parse(responseText1);
if (jsonResponse.translatedText) {
responseText1 = jsonResponse.translatedText;
var translatedCount = jsonResponse.translatedCount;
document.getElementById('translationCount').innerHTML = translatedCount;
console.log('translatedCount(1):'+translatedCount);
}
responseText1 = jsonResponse[0][0][0];
} catch (e) {
// JSON形式でなければ何もしない
}
Expand All @@ -363,14 +356,15 @@
}
}
}
request.send(null);
request.send('q=' + encodeURIComponent(recog_text) + '&sl=' + trans_sourcelang + '&tl=' + trans_destlang);
}

// 翻訳2言語目
// console.log('arg_trans2:'+arg_trans2)
if(arg_trans2 != null){
query2 = TRANS_URL + '?text=' + recog_text + '&source=' + trans_sourcelang + '&target=' + trans2_destlang;
request2.open('GET', query2, true);
query2 = 'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t';
request2.open('POST', query2, true);
request2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

request2.onreadystatechange = function(){
if (request2.readyState === 4 && request2.status === 200){
Expand All @@ -380,13 +374,7 @@
// JSON形式であるかを確認し、JSONであればtranslatedTextを抽出
try {
var jsonResponse = JSON.parse(responseText2);
if (jsonResponse.translatedText) {
responseText2 = jsonResponse.translatedText;
document.getElementById('translationCount').innerHTML = jsonResponse.translatedCount;
var translatedCount2 = jsonResponse.translatedCount;
document.getElementById('translationCount').innerHTML = translatedCount2;
console.log('translatedCount(2):'+translatedCount2);
}
responseText2 = jsonResponse[0][0][0];
} catch (e) {
// JSON形式でなければ何もしない
}
Expand All @@ -405,7 +393,7 @@
}
}
}
request2.send(null);
request2.send('q=' + encodeURIComponent(recog_text) + '&sl=' + trans_sourcelang + '&tl=' + trans2_destlang);
}

} else {
Expand Down