main.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2019-04-21)
  4. * @License MIT
  5. */
  6. var bg,delWord;
  7. $(function(){
  8. $('.loading').addClass('on').find('p').text('词库读取中,请稍后...');
  9. chrome.runtime.getBackgroundPage(function(background){
  10. bg = background;
  11. buildStarList();
  12. $('.showTimeSets input').val(bg.setting.timerMin);
  13. });
  14. chrome.runtime.onMessage.addListener(function(msg){
  15. if(msg=="updateList") buildNormalList();
  16. if(msg=="updateStarList") buildStarList();
  17. })
  18. chrome.notifications.onButtonClicked.addListener(function(e,index){
  19. if(e=='deleteword'){
  20. if(index==0) bg.deleteWord(delWord);
  21. delWord = null;
  22. }
  23. if(e=='clearData' && index==0) bg.clearDatas();
  24. })
  25. $('.tabContent .list').on('click','li a.info',function(){
  26. var word = $(this).parents('li').data('word');
  27. bg.showWordInfo(word);
  28. });
  29. $('.tabContent .list').on('click','li a.star',function(){
  30. var word = $(this).parents('li').data('word');
  31. var nowType = $(this).data('star');
  32. if(nowType=='undefined') nowType = 0;
  33. var toType = Math.abs(nowType-1);
  34. bg.starWord(toType,word);
  35. $(this).data('star',toType);
  36. $(this).html('<i class="fa fa-star'+(toType==1?'':'-o')+'"></i>');
  37. });
  38. $('.tabContent .list').on('click','li a.del',function(){
  39. delWord = $(this).parents('li').data('word');
  40. chrome.notifications.create('deleteword',{iconUrl:'../asset/img/logo@x128.png',message:'确定从词库中删除['+delWord+']单词么?',type:'basic',title:'移除单词!',buttons:[{title:'Yes'},{title:'No'}]});
  41. // if(confirm('确定从词库中删除['+word+']单词么?')){
  42. // bg.deleteWord(word);
  43. // }
  44. });
  45. $('.title a.minimize').click(function(){
  46. chrome.app.window.current().minimize();
  47. });
  48. $('.title a.close').click(function(){
  49. chrome.app.window.current().close();
  50. });
  51. $('.tabContent .search').on('keyup',function(){
  52. var word = $(this).val();
  53. $(this).parent().find('.list li').hide().filter(":contains('"+word+"')").show();
  54. });
  55. $('.tabs a').click(function(){
  56. $(this).addClass('on').siblings().removeClass('on');
  57. $('.tabContent').removeClass('on').eq($(this).index()).addClass('on');
  58. });
  59. $("#clickme").click(function (source) {
  60. $("#my_file").trigger('click');
  61. })
  62. $("#my_file").on('change',function(){
  63. if($('#my_file')[0].files.length==0) return;
  64. $('.loading').addClass('on').find('p').text('词库导入中,请稍后...');
  65. var file = $('#my_file')[0].files[0];
  66. var reader = new FileReader();
  67. reader.readAsText(file, "utf-8");
  68. reader.onload = function (e) {
  69. var tmpArr = e.target.result.split("\n");
  70. bg.importWord(tmpArr);
  71. }
  72. });
  73. $('.settings a.save').click(function(){
  74. bg.setting = {
  75. timerMin:$('input[name=showTime]').val()
  76. }
  77. chrome.storage.local.set({setting:bg.setting},function(e){
  78. bg.setTimer();
  79. chrome.notifications.create('importNotifi'+(new Date().getTime()),{iconUrl:'../asset/img/logo@x128.png',message:'设置已保存',type:'basic',title:'设置已保存!'});
  80. $('.loading').removeClass('on');
  81. })
  82. });
  83. $('.settings button#importWord').click(function(){
  84. chrome.app.window.create('windows/import.html', {
  85. id:'import',
  86. resizable:false,
  87. alwaysOnTop:true,
  88. innerBounds:{width:300,height:400},
  89. // hidden:true,
  90. frame:{type:'none'}
  91. });
  92. });
  93. $('.importForm .ok').click(function(){
  94. var list = $('.importForm textarea').val().split('\n');
  95. bg.importWord(list);
  96. chrome.app.window.current().close();
  97. });
  98. $('.importForm .cancel').click(function(){
  99. chrome.app.window.current().close();
  100. });
  101. $('button#clearData').click(function(){
  102. chrome.notifications.create('clearData',{iconUrl:'../asset/img/logo@x128.png',message:'确定清除全部数据么?',type:'basic',title:'清除数据!',buttons:[{title:'Yes'},{title:'No'}]});
  103. });
  104. });
  105. function buildNormalList(listIndex){
  106. $('.total .list ul').empty();
  107. $('.loading').addClass('on').find('p').text('词库读取中,请稍后...');
  108. var dom = ''
  109. if(bg.wordDataArr.length) bg.wordDataArr.forEach( function(word) {
  110. var starType = bg.wordDataObj[word]['isStar'];
  111. if(starType==undefined) starType=0;
  112. dom += '<li name="'+word+'" data-word="'+word+'">'+word+'<div class="btn"><a class="info" title="查看详情"><i class="fa fa-comment-o"></i></a><a class="star" data-star="'+starType+'" title="加入收藏"><i class="fa fa-star'+(starType==1?'':'-o')+'"></i></a><a class="del" title="移出词库"><i class="fa fa-trash-o"></i></a></div></li>';
  113. });
  114. $('.total .list ul').append(dom);
  115. $('.tabs a:eq(0) span').text('('+bg.wordDataArr.length+')');
  116. $('.loading').removeClass('on');
  117. }
  118. function buildStarList(listIndex){
  119. $('.starList .list ul').empty();
  120. $('.loading').addClass('on').find('p').text('收藏词库读取中,请稍后...');
  121. var dom = ''
  122. if(bg.wordStarArr.length) bg.wordStarArr.forEach( function(word) {
  123. var starType = bg.wordDataObj[word]['isStar'];
  124. if(starType==undefined) starType=0;
  125. dom += '<li data-word="'+word+'">'+word+'<div class="btn"><a class="info" title="查看详情"><i class="fa fa-comment-o"></i></a><a class="star" data-star="'+starType+'" title="加入收藏"><i class="fa fa-star'+(starType==1?'':'-o')+'"></i></a><a class="del" title="移出词库"><i class="fa fa-trash-o"></i></a></div></li>'
  126. });
  127. $('.starList .list ul').append(dom);
  128. $('.tabs a:eq(1) span').text('('+bg.wordStarArr.length+')');
  129. $('.loading').removeClass('on');
  130. buildNormalList();
  131. }