Skip to content

Instantly share code, notes, and snippets.

@corbindavenport
Last active August 13, 2021 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corbindavenport/e1de7f9cacceeedc5f608b93a013c396 to your computer and use it in GitHub Desktop.
Save corbindavenport/e1de7f9cacceeedc5f608b93a013c396 to your computer and use it in GitHub Desktop.
Test local WebExtension in Firefox for Android
: '
This is a script for easier testing of a WebExtension on Firefox for Android. It is a wrapper around Mozilla web-ext tool, without the need to check the list of ADB devices manually. It also automates using a Firefox-specific manifest, because Firefox for Android requires "browser_specific_settings" to be included in the manifest with the extension ID, but that string sometimes causes issues in Chromium-based browsers.
Assumed folder structure:
./test-android.sh (this script!)
./manifest.json
./manifest-firefox.json
More info about web-ext: https://extensionworkshop.com/documentation/develop/getting-started-with-web-ext/
'
echo "Note: Your Android device should already be connected over ADB with Firefox installed."
# Check for required tools
if ! [ -x "$(command -v web-ext)" ]; then
echo "Install web-ext first: sudo npm install --global web-ext"
exit
fi
if ! [ -x "$(command -v adb)" ]; then
echo "Install ADB first: https://git.io/JBuTh"
exit
fi
# Get ADB device ID
DEVICE=$(adb devices -l | grep "model" | awk '{print $1}')
if [ "$DEVICE" = "" ]; then
echo "Device not connected with ADB!"
exit
fi
# Check that Firefox is installed
FIREFOX=$(adb shell pm list packages | grep "org.mozilla.firefox")
PLAYSTORE=$(adb shell pm list packages | grep "com.android.vending")
if [ "$FIREFOX" = "" ]; then
echo "Firefox not installed! Check your device."
if [ "$PLAYSTORE" = "" ]; then
adb shell am start -a android.intent.action.VIEW -d "https://github.com/mozilla-mobile/fenix/releases/latest"
else
adb shell am start -a android.intent.action.VIEW -d "https://play.google.com/store/apps/details?id=org.mozilla.firefox"
fi
exit
fi
# Grant permissions
adb shell pm grant org.mozilla.firefox android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant org.mozilla.firefox android.permission.WRITE_EXTERNAL_STORAGE
# Switch to Firefox manifest
mv ./manifest.json ./manifest-temp.json
mv ./manifest-firefox.json ./manifest.json
# Run the command
web-ext run -t firefox-android --adb-device $DEVICE --firefox-apk org.mozilla.firefox
# Switch manifests back
mv ./manifest.json ./manifest-firefox.json
mv ./manifest-temp.json ./manifest.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment