Last active
May 6, 2019 12:09
-
-
Save noskla/b93c47e0476ca0954322158bc8fa97ed to your computer and use it in GitHub Desktop.
Simple script that makes installing extensions from Chrome Store to Ungoogled Chromium a bit less of a headache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Ungoogled Chromium webstore extensions install utility | |
# Made by _kana ( http://cyberhangar.org ) | |
import sys, urllib.request, urllib.parse, subprocess, os, re | |
_cfg = { | |
# Download directory (without / on the end). Make sure that you have read, write permissions. | |
# Leave empty to download to script's working directory. | |
'dl_dir': '', | |
# Override the current Chromium version. | |
'check_version': True, | |
'custom_version': '', | |
# Length of the extension id in the Chrome Store URL. Don't change it. | |
'ext_id_length': 32 | |
} | |
FNULL = open(os.devnull, 'w') | |
raw_dl_url = "https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&prodversion={VERSION}&x=id%3D{EXTENSION_ID}%26installsource%3Dondemand%26uc" | |
if _cfg['check_version']: | |
try: | |
command_output = subprocess.check_output("chromium --version", shell=True).rstrip() | |
except subprocess.CalledProcessError: | |
print("Please install Ungoogled Chromium first.") | |
sys.exit() | |
browser_ver = command_output.decode().split()[1] | |
browser_ver_short = browser_ver.split(".", 1)[0] + '.0' | |
else: | |
browser_ver_short = browser_ver = _cfg['custom_version'] | |
print("Detected Chromium version: " + browser_ver) | |
try: | |
if sys.argv[1] in ['-h', '--help']: | |
print("Example use:\n python uchrstex.py https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm?hl=pl") | |
sys.exit() | |
except IndexError: | |
pass | |
# Check if user has given any arguments | |
if not len(sys.argv) > 1: | |
print("Not enough arguments.\nExample:\n python uchrstex.py \"https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm?hl=pl\"") | |
sys.exit() | |
try: | |
ext_store_url = sys.argv[1] | |
ext_id = re.search(r"/([a-z]{" + str(_cfg['ext_id_length']) + "})", ext_store_url).group(0)[1:] | |
print("Found extension id: " + ext_id) | |
except AttributeError: | |
print("Chrome Store URL is not correct.") | |
sys.exit() | |
ready_dl_url = raw_dl_url.format(EXTENSION_ID=ext_id, VERSION=browser_ver) | |
ext_filename = ext_id + '.crx' | |
ext_path = (os.getcwd() if not _cfg['dl_dir'] else _cfg['dl_dir']) + '/' + ext_filename | |
print("Downloading {}...".format(ext_filename)) | |
try: | |
urllib.request.urlretrieve(ready_dl_url, ext_path) | |
except urllib.error.HTTPError as e: | |
if e.status == 404: | |
print("Extension with given URL doesn't exist.") | |
else: | |
print(e) | |
sys.exit() | |
print("Installing extension (Chromium may not unminimize automatically)...") | |
subprocess.call(['chromium', ext_path], stdout=FNULL, stderr=subprocess.STDOUT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment