settings.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * FeHelper Settings Tools
  3. */
  4. module.exports = (() => {
  5. // 页面json格式化强制开启
  6. let MSG_TYPE = Tarp.require('../static/js/msg_type');
  7. // 所有配置项
  8. let optionItems = [
  9. 'opt_item_contextMenus',
  10. 'JSON_PAGE_FORMAT',
  11. 'EN_DECODE',
  12. 'CODE_BEAUTIFY',
  13. 'CODE_COMPRESS',
  14. 'JSON_FORMAT',
  15. 'QR_CODE',
  16. 'COLOR_PICKER',
  17. 'REGEXP_TOOL',
  18. 'TIME_STAMP',
  19. 'IMAGE_BASE64',
  20. 'FCP_HELPER_DETECT',
  21. 'SHOW_PAGE_LOAD_TIME',
  22. 'AJAX_DEBUGGER'
  23. ];
  24. /**
  25. * 获取全部配置项
  26. * @returns {string[]}
  27. * @private
  28. */
  29. let _getAllOpts = () => optionItems;
  30. /**
  31. * 向background-page发送请求,提取配置项
  32. * @param {Function} callback 回调方法
  33. */
  34. let _getOptions = function (callback) {
  35. chrome.runtime.sendMessage({
  36. type: MSG_TYPE.GET_OPTIONS
  37. }, callback);
  38. };
  39. /**
  40. * 向background-page发送请求,保存配置项
  41. * @param {Object} items
  42. */
  43. let _setOptions = function (items) {
  44. chrome.runtime.sendMessage({
  45. type: MSG_TYPE.SET_OPTIONS,
  46. items: items
  47. });
  48. };
  49. /**
  50. * 由background-page触发
  51. * @param {Object} callback
  52. */
  53. let _getOptsFromBgPage = function (callback) {
  54. if (callback && typeof callback === 'function') {
  55. let rst = {};
  56. optionItems.forEach((item) => {
  57. let opt = localStorage.getItem(item);
  58. if (opt !== 'false') {
  59. rst[item] = 'true';
  60. }
  61. });
  62. callback.call(null, rst);
  63. }
  64. };
  65. /**
  66. * 由background-page触发
  67. * @param {Object} items
  68. */
  69. let _setOptsFromBgPage = function (items) {
  70. optionItems.forEach((opt) => {
  71. localStorage.setItem(opt, items.indexOf(opt) > -1 ? 'true' : 'false');
  72. });
  73. };
  74. return {
  75. getAllOpts: _getAllOpts,
  76. setOptsFromBgPage: _setOptsFromBgPage,
  77. getOptsFromBgPage: _getOptsFromBgPage,
  78. getOptions: _getOptions,
  79. setOptions: _setOptions
  80. };
  81. })();