popup.js 3.6 KB

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