Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiments with extension manifest v3 for Chromium #984

Merged
merged 46 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0bd1b3f
First attempt at manifest v3 extension
Jaifroid Apr 16, 2023
d153e7f
Get as far as loading the app
Jaifroid Apr 16, 2023
c6cb9a2
Switch to using Service Worker as background script
Jaifroid Apr 16, 2023
6c7356b
Undo some unnecessary changes
Jaifroid Apr 16, 2023
61a8d68
Fix libzim loading error in extension
Jaifroid Apr 16, 2023
2447149
Fix codefactor and error in PWA
Jaifroid Apr 16, 2023
88c5c58
Add ASM and WASM v0.2
Jaifroid Apr 17, 2023
4cdad07
Fix typo
Jaifroid Apr 22, 2023
3b19ba6
Experiments in iframe messaging
Jaifroid Apr 23, 2023
a7fba20
Try Shadow DOM
Jaifroid Apr 23, 2023
66fec2a
Attempt to use base href
Jaifroid Apr 23, 2023
82b79fd
Revert "Attempt to use base href"
Jaifroid Apr 23, 2023
502a31d
Revert "Try Shadow DOM"
Jaifroid Apr 23, 2023
10c544f
Revert "Experiments in iframe messaging"
Jaifroid Apr 23, 2023
55a172a
Experiment with using PWA with Chromium extension
Jaifroid Apr 23, 2023
1cd51bb
Allow web accessible resource
Jaifroid Apr 23, 2023
a0bc4dd
Add ServiceWorkerLocal mode
Jaifroid Apr 23, 2023
efca2ba
Add fallback to oldInjectionMode
Jaifroid Apr 23, 2023
fa6adf2
Tweak descriptions
Jaifroid May 4, 2023
0254db5
Cleanup after rebase
Jaifroid May 7, 2023
731ea72
Fix infinite loop with the bypass app cache option when returning fro…
Jaifroid May 7, 2023
d1acc03
Merge branch 'main' into Extension-manifest-v3
Jaifroid May 7, 2023
2638668
Merge branch 'main' into Extension-manifest-v3
Jaifroid May 12, 2023
0d54c82
Merge branch 'main' into Extension-manifest-v3
Jaifroid May 22, 2023
2f53b01
Merge branch 'main' into Extension-manifest-v3
Jaifroid Jun 12, 2023
996cd84
Merge branch 'main' into Extension-manifest-v3
Jaifroid Jun 12, 2023
04939bd
Merge branch 'main' into Extension-manifest-v3
Jaifroid Jun 13, 2023
bf8386d
[CodeFactor] Apply fixes
code-factor Jun 13, 2023
f513169
Fix ESLint errors
Jaifroid Jun 13, 2023
14aadf4
Merge branch 'main' into Extension-manifest-v3
Jaifroid Jun 13, 2023
35fecbd
Merge branch 'Extension-manifest-v3' of https://github.com/kiwix/kiwi…
Jaifroid Jun 13, 2023
307af18
Keep old manifest v2 for backward compatibility
Jaifroid Jun 13, 2023
15a6659
Update instructions for testing the extension
Jaifroid Jun 13, 2023
add433e
Fix unused expressions
Jaifroid Jun 13, 2023
e80c684
Make clear that backgroundscript.js is for MV2
Jaifroid Jun 13, 2023
da91f0c
Fix MV2 syntax
Jaifroid Jun 13, 2023
680a434
Fix Service Worker blockers
Jaifroid Jun 13, 2023
7043728
Fix typo
Jaifroid Jun 13, 2023
4fc2e8d
Build both MV2 and MV3 for Chromium extension
Jaifroid Jun 13, 2023
bc23295
Fix signing both extension versions
Jaifroid Jun 13, 2023
6b9775f
Update backgroundscript.js
Jaifroid Jun 13, 2023
fe8ec64
Clearer descriptions
Jaifroid Jun 13, 2023
e98ace0
Fixes for IE11
Jaifroid Jun 13, 2023
3e1dec9
Do not warn about object-shorthand
Jaifroid Jun 13, 2023
33f8b4d
Fix blocking ESLint in uiUtil.js
Jaifroid Jun 13, 2023
089440a
Update .eslintrc.cjs
Jaifroid Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
'no-extra-parens': 1,
'no-unused-expressions': 1,
'no-unused-vars': 1,
'n/no-callback-literal': 0
'n/no-callback-literal': 0,
'object-shorthand': 0,
'multiline-ternary': 0
}
}
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Please follow these guidelines when contributing:
- be sure to test your fix in both "JQuery" mode and "Service Worker" mode (see Configuration);
- run the Unit tests (see below) in at least the above browsers.

If all the tests are working fine, you can finally test the extension versions, like this:
If all the tests are working fine, you can finally test the extension versions. Plese note that we are using Manifest V3 for the Chromium extensions, and Manifest V2
for the Firefox extension, so there are different instructions for the two browser families:

- Remove the '-WIP' from the version key from the manifest.json file present in the root of this repo;
- In Chromium, you can install the extension by loading the root folder with Extensions -> Load Unpacked (with Developer Mode turned ON) -> select the root folder of the repository;
- In Firefox, you can load an extension with Manage Your Extensions -> Debug Add-ons -> Load Temporary Add-on, and then pick any file in the repository.
- In Firefox, you need to rename manifest.json to manifest.v3.json, and then rename manifest.v2.json to manifest.json. Then you can load the extension with Manage Your Extensions -> Debug Add-ons -> Load Temporary Add-on, and then pick any file in the repository.

If your feature works and tests are passing, make a PR, describe the testing you have done, and ask for a code review.

Expand Down
18 changes: 8 additions & 10 deletions webextension/backgroundscript.js → backgroundscript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* backgroundscript.js: Background script for the WebExtension
* backgroundscript.js: Background script for the WebExtension Manifest V2
*
* Copyright 2017 Mossroy and contributors
* License GPL v3:
Expand All @@ -20,22 +20,20 @@
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/

/* global chrome, browser */

// In order to work on both Firefox and Chromium/Chrome (and derivatives).
// browser and chrome variables expose almost the same APIs
var genericBrowser;
if (typeof browser !== 'undefined') {
// Firefox
genericBrowser = browser;
}
else {
} else {
// Chromium/Chrome
genericBrowser = chrome;
}

genericBrowser.browserAction.onClicked.addListener(handleClick);

function handleClick(event) {
genericBrowser.tabs.create({
url: genericBrowser.runtime.getURL('/www/index.html')
});
}
genericBrowser.browserAction.onClicked.addListener(function () {
var newURL = chrome.runtime.getURL('www/index.html');
chrome.tabs.create({ url: newURL });
});
30 changes: 16 additions & 14 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Kiwix",
"version": "3.8.1",

"description": "Kiwix : offline Wikipedia reader",
"description": "Kiwix Offline Browser",

"icons": {
"16": "www/img/icons/kiwix-16.png",
Expand All @@ -16,7 +16,7 @@
"128": "www/img/icons/kiwix-128.png"
},

"browser_action": {
"action": {
"default_icon": {
"16": "www/img/icons/kiwix-16.png",
"19": "www/img/icons/kiwix-19.png",
Expand All @@ -26,22 +26,24 @@
},
"default_title": "Kiwix"
},

"applications": {
"gecko": {
"id": "[email protected]"
}
},

"web_accessible_resources": ["www/index.html"],

"background": {
"scripts": ["webextension/backgroundscript.js"]
"service_worker": "service-worker.js"
},

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": ["storage", "activeTab", "scripting"],

"author": "mossroy",
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';",
"sandbox": "sandbox allow-scripts allow-downloads allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self';"
},

"web_accessible_resources": [{
"resources": ["www/index.html", "www/article.html"],
"matches": ["https://*.kiwix.org/*"]
}],

"author": "Kiwix",
"homepage_url": "https://www.kiwix.org",
"offline_enabled": true
}
47 changes: 47 additions & 0 deletions manifest.v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"manifest_version": 2,
"name": "Kiwix",
"version": "3.8.1",

"description": "Kiwix : offline Wikipedia reader",

"icons": {
"16": "www/img/icons/kiwix-16.png",
"19": "www/img/icons/kiwix-19.png",
"32": "www/img/icons/kiwix-32.png",
"38": "www/img/icons/kiwix-38.png",
"48": "www/img/icons/kiwix-48.png",
"64": "www/img/icons/kiwix-64.png",
"90": "www/img/icons/kiwix-90.png",
"128": "www/img/icons/kiwix-128.png"
},

"browser_action": {
"default_icon": {
"16": "www/img/icons/kiwix-16.png",
"19": "www/img/icons/kiwix-19.png",
"32": "www/img/icons/kiwix-32.png",
"38": "www/img/icons/kiwix-38.png",
"64": "www/img/icons/kiwix-64.png"
},
"default_title": "Kiwix"
},

"applications": {
"gecko": {
"id": "[email protected]"
}
},

"web_accessible_resources": ["www/index.html"],

"background": {
"scripts": ["backgroundscript.js"]
},

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",

"author": "mossroy",
"homepage_url": "https://www.kiwix.org",
"offline_enabled": true
}
21 changes: 16 additions & 5 deletions scripts/create_all_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fi
# Copy only the necessary files in a temporary directory
mkdir -p tmp
rm -rf tmp/*
cp -r www webextension manifest.json manifest.webapp LICENSE-GPLv3.txt service-worker.js README.md tmp/
cp -r www manifest.json manifest.v2.json manifest.webapp LICENSE-GPLv3.txt service-worker.js README.md tmp/
# Remove unwanted files
rm -f tmp/www/js/lib/libzim-*dev.*

Expand All @@ -64,26 +64,37 @@ rm -f tmp/www/js/lib/libzim-*dev.*
regexpNumericVersion='^[0-9\.]+$'
if [[ $VERSION =~ $regexpNumericVersion ]] ; then
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/manifest.json
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/manifest.v2.json
else
sed -i -e "s/$VERSION_TO_REPLACE/$MAJOR_NUMERIC_VERSION/" tmp/manifest.json
sed -i -e "s/$VERSION_TO_REPLACE/$MAJOR_NUMERIC_VERSION/" tmp/manifest.v2.json
fi
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/manifest.webapp
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/service-worker.js
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/www/js/app.js

mkdir -p build
rm -rf build/*
# Package for Chromium/Chrome
scripts/package_chrome_extension.sh $DRYRUN $TAG -v $VERSION
# Package for Chromium/Chrome with Manifest V3
scripts/package_chrome_extension.sh -m 3 $DRYRUN $TAG -v $VERSION
# Package for Chromium/Chrome with Manifest V2
cp backgroundscript.js tmp/
rm tmp/manifest.json
mv tmp/manifest.v2.json tmp/manifest.json
scripts/package_chrome_extension.sh -m 2 $DRYRUN $TAG -v $VERSION

# Package for Firefox and Firefox OS
# We have to put a unique version string inside the manifest.json (which Chrome might not have accepted)
# So we take the original manifest again, and replace the version inside it again
cp manifest.json tmp/
# So we take the original manifest v2 again, and replace the version inside it again
cp manifest.v2.json tmp/manifest.json
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION_FOR_MOZILLA_MANIFEST/" tmp/manifest.json
echo ""
scripts/package_firefox_extension.sh $DRYRUN $TAG -v $VERSION
echo ""
scripts/package_firefoxos_app.sh $DRYRUN $TAG -v $VERSION
cp -f ubuntu_touch/* tmp/
sed -i -e "s/$VERSION_TO_REPLACE/$VERSION/" tmp/manifest.json
echo ""
scripts/package_ubuntu_touch_app.sh $DRYRUN $TAG -v $VERSION

# Change permissions on source files to match those expected by the server
Expand Down
13 changes: 9 additions & 4 deletions scripts/package_chrome_extension.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
cd "$BASEDIR"

# Reading arguments
while getopts tdv: option; do
while getopts m:tdv: option; do
case "${option}" in
m) MV=$OPTARG;; # Optionally indicates the manifest version we're using (2 or 3); if present, the version will be added to filename
t) TAG="-t";; # Indicates that we're releasing a public version from a tag
d) DRYRUN="-d";; # Indicates a dryrun test, that does not modify anything on the network
v) VERSION=${OPTARG};;
esac
done

if [ -n $MV ]; then
echo -e "\nManifest version requested: $MV"
VERSION="MV$MV-$VERSION"
fi
echo "Packaging unsigned Chrome extension, version $VERSION"
cd tmp
zip -r ../build/kiwix-chrome-unsigned-extension-$VERSION.zip www webextension manifest.json LICENSE-GPLv3.txt service-worker.js README.md
zip -r ../build/kiwix-chrome-unsigned-extension-$VERSION.zip www manifest.json LICENSE-GPLv3.txt service-worker.js README.md
cd ..
if [ "${TAG}zz" == "zz" ]; then
if [ -z $TAG ]; then
# Package the extension with Chrome or Chromium, if we're not packaging a public version
if hash chromium-browser 2>/dev/null
then
Expand All @@ -28,6 +32,7 @@ if [ "${TAG}zz" == "zz" ]; then
echo "Signing the extension for $CHROME_BIN, version $VERSION"
$CHROME_BIN --no-sandbox --pack-extension=tmp --pack-extension-key=./scripts/kiwix-html5.pem
mv tmp.crx build/kiwix-chrome-signed-extension-$VERSION.crx
ls -l build/kiwix-chrome-signed-extension-$VERSION.crx
else
echo "This unsigned extension must be manually uploaded to Google to be signed and distributed from their store"
fi
Loading