Răsfoiți Sursa

增加FeHelper本地数据自动同步功能:主要用于卸载重装、换电脑等情况

zxlie 6 ani în urmă
părinte
comite
1c4ee957cf

+ 131 - 8
apps/background/index.js

@@ -735,7 +735,7 @@ var BgPageInstance = (function () {
      * @param callback
      * @private
      */
-    let _savePageModifierConfigs = function (params, callback) {
+    let _savePageMonkeyConfigs = function (params, callback) {
         !RegExp.prototype.toJSON && Object.defineProperty(RegExp.prototype, "toJSON", {
             value: RegExp.prototype.toString
         });
@@ -750,11 +750,11 @@ var BgPageInstance = (function () {
      * @returns {*}
      * @private
      */
-    let _getPageModifierConfigs = function (params, callback) {
-        let cacheModifiers = JSON.parse(localStorage.getItem(MSG_TYPE.PAGE_MODIFIER_KEY) || '[]');
+    let _getPageMonkeyConfigs = function (params, callback) {
+        let cacheMonkeys = JSON.parse(localStorage.getItem(MSG_TYPE.PAGE_MODIFIER_KEY) || '[]');
         if (params && params.url) {
             let result = null;
-            cacheModifiers.some(cm => {
+            cacheMonkeys.some(cm => {
                 let m = cm.mPattern.match(/\/(.*)\/(.*)?/);
                 if ((new RegExp(m[1], m[2] || "")).test(params.url)) {
                     result = cm;
@@ -764,7 +764,7 @@ var BgPageInstance = (function () {
             });
             callback && callback(result);
         } else {
-            callback && callback(cacheModifiers);
+            callback && callback(cacheMonkeys);
         }
     };
 
@@ -827,11 +827,11 @@ var BgPageInstance = (function () {
             }
             // 网页涂鸦精灵:获取配置
             else if (request.type === MSG_TYPE.GET_PAGE_MODIFIER_CONFIG) {
-                _getPageModifierConfigs(request.params, callback);
+                _getPageMonkeyConfigs(request.params, callback);
             }
             // 网页涂鸦精灵:保存配置
             else if (request.type === MSG_TYPE.SAVE_PAGE_MODIFIER_CONFIG) {
-                _savePageModifierConfigs(request.params, callback);
+                _savePageMonkeyConfigs(request.params, callback);
             }
 
             // ===========================以下为编码规范检测====start==================================
@@ -931,6 +931,128 @@ var BgPageInstance = (function () {
         }, 1000 * 10);
     };
 
+    /**
+     * 将本地存储在localStorage的数据,同步到Google,确保每次更换浏览器,配置也能随之同步
+     * @private
+     */
+    let _localDataSyncByGoogle = function () {
+
+        let allNoteKeys = [];
+
+        // 数据下载
+        let funcDownload = function () {
+            // 从服务端同步数据
+            chrome.storage.sync.get({
+                fhConfigs: null,
+                stickyNotes: null,
+                pageMonkey: null,
+                otherData: null
+            }, result => {
+
+                // 先把服务端存储的所有笔记key存储起来,上报的时候要用
+                allNoteKeys = result.stickyNotes || [];
+
+                let localDataSize = localStorage.length;
+                // 做数据对比、数据更新、数据上报
+                ['fhConfigs', 'stickyNotes', 'pageMonkey', 'otherData'].forEach(key => {
+                    if (result[key] !== null && typeof result[key] === 'object') {
+                        // 以下情况都强制从服务端强制同步配置下来:
+                        // 1、如果本地缓存数量少于10
+                        // 2、本地数据缓存最后一次上报时间,比服务端已保存的时间早10天
+                        if (localDataSize < 10) {
+                            if (key === 'stickyNotes') {
+                                result[key].forEach(k => {
+                                    chrome.storage.sync.get(JSON.parse(`{"stickynote${k}":null}`), r => {
+                                        if (r !== null) {
+                                            localStorage.setItem('stickynote' + k, r['stickynote' + k]);
+                                        }
+                                    });
+                                });
+                            } else {
+                                Object.keys(result[key]).forEach(item => {
+                                    localStorage.setItem(item, result[key][item]);
+                                });
+                            }
+
+                            console.log(key, ' 相关数据均已从服务器同步至本地!');
+                        }
+                    }
+                });
+            });
+        };
+
+        // 数据上报
+        let funcUpload = function () {
+            // 获取本地缓存的数据
+            let theLocalData = {
+                fhConfigs: {},
+                stickyNotes: {},
+                pageMonkey: {},
+                otherData: {}
+            };
+            let theKey, theData;
+            for (let i = 0; i < localStorage.length; i++) {
+                theKey = localStorage.key(i);
+                theData = localStorage.getItem(localStorage.key(i));
+                if (/^stickynote/.test(theKey)) { // 便签笔记
+                    theLocalData.stickyNotes[theKey] = theData;
+                } else if (!/[^A-Z_]/.test(theKey)) { // FeHelper配置项
+                    theLocalData.fhConfigs[theKey] = theData;
+                } else if (theKey === 'PAGE-MODIFIER-LOCAL-STORAGE-KEY') { // 网页油猴
+                    theLocalData.pageMonkey[theKey] = theData;
+                } else if (!/FE_ENCODING_PREFIX_/.test(theKey)) {  // 其他缓存数据
+                    theLocalData.otherData[theKey] = theData;
+                }
+            }
+
+            // 做数据对比、数据更新、数据上报
+            ['fhConfigs', 'stickyNotes', 'pageMonkey', 'otherData'].forEach(key => {
+                if (Object.keys(theLocalData[key]).length) {
+
+                    // 上报数据
+                    let uploadData = {};
+                    if (key === 'stickyNotes') {
+                        // 服务器端的数据先做清空处理
+                        if (allNoteKeys && allNoteKeys.length) {
+                            chrome.storage.sync.remove(allNoteKeys);
+                        }
+
+                        uploadData[key] = Object.keys(theLocalData[key]).map(k => k.replace(/^stickynote/, ''));
+                        if (JSON.stringify(uploadData).length <= chrome.storage.sync.QUOTA_BYTES_PER_ITEM) {
+                            chrome.storage.sync.set(uploadData, () => {
+                                Object.keys(theLocalData.stickyNotes).forEach(k => {
+                                    let tmp = {};
+                                    tmp[k] = theLocalData.stickyNotes[k];
+                                    if (JSON.stringify(tmp).length <= chrome.storage.sync.QUOTA_BYTES_PER_ITEM) {
+                                        chrome.storage.sync.set(tmp);
+                                    } else {
+                                        console.log('便签笔记 ', k, ' 数据量太大,无法同步到服务器!');
+                                    }
+                                });
+                                console.log(key, ' 数据同步到服务器成功!')
+                            });
+                        } else {
+                            console.log(key, ' 数据量太大,无法同步到服务器!');
+                        }
+
+                    } else {
+                        uploadData[key] = theLocalData[key];
+
+                        if (JSON.stringify(uploadData).length <= chrome.storage.sync.QUOTA_BYTES_PER_ITEM) {
+                            chrome.storage.sync.set(uploadData, () => console.log(key, ' 数据同步到服务器成功!'));
+                        } else {
+                            console.log(key, ' 数据量太大,无法同步到服务器!');
+                        }
+                    }
+                }
+            });
+        };
+
+
+        setTimeout(() => funcDownload(), 0);    // 立即下载数据同步到本地
+        setTimeout(() => funcUpload(), 10000);  // 稍后将本地数据上报到服务器
+    };
+
     /**
      * 初始化
      */
@@ -938,6 +1060,7 @@ var BgPageInstance = (function () {
         _checkUpdate();
         _addExtensionListener();
         _createOrRemoveContextMenu();
+        _localDataSyncByGoogle();
     };
 
     /**
@@ -945,7 +1068,7 @@ var BgPageInstance = (function () {
      * @param url
      * @private
      */
-    let _openUrl = function(url){
+    let _openUrl = function (url) {
         chrome.tabs.create({url: url});
     };
 

+ 3 - 2
apps/manifest.json

@@ -1,6 +1,6 @@
 {
   "name": "WEB前端助手(FeHelper)-Dev",
-  "version": "2019.09.0320",
+  "version": "2019.09.0915",
   "manifest_version": 2,
   "default_locale": "zh_CN",
   "description": "Awesome,All In One的一个工具,包含多个独立小应用,比如:Json工具、代码美化、代码压缩、二维码、Postman、markdown、网页油猴、便签笔记、信息加密与解密、随机密码生成、Crontab等等!",
@@ -34,6 +34,7 @@
     "cookies",
     "notifications",
     "activeTab",
+    "storage",
     "unlimitedStorage",
     "<all_urls>"
   ],
@@ -139,7 +140,7 @@
         "https://*/*"
       ],
       "js": [
-        "page-modifier/content-script.js"
+        "page-monkey/content-script.js"
       ],
       "run_at": "document_end",
       "all_frames": false

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
apps/options/index.html


+ 0 - 0
apps/page-modifier/content-script.js → apps/page-monkey/content-script.js


+ 0 - 0
apps/page-modifier/index.css → apps/page-monkey/index.css


+ 0 - 0
apps/page-modifier/index.html → apps/page-monkey/index.html


+ 0 - 0
apps/page-modifier/index.js → apps/page-monkey/index.js


+ 1 - 1
apps/popup/index.css

@@ -160,7 +160,7 @@ ul.fe-function-list li.-x-remove-bg b {
 ul.fe-function-list li.-x-multi-toolkit b {
     background-position: -176px -111px;
 }
-ul.fe-function-list li.-x-page-modifier b {
+ul.fe-function-list li.-x-page-monkey b {
     background-position: -32px -95px;
 }
 ul.fe-function-list li.-x-post-man b {

+ 1 - 1
apps/popup/index.html

@@ -45,7 +45,7 @@
                     <b></b>简易Postman</li>
                 <li class="-x-grid-ruler" @click="runHelper('GRID_RULER',0)" v-if="canMeShow.GRID_RULER && !isFireFox">
                     <b></b>页面栅格标尺</li>
-                <li class="-x-page-modifier" @click="runHelper('PAGE_MODIFIER',1)" v-if="canMeShow.PAGE_MODIFIER">
+                <li class="-x-page-monkey" @click="runHelper('PAGE_MODIFIER',1)" v-if="canMeShow.PAGE_MODIFIER">
                     <b></b>网页油猴工具</li>
                 <li class="-x-multi-toolkit" @click="runHelper('MULTI_TOOLKIT',1)" v-if="canMeShow.MULTI_TOOLKIT">
                     <b></b>多维小工具集</li>

+ 4 - 4
apps/static/js/msg_type.js

@@ -117,11 +117,11 @@ const MSG_TYPE = {
     // 多维小工具
     MULTI_TOOLKIT: 'toolkit',
 
-    // 打开page-modifier配置页
-    PAGE_MODIFIER:'page-modifier',
-    // 获取某个url对应的page-modifier配置
+    // 打开page-monkey配置页
+    PAGE_MODIFIER:'page-monkey',
+    // 获取某个url对应的page-monkey配置
     GET_PAGE_MODIFIER_CONFIG:'get_page_modifier_config',
-    // 保存page-modifier配置
+    // 保存page-monkey配置
     SAVE_PAGE_MODIFIER_CONFIG:'save_page_modifier_config',
     // page-config配置项的本地缓存key
     PAGE_MODIFIER_KEY:'PAGE-MODIFIER-LOCAL-STORAGE-KEY',

+ 1 - 1
apps/sticky-notes/html5sticky.js

@@ -633,7 +633,7 @@ html5sticky.createFolder = function (folder, time) {
         time = time || new Date().getTime();
         html5sticky.saveFolder(folder, time);
         return $('<li><span></span><i>(0)</i></li>').find('span').text(folder).end().attr('id', 'f_' + time).appendTo('#folders');
-    } else {
+    } else if (folder !== null) {
         return alert('文件夹名不能为空!');
     }
 };

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff