Ross Wan's World!

Python, Ajax, PHP and Linux.

批量修改 Firefox 扩展的最大版本号

Posted by Ross Wan 于 2011/08/26

本人一直都是 Firefox Nightbuild 版本的”忠实”使用者,因为可以提前使用最新的功能,性能也有很大的提升,还可以帮 Mozilla 测试新版本,提交 Bugs 报告.尤其现在的 Firefox 9, 在内存方面有很的改进,值得期待.

回归正题, Firefox Nightbuild 版有它的好处, 问题是很多扩展未能兼容, 但有一部分扩展是兼容的,只是它支持的最大版本号未修改过来,这样,就需要人工手动修改其扩展(xpi文件)的install.rdf. (当然,除了修改最大版本号,还有很多方法可以让其兼容,网上都有介绍)

下面的 Python 脚本可以达到批量修改xpi文件的目的:

#!bin/python3
# coding=utf8

import re, zipfile, os, sys

maxversion = 100
ff_maxversion_reg = re.compile(br'(ec8030f7-c20a-464f-9b0e-13a3a9e97384.*?em:maxVersion.*?)([^>< ="/]+)', re.S+re.I)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        maxversion = int(sys.argv[1])
    print('Target Maxversion: %s\n' % maxversion)
    sys.exit()

    for filename in os.listdir():
        print('%s ' % filename, end='')
        if os.path.isdir(filename) or not filename.lower().endswith('.xpi'):
            print('skip.')
            continue
        zin = zipfile.ZipFile(filename)
        rdf = zin.read('install.rdf')
        version = 0
        for item in ff_maxversion_reg.finditer(rdf):
            match_obj = re.search(br'\d+', item.groups()[1])
            if match_obj and int(match_obj.group()) > version:
                version = int(match_obj.group())
        if version >= maxversion:
            zin.close()
            print('skip.')
            continue

        zout = zipfile.ZipFile('new.xpi','w')
        rdf = ff_maxversion_reg.sub(br'\g<1>'+ str(maxversion).encode('utf8'), rdf)
        zout.writestr('install.rdf', rdf)
        for item in zin.infolist():
            if item.filename.lower() == 'install.rdf':
                continue
            else:
                buffer = zin.read(item.filename)
                zout.writestr(item, buffer)
        zin.close()
        zout.close()
        os.remove(filename)
        os.rename('new.xpi', filename)
        print('done!')

将脚本放于 Firefox 配置文件夹的 extensions 目录下运行即可.也可以带1个参数运行,用于指定目标最大版本号,如:

python3 xpi_rdf_batch.py 200

值得注意的是,目前它仅可以修改 xpi 文件, 对于 unpack 的扩展不能修改.

Have fun :)

Advertisements

7条回应 to “批量修改 Firefox 扩展的最大版本号”

  1. Hello! I’ve been reading your weblog for some time now and finally got the courage to go ahead and give you a shout out from Austin Texas! Just wanted to say keep up the fantastic work!

  2. You actually make it seem so easy with your presentation
    but I find this matter to be actually something which I think I would never
    understand. It seems too complicated and very broad for me.
    I am looking forward for your next post, I’ll try to get the hang of it!

  3. That is really interesting, You’re a very skilled blogger. I have joined your feed and sit up for searching for more of your excellent post. Also, I’ve shared your web site in my social networks

  4. I’ve read some excellent stuff here. Certainly price bookmarking for revisiting. I wonder how much effort you place to make this kind of wonderful informative site.

  5. Tomas said

    Pretty! This has been an incredibly wonderful post. Many thanks
    for supplying these details.

  6. I think the admin of this website is genuinely working hard in support of his web site, since
    here every information is quality based stuff.

  7. Hello everyone, it’s my first visit at this
    site, and post is truly fruitful in support of me,
    keep up posting such posts.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

 
%d 博主赞过: