Skip to content

Instantly share code, notes, and snippets.

@harabat
Created January 9, 2023 19:38
Show Gist options
  • Save harabat/7c7f8ce1aafc3eb0ab9794984f069935 to your computer and use it in GitHub Desktop.
Save harabat/7c7f8ce1aafc3eb0ab9794984f069935 to your computer and use it in GitHub Desktop.
import json
import pathlib
from bs4 import BeautifulSoup
# read the file
root = pathlib.Path.cwd()
file = root / "about_config.html"
html_doc = file.read_text().replace("\n", "")
# parse html
html = BeautifulSoup(html_doc, "html.parser")
# locate prefs
prefs_container = html.find(id="prefs-container")
# collect settings
settings = {}
for i in prefs_container.children:
settings[i.attrs["name"]] = {
k: v for k, v in i.attrs.items() if k in ["value", "default"]
}
settings[i.attrs["name"]]["type"] = i.find("input").attrs["type"]
# save non-default settings js file (same format as prefs.js and user.js)
file_js = root / "about_config.js"
user_prefs_js = ""
for k, v in settings.items():
# skip non-default
if v["default"] == "true":
continue
value = v["value"] if v["type"] != "text" else f'"{v["value"]}"'
user_pref = f'user_pref("{k}", {value});\n'
user_prefs_js += user_pref
file_js.write_text(user_prefs_js)
# save all settings as json file
file_json = root / "about_config.json"
with file_json.open("w") as target_json:
json.dump(settings, target_json, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment