main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2019-04-21)
  4. * @License MIT
  5. */
  6. var mainWidth = 600,mainHeight = 800;
  7. var wordDataArr,wordStarArr,wordDataObj,wordOpt,currentWord,setting;
  8. var autoShowTimer;
  9. chrome.storage.local.get(['wordsArr','starsArr','wordsObj','wordOpt','setting'],function(items){
  10. wordDataArr = items.wordsArr?items.wordsArr:[];
  11. wordStarArr = items.starsArr?items.starsArr:[];
  12. wordDataObj = items.wordsObj?items.wordsObj:{};
  13. wordOpt = items.wordOpt?items.wordOpt:{};
  14. setting = items.setting?items.setting:{timerMin:10};
  15. setTimer();
  16. });
  17. chrome.app.runtime.onLaunched.addListener(function() {
  18. chrome.app.window.create('windows/main.html', {
  19. id:'main',
  20. innerBounds:{width:mainWidth,height:mainHeight},
  21. resizable:false,
  22. frame:{type:'none'}
  23. },function(mainWindow){
  24. mainWindow.resizeTo(mainWidth,mainHeight);
  25. mainWindow.onClosed.addListener(function(){
  26. chrome.storage.local.set({wordsArr:wordDataArr,starsArr:wordStarArr,wordsObj:wordDataObj,wordOpt:wordOpt,setting:setting});
  27. });
  28. });
  29. });
  30. var showWordInfo = function(word){
  31. var oldWindow = chrome.app.window.get('popup');
  32. if(oldWindow){
  33. createPopup(oldWindow,word);
  34. }else{
  35. chrome.app.window.create('windows/popup.html', {
  36. id:'popup',
  37. resizable:false,
  38. alwaysOnTop:true,
  39. hidden:true,
  40. frame:{type:'none'}
  41. },function(popup){
  42. createPopup(popup,word);
  43. });
  44. }
  45. }
  46. function createPopup(popup,word){
  47. var popWidth = 300,popHeight = 400;
  48. currentWord = word;
  49. chrome.system.display.getInfo(function(display){
  50. var displayOpt = display[0].workArea;
  51. popup.outerBounds.left = displayOpt.width - popWidth - 20;
  52. popup.outerBounds.top = 20 + displayOpt.top;
  53. // popup.innerBounds = {width:popWidth,height:popHeight};
  54. popup.show();
  55. chrome.runtime.sendMessage('updateInfo');
  56. // popup.onClosed.addListener(function(){
  57. // chrome.storage.local.set({wordsObj:bg.wordDataObj,wordOpt:bg.wordOpt});
  58. // });
  59. });
  60. }
  61. function randomShow(){
  62. console.log(1)
  63. if(wordDataArr.length==0) return;
  64. var mid = 3;
  65. var percent = Math.floor(Math.random()*10);
  66. var randomIndex = 0,word;
  67. if(wordStarArr.length==0) percent = 0;
  68. if(percent<=mid){
  69. randomIndex = Math.floor(Math.random()*wordDataArr.length);
  70. word = wordDataArr[randomIndex];
  71. }else{
  72. randomIndex = Math.floor(Math.random()*wordStarArr.length);
  73. word = wordStarArr[randomIndex];
  74. }
  75. showWordInfo(word);
  76. }
  77. //0:unstar 1:star
  78. function starWord(type,word){
  79. if(type==0){
  80. var wi = wordStarArr.indexOf(word);
  81. wordStarArr.splice(wi,1);
  82. }else if(type==1){
  83. wordStarArr.push(word);
  84. }
  85. wordDataObj[word]['isStar'] = type;
  86. wordStarArr.sort();
  87. chrome.storage.local.set({starsArr:wordStarArr,wordsObj:wordDataObj},function(e){
  88. chrome.notifications.create('importNotifi'+(new Date().getTime()),{iconUrl:'../asset/img/logo@x128.png',message:'单词['+word+']已'+(type==0?'从收藏列表移除':'加入收藏'),type:'basic',title:'设置已保存!'});
  89. chrome.runtime.sendMessage('updateStarList');
  90. })
  91. }
  92. function setTimer(){
  93. if(autoShowTimer) clearInterval(autoShowTimer);
  94. autoShowTimer = setInterval(randomShow, setting.timerMin*1000*60);
  95. }