1
0

fe-option.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * FeHelper配置项
  3. * @author zhaoxianlie
  4. */
  5. baidu.feOption = (function(){
  6. /**
  7. * 将这些配置项保存到background-page,这样才能对每个页面生效
  8. * @param {Object} items {key:value}
  9. */
  10. var _saveOptionItemByBgPage = function(items){
  11. for(var key in items){
  12. window.localStorage.setItem(key,items[key]);
  13. }
  14. };
  15. /**
  16. * 从background-page获取配置项
  17. * @param {Object} items ["",""]
  18. * @return {key:value}
  19. */
  20. var _getOptionItemByBgPage = function(items){
  21. var rst = {};
  22. for(var i = 0,len = items.length;i < len;i++){
  23. rst[items[i]] = window.localStorage.getItem(items[i]);
  24. }
  25. return rst;
  26. };
  27. /**
  28. * 向background-page发送请求,提取配置项
  29. * @param {Object} items
  30. * @param {Function} 回调方法
  31. */
  32. var _goGetOptions = function(items,callback){
  33. chrome.extension.sendMessage({
  34. type : MSG_TYPE.GET_OPTIONS,
  35. items : items
  36. },callback);
  37. };
  38. /**
  39. * 向background-page发送请求,保存配置项
  40. * @param {Object} items
  41. * @param {Object} callback
  42. */
  43. var _goSetOptions = function(items){
  44. chrome.extension.sendMessage({
  45. type : MSG_TYPE.SET_OPTIONS,
  46. items : items
  47. });
  48. };
  49. /**
  50. * 由background-page触发
  51. * @param {Object} items
  52. * @param {Object} callback
  53. */
  54. var _doGetOptions = function(items,callback){
  55. if(callback && typeof callback == 'function'){
  56. callback.call(null,_getOptionItemByBgPage(items));
  57. }
  58. };
  59. /**
  60. * 由background-page触发
  61. * @param {Object} items
  62. * @param {Object} callback
  63. */
  64. var _doSetOptions = function(items){
  65. _saveOptionItemByBgPage(items);
  66. };
  67. /**
  68. * 获取某一项配置
  69. * @param {String} optName 配置参数名
  70. */
  71. var _getOptionItem = function(optName){
  72. return _getOptionItemByBgPage([optName])[optName];
  73. };
  74. /**
  75. * 保存启动项
  76. */
  77. var _save_opt_form_start = function(){
  78. var items = {};
  79. items['opt_item_contextMenus'] = $('#opt_item_contextMenus').attr('checked');
  80. items['opt_item_showfdpmenu'] = $('#opt_item_showfdpmenu').attr('checked');
  81. items['opt_item_autojson'] = $('#opt_item_autojson').attr('checked');
  82. items['opt_item_notification'] = $('#opt_item_notification').val();
  83. _goSetOptions(items);
  84. };
  85. /**
  86. * 显示启动项
  87. */
  88. var _show_opt_form_start = function(){
  89. var optItems = ['opt_item_contextMenus',"opt_item_showfdpmenu",
  90. "opt_item_autojson","opt_item_notification"];
  91. _goGetOptions(optItems,function(opts){
  92. $.each(optItems,function(i,item){
  93. if(i == 3) {
  94. $('#' + item).val(opts[item]);
  95. } else if(opts[item] === 'true') {
  96. $('#' + item).attr('checked','checked');
  97. }
  98. });
  99. })
  100. };
  101. /**
  102. * 保存相应的表单配置
  103. * @param {Object} form_id
  104. */
  105. var _save = function(form_id){
  106. switch(form_id){
  107. case 'opt_form_start':
  108. _save_opt_form_start();
  109. break;
  110. case '':
  111. break;
  112. }
  113. };
  114. /**
  115. * 关闭配置页面
  116. */
  117. var _closeTab = function(){
  118. chrome.tabs.query({active:true, currentWindow:true}, function (tabs) {
  119. var tab = tabs[0];
  120. chrome.tabs.remove(tab.id);
  121. });
  122. };
  123. /**
  124. * 事件绑定
  125. */
  126. var _bindEvent = function(){
  127. //左边的可选项
  128. $('#fe-opt-list-item>li').click(function(e){
  129. var $this = $(this).siblings().removeClass('selected').end().addClass('selected');
  130. $($this.attr('data-content')).siblings().removeClass('selected').addClass('fe-hide').end().removeClass('fe-hide').addClass('selected');
  131. });
  132. //为每个表单注册submit事件
  133. $('.right form').submit(function(e){
  134. //保存各个值
  135. _save($(this).attr('id'));
  136. //关闭当前tab
  137. _closeTab();
  138. e.preventDefault();
  139. e.stopPropagation();
  140. });
  141. //给保存按钮注册事件
  142. $('#btn_close').click(function(){
  143. //关闭当前tab
  144. _closeTab();
  145. });
  146. //给保存按钮注册事件
  147. $('#btn_save').click(function(){
  148. $('.right div.selected form').submit();
  149. });
  150. };
  151. /**
  152. * 初始化各个配置项
  153. */
  154. var _initOptions = function(){
  155. _show_opt_form_start();
  156. };
  157. /**
  158. * 初始化
  159. */
  160. var _init = function(){
  161. _bindEvent();
  162. _initOptions();
  163. };
  164. return {
  165. init : _init,
  166. doSetOptions : _doSetOptions,
  167. doGetOptions : _doGetOptions,
  168. getOptionItem : _getOptionItem,
  169. getOptions : _goGetOptions,
  170. setOptions : _goSetOptions
  171. };
  172. })();