Last active
March 10, 2025 12:01
-
-
Save abjugard/9f32ec422a78cf7b67275d99ec181e6f to your computer and use it in GitHub Desktop.
Convert Chrome/Brave search engines to Firefox/Zen
This file contains hidden or 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/env python | |
import sqlite3, os, uuid, json, lz4.block | |
DB_PATH = os.path.expanduser('~/Library/Application Support/BraveSoftware/Brave-Browser/Default/Web Data') | |
MOZILLA_PATH = os.path.expanduser('~/Library/Application Support/zen/Profiles/<profile id>.Default (release)/search.json.mozlz4') | |
NAMESPACE_GUID = uuid.UUID('00000000-0000-0000-0000-0000deadbeef') | |
def read_mozlz4(): | |
with open(MOZILLA_PATH, 'rb') as f: | |
f.read(8) # skip stupid b'mozLz40\0' header | |
compressed_data = f.read() | |
decompressed_data = lz4.block.decompress(compressed_data) | |
json_data = json.loads(decompressed_data) | |
return json_data | |
def write_mozlz4(data): | |
json_data = json.dumps(data) | |
compressed_data = lz4.block.compress(json_data.encode('utf-8')) | |
with open(MOZILLA_PATH, 'wb') as f: | |
f.write(b'mozLz40\0' + compressed_data) | |
def extract_chrome_keywords(): | |
with sqlite3.connect(database=DB_PATH) as s: | |
return list(s.execute(''' | |
select keyword, short_name, url | |
from keywords | |
where usage_count > 0 | |
order by short_name asc | |
''')) | |
def update_firefox_db(moz_data, chrome_keywords): | |
idx = 1337 | |
chrome_engines = [] | |
to_clean = [] | |
for entry in chrome_keywords: | |
if 'google:baseurl' in entry[2].lower(): | |
continue | |
engine_id = str(uuid.uuid5(NAMESPACE_GUID, entry[0])) | |
to_clean.append(engine_id) | |
chrome_engines.append({ | |
'id': engine_id, | |
'_name': entry[1], # short_name | |
'_loadPath': '[user]', | |
'_metaData': { | |
'order': idx | |
}, | |
'_urls': [ | |
{ | |
'params': [], | |
'rels': [], | |
'template': entry[2] # url | |
} | |
], | |
'_definedAliases': [ | |
entry[0] # keyword | |
] | |
}) | |
idx += 1 | |
engines = moz_data['engines'] | |
for idx_del in to_clean: | |
for idx, engine in enumerate(engines): | |
if engine['id'] == idx_del: | |
del engines[idx] | |
engines += chrome_engines | |
def main(): | |
moz_data = read_mozlz4() | |
chrome_keywords = extract_chrome_keywords() | |
update_firefox_db(moz_data, chrome_keywords) | |
write_mozlz4(moz_data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment