[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [pluggable-transports/snowflake-webext] 03/08: fix: don't include 'background' permission in Gecko (Firefox) build



This is an automated email from the git hooks/post-receive script.

shelikhoo pushed a commit to branch main
in repository pluggable-transports/snowflake-webext.

commit d24e6bb6dbc74a2eb590dfab327cc61006d89c9d
Author: WofWca <wofwca@xxxxxxxxxxxxxx>
AuthorDate: Thu Aug 18 16:20:05 2022 +0300

    fix: don't include 'background' permission in Gecko (Firefox) build
---
 README.md                                    | 26 +++++++++++-----
 make.js                                      | 45 ++++++++++++++++++++++++----
 static/popup.js                              |  7 +++--
 webext/{manifest.json => manifest_base.json} |  3 --
 4 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index d6a508a..0bd6461 100644
--- a/README.md
+++ b/README.md
@@ -20,10 +20,22 @@ which outputs to the `build/` directory.
 
 ### Building the webextension
 
-```
-npm install
-npm run webext
-```
+1.
+    ```
+    npm install
+    ```
+2.
+    * For Gecko (e.g. Firefox):
+
+        ```bash
+        npm run webext gecko
+        ```
+
+    * For Chromium (e.g. Chrome, Edge)
+
+        ```bash
+        npm run webext chromium
+        ```
 
 and then load the `build-webext/` directory as an unpacked extension.
  * https://developer.mozilla.org/en-US/docs/Tools/about:debugging#Loading_a_temporary_extension
@@ -122,9 +134,9 @@ npm run pack-webext x.y.z
 git push origin master
 git push origin --tags
 
-# Upload the generated build-webext.zip (and source.zip) to the webextension stores,
-# 1. https://addons.mozilla.org/en-US/developers/addon/torproject-snowflake/versions/submit/
-# 2. https://chrome.google.com/webstore/devconsole/
+# Upload the generated build-webext-chromium.zip, build-webext-gecko.zip (and source.zip) to the webextension stores, respectively,
+# 1. https://chrome.google.com/webstore/devconsole/
+# 2. https://addons.mozilla.org/en-US/developers/addon/torproject-snowflake/versions/submit/
 
 # This time, really clean, because we don't want any extraneous files uploaded
 git clean -f -d -x
diff --git a/make.js b/make.js
index d3a4af1..63e4cb6 100755
--- a/make.js
+++ b/make.js
@@ -163,14 +163,43 @@ task('build', 'build the snowflake proxy', function() {
   console.log('Snowflake prepared.');
 });
 
-task('webext', 'build the webextension', function() {
+const browserEngines = ['chromium', 'gecko'];
+function buildWebext(browserEngine) {
+  const definitions = {
+    // Gecko currently doesn't support it:
+    // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions#browser_compatibility
+    SUPPORTS_WEBEXT_OPTIONAL_BACKGROUND_PERMISSION: browserEngine === 'chromium',
+  };
   const outDir = 'build-webext';
   execSync(`rm -rf ${outDir} && mkdir ${outDir}`);
   execSync(`cp -r webext/. ${outDir}/`);
   execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' });
+  for (const [key, value] of Object.entries(definitions)) {
+    execSync(`sed -i "s/${key}/${value}/g" ${outDir}/popup.js`);
+  }
+  {
+    const manfestBasePath = `${outDir}/manifest_base.json`;
+    const manifest = JSON.parse(readFileSync(manfestBasePath, 'utf-8'));
+    if (definitions.SUPPORTS_WEBEXT_OPTIONAL_BACKGROUND_PERMISSION) {
+      manifest.optional_permissions = ['background'];
+    }
+    writeFileSync(
+      `${outDir}/manifest.json`,
+      JSON.stringify(manifest, undefined, '  '),
+      'utf-8'
+    );
+    execSync(`rm ${manfestBasePath}`);
+  }
   copyTranslations(outDir);
   concatJS(outDir, 'webext', 'snowflake.js', '');
   console.log('Webextension prepared.');
+}
+task('webext', 'build the webextension', function() {
+  const browserEngine = process.argv[3];
+  if (!(browserEngines.includes(browserEngine))) {
+    throw new Error(`You must provide browser engine string: ${browserEngines.join('|')}`);
+  }
+  buildWebext(browserEngine);
 });
 
 task('node', 'build the node binary', function() {
@@ -188,7 +217,9 @@ var updateVersion = function(file, version) {
 task('pack-webext', 'pack the webextension for deployment', function() {
   try {
     execSync(`rm -f source.zip`);
-    execSync(`rm -f build-webext.zip`);
+    for (const browserEngine of browserEngines) {
+      execSync(`rm -f build-webext-${browserEngine}.zip`);
+    }
   } catch (error) {
     //Usually this happens because the zip files were removed previously
     console.log('Error removing zip files');
@@ -197,7 +228,7 @@ task('pack-webext', 'pack the webextension for deployment', function() {
   var version = process.argv[3];
   console.log(version);
   updateVersion('./package.json', version);
-  updateVersion('./webext/manifest.json', version);
+  updateVersion('./webext/manifest_base.json', version);
   execSync(`git commit -am "bump version to ${version}"`);
   try {
     execSync(`git tag webext-${version}`);
@@ -205,13 +236,15 @@ task('pack-webext', 'pack the webextension for deployment', function() {
     console.log('Error creating git tag');
     // Revert changes
     execSync(`git reset HEAD~`);
-    execSync(`git checkout ./webext/manifest.json`);
+    execSync(`git checkout ./webext/manifest_base.json`);
     execSync(`git submodule update`);
     return;
   }
   execSync(`git archive -o source.zip HEAD .`);
-  execSync(`npm run webext`);
-  execSync(`cd build-webext && zip -Xr ../build-webext.zip ./*`);
+  for (const browserEngine of browserEngines) {
+    execSync(`npm run webext ${browserEngine}`);
+    execSync(`cd build-webext && zip -Xr ../build-webext-${browserEngine}.zip ./*`);
+  }
 });
 
 task('clean', 'remove all built files', function() {
diff --git a/static/popup.js b/static/popup.js
index 2625201..fc4c0e7 100644
--- a/static/popup.js
+++ b/static/popup.js
@@ -26,8 +26,11 @@ class Popup {
     this.statusdesc = document.getElementById('statusdesc');
     this.img = document.getElementById('statusimg');
     this.button = document.querySelector('.button');
-    // if (SUPPORTS_WEBEXT_OPTIONAL_BACKGROUND_PERMISSION)
-    {
+    if (
+      typeof SUPPORTS_WEBEXT_OPTIONAL_BACKGROUND_PERMISSION !== 'undefined'
+      // eslint-disable-next-line no-undef
+      && SUPPORTS_WEBEXT_OPTIONAL_BACKGROUND_PERMISSION
+    ) {
       /** @type {HTMLInputElement} */
       const runInBackgroundInput = document.getElementById('run-in-background');
       document.getElementById('run-in-background-wrapper').classList.remove('display-none');
diff --git a/webext/manifest.json b/webext/manifest_base.json
similarity index 91%
rename from webext/manifest.json
rename to webext/manifest_base.json
index 2c6b032..6de42ab 100644
--- a/webext/manifest.json
+++ b/webext/manifest_base.json
@@ -24,8 +24,5 @@
   },
   "permissions": [
     "storage"
-  ],
-  "optional_permissions": [
-    "background"
   ]
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits