|
@@ -5,39 +5,44 @@ module.exports = (() => {
|
|
|
|
|
|
// 页面json格式化强制开启
|
|
|
let pageJsonMustFormat = false;
|
|
|
+ let MSG_TYPE = Tarp.require('../static/js/core/msg_type');
|
|
|
|
|
|
- /**
|
|
|
- * 将这些配置项保存到background-page,这样才能对每个页面生效
|
|
|
- * @param {Object} items {key:value}
|
|
|
- */
|
|
|
- let _saveOptionItemByBgPage = function (items) {
|
|
|
- for (let key in items) {
|
|
|
- window.localStorage.setItem(key, items[key]);
|
|
|
- }
|
|
|
- };
|
|
|
+ // 所有配置项
|
|
|
+ let optionItems = [
|
|
|
+ 'opt_item_contextMenus',
|
|
|
+ 'EN_DECODE',
|
|
|
+ 'CODE_BEAUTIFY',
|
|
|
+ 'CODE_COMPRESS',
|
|
|
+ 'JSON_FORMAT',
|
|
|
+ 'QR_CODE',
|
|
|
+ 'COLOR_PICKER',
|
|
|
+ 'REGEXP_TOOL',
|
|
|
+ 'TIME_STAMP',
|
|
|
+ 'IMAGE_BASE64',
|
|
|
+ 'FCP_HELPER_DETECT',
|
|
|
+ 'SHOW_PAGE_LOAD_TIME',
|
|
|
+ 'AJAX_DEBUGGER'
|
|
|
+ ];
|
|
|
+ if (!pageJsonMustFormat) {
|
|
|
+ optionItems.push('JSON_PAGE_FORMAT');
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * 从background-page获取配置项
|
|
|
- * @param {Object} items ["",""]
|
|
|
- * @return {key:value}
|
|
|
+ * 获取全部配置项
|
|
|
+ * @returns {string[]}
|
|
|
+ * @private
|
|
|
*/
|
|
|
- let _getOptionItemByBgPage = function (items) {
|
|
|
- let rst = {};
|
|
|
- for (let i = 0, len = items.length; i < len; i++) {
|
|
|
- rst[items[i]] = window.localStorage.getItem(items[i]);
|
|
|
- }
|
|
|
- return rst;
|
|
|
- };
|
|
|
+ let _getAllOpts = () => optionItems;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 向background-page发送请求,提取配置项
|
|
|
* @param {Object} items
|
|
|
- * @param {Function} 回调方法
|
|
|
+ * @param {Function} callback 回调方法
|
|
|
*/
|
|
|
- let _goGetOptions = function (items, callback) {
|
|
|
+ let _getOptions = function (callback) {
|
|
|
chrome.extension.sendMessage({
|
|
|
- type: MSG_TYPE.GET_OPTIONS,
|
|
|
- items: items
|
|
|
+ type: MSG_TYPE.GET_OPTIONS
|
|
|
}, callback);
|
|
|
};
|
|
|
|
|
@@ -45,7 +50,7 @@ module.exports = (() => {
|
|
|
* 向background-page发送请求,保存配置项
|
|
|
* @param {Object} items
|
|
|
*/
|
|
|
- let _goSetOptions = function (items) {
|
|
|
+ let _setOptions = function (items) {
|
|
|
chrome.extension.sendMessage({
|
|
|
type: MSG_TYPE.SET_OPTIONS,
|
|
|
items: items
|
|
@@ -54,12 +59,18 @@ module.exports = (() => {
|
|
|
|
|
|
/**
|
|
|
* 由background-page触发
|
|
|
- * @param {Object} items
|
|
|
* @param {Object} callback
|
|
|
*/
|
|
|
- let _doGetOptions = function (items, callback) {
|
|
|
- if (callback && typeof callback == 'function') {
|
|
|
- callback.call(null, _getOptionItemByBgPage(items));
|
|
|
+ let _getOptsFromBgPage = function (callback) {
|
|
|
+ if (callback && typeof callback === 'function') {
|
|
|
+ let rst = {};
|
|
|
+ optionItems.forEach((item) => {
|
|
|
+ let opt = localStorage.getItem(item);
|
|
|
+ if (opt !== 'false') {
|
|
|
+ rst[item] = 'true';
|
|
|
+ }
|
|
|
+ });
|
|
|
+ callback.call(null, rst);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -67,57 +78,18 @@ module.exports = (() => {
|
|
|
* 由background-page触发
|
|
|
* @param {Object} items
|
|
|
*/
|
|
|
- let _doSetOptions = function (items) {
|
|
|
- _saveOptionItemByBgPage(items);
|
|
|
- };
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取某一项配置
|
|
|
- * @param {String} optName 配置参数名
|
|
|
- */
|
|
|
- let _getOptionItem = function (optName) {
|
|
|
- return _getOptionItemByBgPage([optName])[optName];
|
|
|
- };
|
|
|
-
|
|
|
- // 所有配置项
|
|
|
- let optionItems = [
|
|
|
- 'opt_item_contextMenus',
|
|
|
- 'EN_DECODE',
|
|
|
- 'CODE_BEAUTIFY',
|
|
|
- 'CODE_COMPRESS',
|
|
|
- 'JSON_FORMAT',
|
|
|
- 'QR_CODE',
|
|
|
- 'COLOR_PICKER',
|
|
|
- 'REGEXP_TOOL',
|
|
|
- 'TIME_STAMP',
|
|
|
- 'IMAGE_BASE64',
|
|
|
- 'FCP_HELPER_DETECT',
|
|
|
- 'SHOW_PAGE_LOAD_TIME',
|
|
|
- 'AJAX_DEBUGGER'
|
|
|
- ];
|
|
|
- if (!pageJsonMustFormat) {
|
|
|
- optionItems.push('JSON_PAGE_FORMAT');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 保存启动项
|
|
|
- */
|
|
|
- let _save_opt_form_start = function () {
|
|
|
- let items = {};
|
|
|
- $.each(optionItems, function (i, item) {
|
|
|
- items[item] = $('#' + item).attr('checked');
|
|
|
+ let _setOptsFromBgPage = function (items) {
|
|
|
+ optionItems.forEach((opt) => {
|
|
|
+ localStorage.setItem(opt, items.indexOf(opt) > -1 ? 'true' : 'false');
|
|
|
});
|
|
|
-
|
|
|
- _goSetOptions(items);
|
|
|
};
|
|
|
|
|
|
return {
|
|
|
pageJsonMustFormat: pageJsonMustFormat,
|
|
|
- optionItems: optionItems,
|
|
|
- doSetOptions: _doSetOptions,
|
|
|
- doGetOptions: _doGetOptions,
|
|
|
- getOptionItem: _getOptionItem,
|
|
|
- getOptions: _goGetOptions,
|
|
|
- setOptions: _goSetOptions
|
|
|
+ getAllOpts: _getAllOpts,
|
|
|
+ setOptsFromBgPage: _setOptsFromBgPage,
|
|
|
+ getOptsFromBgPage: _getOptsFromBgPage,
|
|
|
+ getOptions: _getOptions,
|
|
|
+ setOptions: _setOptions
|
|
|
};
|
|
|
})();
|