popup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var bg,currentWord,wordObj;
  2. var wordInfoApi = 'https://api.shanbay.com/bdc/search/';
  3. var wordSentenceApi = 'https://api.shanbay.com/bdc/example/';
  4. var audio = new Audio();
  5. $(function(){
  6. $('.word a.audio').click(function(){
  7. audio.src = wordObj.info.audio;
  8. audio.play();
  9. });
  10. $('.info a.close').click(function(){
  11. chrome.app.window.current().close();
  12. });
  13. });
  14. chrome.runtime.onMessage.addListener(function(msg){
  15. if(msg=="updateInfo") build();
  16. })
  17. function build(){
  18. $('.loading').addClass('on');
  19. chrome.runtime.getBackgroundPage(function(background){
  20. bg = background;
  21. currentWord = bg.currentWord;
  22. wordObj = bg.wordDataObj[currentWord];
  23. initWordInfo();
  24. });
  25. }
  26. function initWordInfo(){
  27. if(wordObj['info']){
  28. initWordSentence();
  29. }else{
  30. $.ajax({
  31. method:'GET',
  32. url:wordInfoApi,
  33. data:{word:currentWord},
  34. dataType:'json',
  35. success:function(res){
  36. if(res.msg=="SUCCESS"){
  37. console.log(res.data)
  38. wordObj.info = {
  39. id:res.data.id,
  40. audio:res.data.audio,
  41. pron:res.data.pronunciation,
  42. definition:res.data.definition.split('\n')
  43. }
  44. chrome.storage.local.set({wordsObj:bg.wordDataObj});
  45. console.log(wordObj)
  46. initWordSentence();
  47. }else{
  48. console.log(res.msg);
  49. }
  50. },
  51. error:function(e){
  52. console.log('ERROR',e);
  53. // alert('接口调用失败')
  54. }
  55. });
  56. }
  57. }
  58. function initWordSentence(){
  59. if(wordObj['example']){
  60. fillContent();
  61. }else{
  62. $.ajax({
  63. method:'GET',
  64. url:wordSentenceApi,
  65. data:{vocabulary_id:wordObj.info.id,type:'sys'},
  66. dataType:'json',
  67. success:function(res){
  68. if(res.msg=="SUCCESS"){
  69. var samples = res.data;
  70. var exampleArr = [];
  71. console.log(samples)
  72. if(samples.length) for(var i = 0;i<samples.length;i++){
  73. var sentence = samples[i];
  74. console.log(sentence)
  75. exampleArr.push({
  76. sentence:sentence.annotation,
  77. trans:sentence.translation
  78. });
  79. if(i==4) break;
  80. };
  81. wordObj.example = exampleArr;
  82. chrome.storage.local.set({wordsObj:bg.wordDataObj});
  83. }else{
  84. console.log(res.msg);
  85. }
  86. fillContent();
  87. },
  88. error:function(e){
  89. console.log('ERROR',e);
  90. // alert('接口调用失败')
  91. }
  92. });
  93. }
  94. }
  95. function fillContent(){
  96. var newWord = currentWord.split('');
  97. newWord[0] = (newWord[0]).toUpperCase();
  98. newWord = newWord.join('');
  99. $('.word .w').text(newWord);
  100. $('.word .pron span').text(wordObj.info.pron);
  101. $('.def').empty();
  102. wordObj.info.definition.forEach(function(def){
  103. $('.def').append('<p>'+def+'</p>');
  104. });
  105. wordObj.example.forEach(function(ex){
  106. $('.example').append('<div class="ex"><p>'+ex.sentence+'</p><p>'+ex.trans+'</p></div>')
  107. })
  108. chrome.app.window.current().resizeTo(300,$('.info').height()+40);
  109. $('.loading').removeClass('on');
  110. }