![](/img/trans.png)
[英]I know what I want, but I don't know how to do it! (chrome extension)
[英]I do know how to solve the language/audio issue, but I don't know how to put it together as code
這是我的代碼:
$(document).ready(function() {
$(document).on("click", ".audioButton", function() {
var word = $(this).parent().find('.exerciseWord').html().toLowerCase() + '.mp3';
play(word);
});
getFileArray(); // load on page load
});
function getFileArray(word) {
$.getJSON("https://test.diglin.eu/api/media/fileList", {
lang: param
})
.done(r => {
audioArray = r.audio;
console.log("audio data loaded");
if (word) play(word);
});
}
function play(word) {
if (!audioArray) getFileArray(word);
else {
var foundID = audioArray.lowercase.indexOf(word);
console.log("foundID", foundID);
if (foundID > -1) {
var audio = new Audio();
audio.src = 'http://test.diglin.eu/' + audioArray.path + audioArray.files[foundID];
audio.play();
}
}
}
我想給param
的代碼:
$.getJSON('json_files/jsonData_' + ID + '.json', function(json) {
var jsonDataLanguage = json.main_object.language;
}
JSON(具有唯一ID)的樣子:
{
"main_object": {
"id": "new",
"getExerciseTitle": "TestToConfirm",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
"title": "TestToConfirm",
"language": "nl_NL",
"exercises": [
{
"word": "Hallo Marja.",
"syllables": [
"hallo",
"marja",
"",
""
]
}
]
},
"dataType": "json"
}
}
因此,下一件事應該發生(但嘗試時不起作用):
我嘗試在json文件中訪問所需的ID。 在我的JSON文件中,我還發送了一種語言,它應該獲取該語言並成為param
的值。 我嘗試這樣做,但會引發錯誤:“未定義json”。 很可能是因為我沒有訪問具有特定ID的JSON文件。 我該怎么辦? 我知道這是問題所在,但我不知道如何解決。
這是代碼,所有與錯誤無關的代碼都已刪除。 首先,分配按鈕單擊處理程序,該處理程序將使用單詞並嘗試播放相應的音頻。 如果尚未加載音頻數組,則調用getFileArray
,然后在完成的回調中播放音頻。
如果要執行更多操作,建議將done()
代碼移到單獨的函數中。
編輯:固定請求格式
EDIT2:添加了第二個請求
var audioArray;
var LANGUAGE, WORDS, audioArray;
$(document).ready(function() {
$(document).on("click", ".audioButton", function() {
var word = $(this).parent().find('.exerciseWord').html().toLowerCase() + '.mp3';
play(word);
});
getFileArray(); // load on page load
});
function getFileArray(word) {
$.getJSON('jsonLanguage/language.json').done(response => {
LANGUAGE = response.main_object.language;
WORDS = response.main_object.exerciseGetWordInput;
$.post("https://test.diglin.eu/api/media/fileList", {
language: LANGUAGE
})
.done(r => {
audioArray = r.audio;
console.log("audio data loaded");
if (word) play(word);
});
});
}
function play(word) {
if (!audioArray) getFileArray(word);
else {
var foundID = audioArray.lowercase.indexOf(word);
console.log("foundID", foundID);
if (foundID > -1) {
var audio = new Audio();
audio.src = 'http://test.diglin.eu/' + audioArray.path + audioArray.files[foundID];
audio.play();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.