From 5d3ec9abd3eca0b9c1ddceca1a4968242997a0d3 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Thu, 5 Mar 2020 14:56:43 +0100
Subject: [PATCH] [enh] Build as webExtension (.xpi) file

---
 gulpfile.js                     |  63 +++++++++++++++-
 package.json                    |   2 +-
 resources/web-ext/background.js |  49 +++++++++++++
 resources/web-ext/manifest.json |  14 ++++
 web-ext-config.js               |   4 +
 yarn.lock                       | 125 +++++++++-----------------------
 6 files changed, 162 insertions(+), 95 deletions(-)
 create mode 100644 resources/web-ext/background.js
 create mode 100644 resources/web-ext/manifest.json
 create mode 100644 web-ext-config.js

diff --git a/gulpfile.js b/gulpfile.js
index a921118a..c6428fe3 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -269,6 +269,7 @@ function pluginSass() {
 function webClean() {
   return del([
     './dist/web/www',
+    './dist/web/extension',
     './dist/web/build'
   ]);
 }
@@ -615,9 +616,65 @@ function webZip() {
     .pipe(gulp.dest('./dist/web/build'));
 }
 
+
+function webExtCopyFiles() {
+  log(colors.green('Copy web extension files...'));
+
+  const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
+  const manifestFilter = filter(["**/manifest.json"], { restore: true });
+  const txtFilter = filter(["**/*.txt"], { restore: true });
+
+  // Copy files
+  const wwwPath = './dist/web/www';
+  const resourcesPath = './resources/web-ext';
+  return gulp.src([
+    wwwPath + '/**/*',
+    wwwPath + '/dist_js/*.*',
+
+    // Remove debug JS files
+    '!' + wwwPath + '/dist_js/cesium.js',
+    '!' + wwwPath + '/dist_js/vendor.js',
+    '!' + wwwPath + '/dist_js/cesium-api*.js',
+    '!' + wwwPath + '/dist_js/vendor-api*.js',
+
+    wwwPath + '/dist_css/*.*',
+    // Remove debug CSS files
+    '!' + wwwPath + '/dist_css/cesium.css',
+    '!' + wwwPath + '/dist_css/cesium-api*.css',
+    '!' + wwwPath + '/debug.html',
+    '!' + wwwPath + '/manifest.json',
+
+    // Add specific resource
+    resourcesPath + '/**/*.*'
+   ])
+
+    // Process TXT files: Add the UTF-8 BOM character
+    .pipe(txtFilter)
+    .pipe(header('\ufeff'))
+    .pipe(txtFilter.restore)
+
+    // Replace version in 'manifest.json' file
+    .pipe(manifestFilter)
+    .pipe(replace(/\"version\": \"[^\"]*\"/, '"version": "' + version + '"'))
+    .pipe(manifestFilter.restore)
+
+    .pipe(gulp.dest('./dist/web/ext'));
+}
+
+function webExtXpi() {
+  const srcPath = './dist/web/ext';
+  const distPath = './dist/web/build';
+  const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
+
+  return gulp.src(srcPath + '/**/*.*')
+    .pipe(zip('cesium-v'+version+'-extension.xpi'))
+    .pipe(gulp.dest(distPath));
+}
+
 function webBuildSuccess(done) {
   var version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
   log(colors.green("Build for web created at: 'dist/web/build/cesium-v" + version + "-web.zip'"));
+  log(colors.green("Build for web extension created at: 'dist/webExtension/cesium-v" + version + "-extension.xpi'"));
   done();
 }
 
@@ -674,7 +731,9 @@ gulp.task('webCleanUnusedFiles', ['webApiUglify'], webCleanUnusedFiles);
 gulp.task('webCleanUnusedDirectories', ['webCleanUnusedFiles'], webCleanUnusedDirectories);
 gulp.task('webZip', ['webCleanUnusedDirectories'], webZip);
 
-gulp.task('webBuild', ['webZip'], webBuildSuccess);
+gulp.task('webExtCopyFiles', ['webCleanUnusedDirectories'], webExtCopyFiles);
+gulp.task('webExtXpi', ['webExtCopyFiles'], webExtXpi);
 
-gulp.task('build:web', ['webZip'], webBuildSuccess); // @deprecated
+gulp.task('webBuild', ['webZip', 'webExtXpi'], webBuildSuccess);
+gulp.task('build:web', ['webBuild']); // = webBuild
 
diff --git a/package.json b/package.json
index 638bdc18..88aaa115 100644
--- a/package.json
+++ b/package.json
@@ -215,4 +215,4 @@
   "engines": {
     "yarn": ">= 1.0.0"
   }
-}
\ No newline at end of file
+}
diff --git a/resources/web-ext/background.js b/resources/web-ext/background.js
new file mode 100644
index 00000000..b48899c2
--- /dev/null
+++ b/resources/web-ext/background.js
@@ -0,0 +1,49 @@
+/**
+ * Add browser actions, for WebExtension
+ * (e.g. to open Cesium in a tab, when integrated as a Firefox extension).
+ *
+ * See https://forum.duniter.org/t/premiere-version-du-module-cesium-pour-firefox/6944
+ *
+ **/
+var browser, chrome;
+
+browser = browser || chrome;
+
+var browserExtensionRequirements = browser.browserAction && browser.browserAction.onClicked && browser.tabs;
+
+// If integrated as a browser extension
+if (browserExtensionRequirements) {
+
+  /**
+   * Open Cesium in a new browser's tab
+   */
+  function openInTab() {
+    console.debug("[extension] Opening Cesium...")
+    browser.tabs.create({
+      url: "index.html"
+    });
+
+
+  }
+
+  // FIXME: finish this code
+  function checkNotifications() {
+    console.debug("[extension] Checking for notifications...");
+
+    browser.browserAction.setBadgeText({
+      text: '0'
+    });
+    browser.browserAction.setBadgeBackgroundColor({
+      color: '#387EF5' // = $positive color - see the SCSS theme
+    });
+
+    // Loop, after a delay
+    setTimeout(function() {
+      checkNotifications();
+    }, 60 * 1000 /*1min*/);
+  }
+
+  // Adding browser action
+  browser.browserAction.onClicked.addListener(openInTab);
+
+}
diff --git a/resources/web-ext/manifest.json b/resources/web-ext/manifest.json
new file mode 100644
index 00000000..06586f20
--- /dev/null
+++ b/resources/web-ext/manifest.json
@@ -0,0 +1,14 @@
+{
+  "short_name": "Cesium",
+  "name": "Cesium",
+  "manifest_version": 2,
+  "version": "1.0.0",
+  "description": "Manage your Duniter Wallet on a libre currency, like Äž1",
+  "background": {
+    "scripts": ["background.js"]
+  },
+  "browser_action" : {
+    "default_title": "Cesium",
+    "default_icon":  "img/logo_32px.png"
+  }
+}
diff --git a/web-ext-config.js b/web-ext-config.js
new file mode 100644
index 00000000..3894f9a1
--- /dev/null
+++ b/web-ext-config.js
@@ -0,0 +1,4 @@
+module.exports = {
+  "sourceDir": "dist/web/ext/",
+  "artifactsDir": "dist/web/build/"
+};
diff --git a/yarn.lock b/yarn.lock
index c122feb9..aa84c6af 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -244,14 +244,6 @@
   version "0.6.6"
   resolved "https://codeload.github.com/fians/Waves/tar.gz/7cfc479b8246e6d15e660bc2a69fa5b3a792ce98"
 
-"@gulp-sourcemaps/map-sources@1.X":
-  version "1.0.0"
-  resolved "https://registry.yarnpkg.com/@gulp-sourcemaps/map-sources/-/map-sources-1.0.0.tgz#890ae7c5d8c877f6d384860215ace9d7ec945bda"
-  integrity sha1-iQrnxdjId/bThIYCFazp1+yUW9o=
-  dependencies:
-    normalize-path "^2.0.1"
-    through2 "^2.0.3"
-
 "@ionic/cli-framework-prompts@1.0.4":
   version "1.0.4"
   resolved "https://registry.yarnpkg.com/@ionic/cli-framework-prompts/-/cli-framework-prompts-1.0.4.tgz#c1ddc292bd51a0129fdfd2425eaf16666cb83980"
@@ -467,11 +459,6 @@ accepts@~1.3.5, accepts@~1.3.7:
     mime-types "~2.1.24"
     negotiator "0.6.2"
 
-acorn@4.X:
-  version "4.0.13"
-  resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787"
-  integrity sha1-EFSVrlNh1pe9GVyCUZLhrX8lN4c=
-
 acorn@~0.11.0:
   version "0.11.0"
   resolved "https://registry.yarnpkg.com/acorn/-/acorn-0.11.0.tgz#6e95f0253ad161ff0127db32983e5e2e5352d59a"
@@ -1843,7 +1830,7 @@ conventional-changelog@0.0.11:
     event-stream "~3.1.0"
     lodash.assign "~2.4.1"
 
-convert-source-map@1.X, convert-source-map@^1.5.0:
+convert-source-map@^1.5.0:
   version "1.7.0"
   resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
   integrity sha512-4FJkXzKXEDB1snCFZlLP4gpC3JILicCpGbzG9f9G7tGqGCzETQ2hWPrcinA9oU4wtf2biUaEH5065UnMeR33oA==
@@ -2206,16 +2193,6 @@ crypto-random-string@^1.0.0:
   resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
   integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=
 
-css@2.X:
-  version "2.2.4"
-  resolved "https://registry.yarnpkg.com/css/-/css-2.2.4.tgz#c646755c73971f2bba6a601e2cf2fd71b1298929"
-  integrity sha512-oUnjmWpy0niI3x/mPL8dVEI1l7MnG3+HHyRPHf+YFSbK+svOhXpmSOcDURUh2aOCgl2grzrOPt1nHLuCVFULLw==
-  dependencies:
-    inherits "^2.0.3"
-    source-map "^0.6.1"
-    source-map-resolve "^0.5.2"
-    urix "^0.1.0"
-
 csso@^2.0.0:
   version "2.3.2"
   resolved "https://registry.yarnpkg.com/csso/-/csso-2.3.2.tgz#ddd52c587033f49e94b71fc55569f252e8ff5f85"
@@ -2266,16 +2243,7 @@ dateformat@^2.0.0:
   resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-2.2.0.tgz#4065e2013cf9fb916ddfd82efb506ad4c6769062"
   integrity sha1-QGXiATz5+5Ft39gu+1Bq1MZ2kGI=
 
-debug-fabulous@0.0.X:
-  version "0.0.4"
-  resolved "https://registry.yarnpkg.com/debug-fabulous/-/debug-fabulous-0.0.4.tgz#fa071c5d87484685424807421ca4b16b0b1a0763"
-  integrity sha1-+gccXYdIRoVCSAdCHKSxawsaB2M=
-  dependencies:
-    debug "2.X"
-    lazy-debug-legacy "0.0.X"
-    object-assign "4.1.0"
-
-debug@2, debug@2.6.9, debug@2.X, debug@^2.1.0, debug@^2.1.3, debug@^2.2.0, debug@^2.3.3:
+debug@2, debug@2.6.9, debug@^2.1.0, debug@^2.1.3, debug@^2.2.0, debug@^2.3.3:
   version "2.6.9"
   resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
   integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
@@ -2495,11 +2463,6 @@ detect-indent@^5.0.0:
   resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
   integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50=
 
-detect-newline@2.X:
-  version "2.1.0"
-  resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
-  integrity sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I=
-
 diff@^4.0.1:
   version "4.0.1"
   resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
@@ -3782,11 +3745,6 @@ got@^6.7.1:
     unzip-response "^2.0.1"
     url-parse-lax "^1.0.0"
 
-graceful-fs@4.X, graceful-fs@^4.0.0, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.3:
-  version "4.2.3"
-  resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
-  integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==
-
 graceful-fs@^3.0.0:
   version "3.0.12"
   resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-3.0.12.tgz#0034947ce9ed695ec8ab0b854bc919e82b1ffaef"
@@ -3794,6 +3752,11 @@ graceful-fs@^3.0.0:
   dependencies:
     natives "^1.1.3"
 
+graceful-fs@^4.0.0, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.3:
+  version "4.2.3"
+  resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
+  integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==
+
 graceful-fs@~1.2.0:
   version "1.2.3"
   resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-1.2.3.tgz#15a4806a57547cb2d2dbf27f42e89a8c3451b364"
@@ -4146,23 +4109,6 @@ gulp-sass@^4.0.2:
     through2 "^2.0.0"
     vinyl-sourcemaps-apply "^0.2.0"
 
-gulp-sourcemaps@^1.6.0:
-  version "1.12.1"
-  resolved "https://registry.yarnpkg.com/gulp-sourcemaps/-/gulp-sourcemaps-1.12.1.tgz#b437d1f3d980cf26e81184823718ce15ae6597b6"
-  integrity sha1-tDfR89mAzyboEYSCNxjOFa5ll7Y=
-  dependencies:
-    "@gulp-sourcemaps/map-sources" "1.X"
-    acorn "4.X"
-    convert-source-map "1.X"
-    css "2.X"
-    debug-fabulous "0.0.X"
-    detect-newline "2.X"
-    graceful-fs "4.X"
-    source-map "~0.6.0"
-    strip-bom "2.X"
-    through2 "2.X"
-    vinyl "1.X"
-
 gulp-uglify-es@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/gulp-uglify-es/-/gulp-uglify-es-2.0.0.tgz#0a48d9f352393397e78cf2be44bf0fb66a353031"
@@ -5435,11 +5381,6 @@ lazy-cache@^2.0.1:
   dependencies:
     set-getter "^0.1.0"
 
-lazy-debug-legacy@0.0.X:
-  version "0.0.1"
-  resolved "https://registry.yarnpkg.com/lazy-debug-legacy/-/lazy-debug-legacy-0.0.1.tgz#537716c0776e4cf79e3ed1b621f7658c2911b1b1"
-  integrity sha1-U3cWwHduTPeePtG2IfdljCkRsbE=
-
 lazystream@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/lazystream/-/lazystream-1.0.0.tgz#f6995fe0f820392f61396be89462407bb77168e4"
@@ -6582,7 +6523,7 @@ normalize-package-data@^2.0.0, normalize-package-data@^2.3.2, normalize-package-
     semver "2 || 3 || 4 || 5"
     validate-npm-package-license "^3.0.1"
 
-normalize-path@^2.0.1, normalize-path@^2.1.1:
+normalize-path@^2.1.1:
   version "2.1.1"
   resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9"
   integrity sha1-GrKLVW4Zg2Oowab35vogE3/mrtk=
@@ -8348,7 +8289,7 @@ sort-keys@^1.0.0:
   dependencies:
     is-plain-obj "^1.0.0"
 
-source-map-resolve@^0.5.0, source-map-resolve@^0.5.2:
+source-map-resolve@^0.5.0:
   version "0.5.2"
   resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.2.tgz#72e2cc34095543e43b2c62b2c4c10d4a9054f259"
   integrity sha512-MjqsvNwyz1s0k81Goz/9vRBe9SZdB09Bdw+/zYyO+3CuPk6fouTaxscHkgtE8jKvf01kVfl8riHzERQ/kefaSA==
@@ -8706,13 +8647,6 @@ strip-bom-stream@^1.0.0:
     first-chunk-stream "^1.0.0"
     strip-bom "^2.0.0"
 
-strip-bom@2.X, strip-bom@^2.0.0:
-  version "2.0.0"
-  resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e"
-  integrity sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=
-  dependencies:
-    is-utf8 "^0.2.0"
-
 strip-bom@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-1.0.0.tgz#85b8862f3844b5a6d5ec8467a93598173a36f794"
@@ -8721,6 +8655,13 @@ strip-bom@^1.0.0:
     first-chunk-stream "^1.0.0"
     is-utf8 "^0.2.0"
 
+strip-bom@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e"
+  integrity sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=
+  dependencies:
+    is-utf8 "^0.2.0"
+
 strip-bom@^3.0.0:
   version "3.0.0"
   resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
@@ -8905,14 +8846,6 @@ through2@2.0.3:
     readable-stream "^2.1.5"
     xtend "~4.0.1"
 
-through2@2.X, through2@^2.0.0, through2@^2.0.1, through2@^2.0.3, through2@~2.0.0:
-  version "2.0.5"
-  resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"
-  integrity sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==
-  dependencies:
-    readable-stream "~2.3.6"
-    xtend "~4.0.1"
-
 through2@3.0.1, through2@^3.0.0:
   version "3.0.1"
   resolved "https://registry.yarnpkg.com/through2/-/through2-3.0.1.tgz#39276e713c3302edf9e388dd9c812dd3b825bd5a"
@@ -8944,6 +8877,14 @@ through2@^0.6.1, through2@^0.6.3, through2@^0.6.5:
     readable-stream ">=1.0.33-1 <1.1.0-0"
     xtend ">=4.0.0 <4.1.0-0"
 
+through2@^2.0.0, through2@^2.0.1, through2@^2.0.3, through2@~2.0.0:
+  version "2.0.5"
+  resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"
+  integrity sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==
+  dependencies:
+    readable-stream "~2.3.6"
+    xtend "~4.0.1"
+
 through@2, through@^2.3.4, through@^2.3.6, through@^2.3.8, through@~2.3, through@~2.3.1, through@~2.3.4, through@~2.3.8:
   version "2.3.8"
   resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
@@ -9483,15 +9424,6 @@ vinyl-sourcemaps-apply@^0.1.4:
   dependencies:
     source-map "^0.1.39"
 
-vinyl@1.X, vinyl@^1.1.0:
-  version "1.2.0"
-  resolved "https://registry.yarnpkg.com/vinyl/-/vinyl-1.2.0.tgz#5c88036cf565e5df05558bfc911f8656df218884"
-  integrity sha1-XIgDbPVl5d8FVYv8kR+GVt8hiIQ=
-  dependencies:
-    clone "^1.0.0"
-    clone-stats "^0.0.1"
-    replace-ext "0.0.1"
-
 vinyl@^0.2.1, vinyl@~0.2.3:
   version "0.2.3"
   resolved "https://registry.yarnpkg.com/vinyl/-/vinyl-0.2.3.tgz#bca938209582ec5a49ad538a00fa1f125e513252"
@@ -9516,6 +9448,15 @@ vinyl@^0.5.0:
     clone-stats "^0.0.1"
     replace-ext "0.0.1"
 
+vinyl@^1.1.0:
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/vinyl/-/vinyl-1.2.0.tgz#5c88036cf565e5df05558bfc911f8656df218884"
+  integrity sha1-XIgDbPVl5d8FVYv8kR+GVt8hiIQ=
+  dependencies:
+    clone "^1.0.0"
+    clone-stats "^0.0.1"
+    replace-ext "0.0.1"
+
 vinyl@^2.0.0, vinyl@^2.1.0, vinyl@^2.2.0:
   version "2.2.0"
   resolved "https://registry.yarnpkg.com/vinyl/-/vinyl-2.2.0.tgz#d85b07da96e458d25b2ffe19fece9f2caa13ed86"
-- 
GitLab