settings.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'JSON_COMPARE',
  16. 'QR_CODE',
  17. 'COLOR_PICKER',
  18. 'REGEXP_TOOL',
  19. 'TIME_STAMP',
  20. 'IMAGE_BASE64',
  21. 'FCP_HELPER_DETECT',
  22. 'SHOW_PAGE_LOAD_TIME',
  23. 'AJAX_DEBUGGER',
  24. 'JS_CSS_PAGE_BEAUTIFY',
  25. 'HTML_TO_MARKDOWN',
  26. 'PAGE_CAPTURE',
  27. 'RANDOM_PASSWORD'
  28. ];
  29. /**
  30. * 获取全部配置项
  31. * @returns {string[]}
  32. * @private
  33. */
  34. let _getAllOpts = () => optionItems;
  35. /**
  36. * 向background-page发送请求,提取配置项
  37. * @param {Function} callback 回调方法
  38. */
  39. let _getOptions = function (callback) {
  40. chrome.runtime.sendMessage({
  41. type: MSG_TYPE.GET_OPTIONS
  42. }, callback);
  43. };
  44. /**
  45. * 向background-page发送请求,保存配置项
  46. * @param {Object} items
  47. */
  48. let _setOptions = function (items) {
  49. chrome.runtime.sendMessage({
  50. type: MSG_TYPE.SET_OPTIONS,
  51. items: items
  52. });
  53. };
  54. /**
  55. * 由background-page触发
  56. * @param {Object} callback
  57. */
  58. let _getOptsFromBgPage = function (callback) {
  59. if (callback && typeof callback === 'function') {
  60. let rst = {};
  61. optionItems.forEach((item) => {
  62. let opt = localStorage.getItem(item);
  63. if (opt !== 'false') {
  64. rst[item] = 'true';
  65. }
  66. });
  67. callback.call(null, rst);
  68. }
  69. };
  70. /**
  71. * 由background-page触发
  72. * @param {Object} items
  73. */
  74. let _setOptsFromBgPage = function (items) {
  75. optionItems.forEach((opt) => {
  76. localStorage.setItem(opt, items.indexOf(opt) > -1 ? 'true' : 'false');
  77. });
  78. };
  79. return {
  80. getAllOpts: _getAllOpts,
  81. setOptsFromBgPage: _setOptsFromBgPage,
  82. getOptsFromBgPage: _getOptsFromBgPage,
  83. getOptions: _getOptions,
  84. setOptions: _setOptions
  85. };
  86. })();