user.js compare tool (python)

Date: 2021-04-10 (last update: 2022-03-13)

Description: A python application to compare multiple user.js files.

The user.js of Firefox allows to configure your browser to your needs. There are different examples out there, but to manually compare them with each other takes time. This python application allows to print out a summary about the differences of two user.js files as input.

userjscompare.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import re
import dictdiffer
from tabulate import tabulate

file1 = "user.js"
file2 = "user.ark.js"


def create_dict(file):

    dic = {}

    with open(file) as f:
        content = f.readlines()
    content = [x.strip() for x in content]

    for line in content:
        if line.startswith("user_pref"):
            results = re.match('user_pref\(\"(.+)\",\s*(.+)\s*\);', line)
            setting_name = results[1]
            setting_value = results[2]
            dic[setting_name] = setting_value
    return dic


def main():
    dic1 = create_dict(file1)
    dic2 = create_dict(file2)

    currlist = []
    done = False

    print("Differences between {} and {}:".format(file1, file2))

    for diff in list(dictdiffer.diff(dic1, dic2)):

        if diff[0] == "change":
            currlist.append([diff[1][0], diff[2][0], diff[2][1]])

        if not done and diff[0] == "add":
            done = True
            print(tabulate(currlist, headers=['Name', file1, file2],
                           tablefmt='orgtbl'))

        if diff[0] == "add":
            print("Only in {}".format(file1))
            print(tabulate(diff[1], headers=['Name', 'Setting'],
                           tablefmt='orgtbl'))

            print("Only in {}".format(file2))
            print(tabulate(diff[2], headers=['Name', 'Setting'],
                           tablefmt='orgtbl'))

        if diff[0] == "remove":
            print("Removed in {}".format(file1))
            print(tabulate(diff[1], headers=['Name', 'Setting'],
                           tablefmt='orgtbl'))

            print("Removed in {}".format(file2))
            print(tabulate(diff[2], headers=['Name', 'Setting'],
                           tablefmt='orgtbl'))


if __name__ == '__main__':
    main()