Browse Source

增加功能:下载和分享插件

zxlie 6 years ago
parent
commit
a8fe50fd44

+ 107 - 6
apps/background/index.js

@@ -238,9 +238,9 @@ var BgPageInstance = (function () {
     /**
      * 屏幕栅格标尺
      */
-    let _doGridDetect = function(tab){
+    let _doGridDetect = function (tab) {
         chrome.tabs.sendMessage(tab.id, {
-            type : MSG_TYPE.GRID_RULER
+            type: MSG_TYPE.GRID_RULER
         });
     };
 
@@ -285,6 +285,104 @@ var BgPageInstance = (function () {
         });
     };
 
+    /**
+     * 检测Google chrome服务能不能访问,在2s内检测心跳
+     * @param success
+     * @param failure
+     */
+    let detectGoogleDotCom = function (success, failure) {
+        Promise.race([
+            fetch('https://clients2.google.com/service/update2/crx'),
+            new Promise(function (resolve, reject) {
+                setTimeout(() => reject(new Error('request timeout')), 2000)
+            })])
+            .then((data) => {
+                success && success();
+            }).catch(() => {
+            failure && failure();
+        });
+    };
+
+    /**
+     * 从google官方渠道下载chrome扩展
+     * @param crxId 需要下载的extension id
+     * @param crxName 扩展名称
+     * @param callback 下载动作结束后的回调
+     */
+    let downloadCrxFileByCrxId = function (crxId, crxName, callback) {
+        detectGoogleDotCom(() => {
+            // google可以正常访问,则正常下载
+            let url = "https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D"
+                + crxId + "%26uc&prodversion=" + navigator.userAgent.split("Chrome/")[1].split(" ")[0];
+            if (!chrome.downloads) {
+                let a = document.createElement('a');
+                a.href = url;
+                a.download = crxName || crxId;
+                (document.body || document.documentElement).appendChild(a);
+                a.click();
+                a.remove();
+            } else {
+                chrome.downloads.download({
+                    url: url,
+                    filename: crxName || crxId,
+                    conflictAction: 'overwrite',
+                    saveAs: true
+                }, function (downloadId) {
+                    if (chrome.runtime.lastError) {
+                        alert('抱歉,下载失败!错误信息:' + chrome.runtime.lastError.message);
+                    }
+                });
+            }
+        }, () => {
+            // google不能正常访问
+            callback ? callback() : alert('抱歉,下载失败!');
+        });
+
+    };
+
+    /**
+     * 从chrome webstore下载crx文件
+     * 在chrome extension详情页使用
+     */
+    let downloadCrxFileFromWebStoreDetailPage = function (callback) {
+
+        chrome.tabs.getSelected(null, function (tab) {
+            let crxId = tab.url.split("/")[6].split('?')[0];
+            let crxName = tab.title.split(" - Chrome")[0] + ".crx";
+            crxName = crxName.replace(/[&\/\\:"*<>|?]/g, '');
+
+            downloadCrxFileByCrxId(crxId, crxName, callback);
+        });
+    };
+
+    /**
+     * 通过右键菜单下载或者分享crx
+     * @param tab
+     * @private
+     */
+    let _downloadCrx = function (tab) {
+        let isWebStoreDetailPage = tab.url.indexOf('https://chrome.google.com/webstore/detail/') === 0;
+        if (isWebStoreDetailPage) {
+            // 如果是某个chrome extension的详情页面了,直接下载当前crx文件
+            downloadCrxFileFromWebStoreDetailPage(() => {
+                alert('下载失败,可能是当前网络无法访问Google站点!');
+            });
+        } else {
+            // 否则,下载FeHelper并分享出去:ID:pkgccpejnmalmdinmhkkfafefagiiiad
+            // feHelper.manifest.name
+            if (confirm('下载最新版【FeHelper】并分享给其他小伙伴儿,走你~~~')) {
+                let crxId = MSG_TYPE.STABLE_EXTENSION_ID;
+                let crxName = feHelper.manifest.name + '- latestVersion';
+
+                downloadCrxFileByCrxId(crxId, crxName, () => {
+                    chrome.tabs.create({
+                        url: MSG_TYPE.DOWNLOAD_FROM_GITHUB
+                    });
+                });
+            }
+        }
+    };
+
     /**
      * 右键菜单创建工具
      * @param menuList
@@ -392,11 +490,14 @@ var BgPageInstance = (function () {
                     MENU_STICKY_NOTE: function (info, tab) {
                         _openFileAndRun(tab, MSG_TYPE.STICKY_NOTES);
                     },
-                    MENU_REMOVE_BG: function(info,tab){
+                    MENU_REMOVE_BG: function (info, tab) {
                         _openFileAndRun(tab, MSG_TYPE.REMOVE_BG);
                     },
-                    MENU_GRID_RULER: function(info,tab){
+                    MENU_GRID_RULER: function (info, tab) {
                         _doGridDetect(tab);
+                    },
+                    MENU_DOWNLOAD_CRX: function (info, tab) {
+                        _downloadCrx(tab);
                     }
                 };
 
@@ -637,7 +738,7 @@ var BgPageInstance = (function () {
                 });
             }
             // 判断菜单是否保存过
-            else if(request.type === MSG_TYPE.MENU_SAVED){
+            else if (request.type === MSG_TYPE.MENU_SAVED) {
                 Settings.didMenuSettingSaved(callback);
             }
             //判断是否可以针对json页面进行自动格式化
@@ -669,7 +770,7 @@ var BgPageInstance = (function () {
                 chrome.runtime.openOptionsPage();
             }
             // 开启remove-bg功能
-            else if(request.type === MSG_TYPE.REMOVE_PERSON_IMG_BG) {
+            else if (request.type === MSG_TYPE.REMOVE_PERSON_IMG_BG) {
                 Tarp.require('../remove-bg/proxy').addBackgroundRemoveListener(callback);
             }
 

+ 2 - 2
apps/manifest.json

@@ -1,9 +1,9 @@
 {
   "name": "WEB前端助手(FeHelper)",
-  "version": "2019.03.2016",
+  "version": "2019.04.1715",
   "manifest_version": 2,
   "default_locale": "zh_CN",
-  "description": "FE助手:包括JSON格式化、二维码生成与解码、信息编解码、代码压缩、美化、页面取色、Markdown与HTML互转、网页滚动截屏、正则表达式、时间转换工具、编码规范检测、页面性能检测、Ajax接口调试、密码生成器、JSON比对工具、网页编码设置、便签笔记",
+  "description": "FE助手:JSON格式化、JSON比对、二维码生成与解码、信息编解码、代码压缩&美化、页面取色、Markdown与HTML、网页截屏、编码设置、正则、时间转换、网页性能检测、Ajax调试、密码生成器、便签笔记、chrome插件下载",
   "icons": {
     "16": "static/img/fe-16.png",
     "48": "static/img/fe-48.png",

+ 4 - 0
apps/options/settings.js

@@ -138,6 +138,10 @@ module.exports = (() => {
         MENU_REMOVE_BG: {
             icon: '⚘',
             text: '人像背景移除'
+        },
+        MENU_DOWNLOAD_CRX: {
+            icon:'↬',
+            text: '下载/分享插件'
         }
     };
 

+ 2 - 2
apps/page-capture/capture-api.js

@@ -7,8 +7,8 @@
 
 module.exports = function (MSG_TYPE) {
 
-    let MAX_PRIMARY_DIMENSION = 15000 * 2,
-        MAX_SECONDARY_DIMENSION = 4000 * 2,
+    let MAX_PRIMARY_DIMENSION = 50000 * 2,
+        MAX_SECONDARY_DIMENSION = 20000 * 2,
         MAX_AREA = MAX_PRIMARY_DIMENSION * MAX_SECONDARY_DIMENSION;
 
     let matches = ['http://*/*', 'https://*/*', 'ftp://*/*', 'file://*/*'],

+ 5 - 0
apps/static/js/msg_type.js

@@ -3,6 +3,11 @@
  */
 const MSG_TYPE = {
 
+    // 正式版chrome extension id
+    STABLE_EXTENSION_ID:'pkgccpejnmalmdinmhkkfafefagiiiad',
+    // github 上的下载地址
+    DOWNLOAD_FROM_GITHUB:'https://github.com/zxlie/FeHelper/tree/master/apps/static/screenshot/crx',
+
     // 编码规范检测
     CODE_STANDARDS: "code_standards",
     FCP_HELPER_INIT: "fcp_helper_init",