Skip to content

Instantly share code, notes, and snippets.

@derjanb
Last active August 4, 2025 07:11
#!/usr/bin/env python
# Linux usage: ./extract_tampermonkey_script.py "/home/<USER>/.config/<BROWSER>/Default/Local Extension Settings/<EXTENSION_ID>"
# i.e.: ./extract_tampermonkey_script.py "/home/foo/.config/google-chrome-beta/Default/Local Extension Settings/gcalenpjmijncebpfijmoaglllgpjagf"
# Mac usage: ./extract_tampermonkey_script.py "/Users/<USER>/Library/Application Support/Google/Chrome/Default/Local Extension Settings/<EXTENSION_ID>/"
# i.e.: ./extract_tampermonkey_script.py "/Users/foo/Library/Application Support/Google/Chrome/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo/"
import leveldb
import sys
import re
import json
import codecs
pattern = re.compile("^@source(.*)$")
db = leveldb.LevelDB(sys.argv[1:][0])
for k,v in db.RangeIter():
m = pattern.match(k)
if m:
name = re.sub("[\W\b]", "_", m.groups()[0].strip())
full_name = "%s.user.js" % name
print "Writing to %s" % full_name
content = json.JSONDecoder(encoding='UTF-8').decode(v)['value']
with codecs.open(full_name, 'w', 'utf-8') as text_file:
text_file.write(content)
@optionsx
Copy link

Nice! thanks a bunch

@ziqian-L
Copy link

@iamqiz 佬,我把位于C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default\Extensions里的油猴插件拖出来后,油猴脚本消失了,这种情况可以恢复脚本吗?

@iamqiz
Copy link

iamqiz commented Apr 12, 2023

@ziqian-L 油猴数据位于 "C:\Users<USERNAME>\AppData\Local\Microsoft\Edge\User Data\Default\Local Extension Settings\<EXTENSION_ID> 下, 如果有CURRENT *.ldb 等文件,那么可以恢复, 我的仓库可以在线恢复,不需要自己搭python环境 ,见: https://github.com/iamqiz/github-action-for-tapermonkey

@ziqian-L
Copy link

ziqian-L commented Apr 12, 2023 via email

@iamqiz
Copy link

iamqiz commented Apr 12, 2023

@ziqian-L 😂 以后定期做好备份吧 ,设置>实用工具>压缩包|文件> 导出

@ziqian-L
Copy link

ziqian-L commented Apr 12, 2023 via email

@aleclarson
Copy link

Is there something like this for Firefox?

@robinlej
Copy link

I had to rewrite it slightly for it to work because values are bytearray, not strings.
I also used python3 which AFAIK is the default on most distros now:

#!/usr/bin/env python3

# Linux usage: ./extract_tampermonkey_script.py "/home/<USER>/.config/<BROWSER>/Default/Local Extension Settings/<EXTENSION_ID>"
#        i.e.: ./extract_tampermonkey_script.py "/home/foo/.config/google-chrome-beta/Default/Local Extension Settings/gcalenpjmijncebpfijmoaglllgpjagf"
#   Mac usage: ./extract_tampermonkey_script.py "/Users/<USER>/Library/Application Support/Google/Chrome/Default/Local Extension Settings/<EXTENSION_ID>/"
#        i.e.: ./extract_tampermonkey_script.py "/Users/foo/Library/Application Support/Google/Chrome/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo/"

import leveldb
import sys
import re
import json
import codecs

pattern = re.compile(b"^@source(.*)$")

db = leveldb.LevelDB(sys.argv[1:][0])

for k,v in db.RangeIter():
    k = k.decode('utf-8')
    m = pattern.match(k)
    if m:
        v = v.decode('utf-8')
        name = re.sub("[\W\b]", "_", m.groups()[0].strip())
        full_name = "%s.user.js" % name

        print("Writing to %s" % full_name)

        content = json.JSONDecoder().decode(v)['value']

        with codecs.open(full_name, 'w', 'utf-8') as text_file:
            text_file.write(content)

@eldepor
Copy link

eldepor commented Dec 14, 2024

for those who cant install that leveldb crap. Install plyvel and try this script chatgpt adapted for me (tested on python 3.8 windows7)

import plyvel
import sys
import re
import json
import codecs

pattern = re.compile(b"^@source(.*)$")

db_path = sys.argv[1]
db = plyvel.DB(db_path, create_if_missing=False)

for k, v in db:
    m = pattern.match(k)
    if m:
        k = k.decode('utf-8')
        v = v.decode('utf-8')

        group_decoded = m.groups()[0].decode('utf-8')
        name = re.sub("[\\W\\b]", "_", group_decoded.strip())
        full_name = f"{name}.user.js"

        print(f"Writing to {full_name}")

        content = json.JSONDecoder().decode(v)['value']

        with codecs.open(full_name, 'w', 'utf-8') as text_file:
            text_file.write(content)

db.close()

@KulaGGin
Copy link

For those who just want to copy tampermonkey scripts from one Chrome browser to another, you don't need scripts to just copy the scripts to another browser.
Copy folder C:\Users\YourUserName\AppData\Local\Vivaldi\User Data\Default\Local Extension Settings\dhdgffkkebhmkfjojejmpbldmpobfkfo. If the extension id changes when you will want to do it, look it up in the extension list.

@guillmz
Copy link

guillmz commented Aug 3, 2025

Hi @iamqiz, is it possible that you could help me recover my scripts?

I had an issue with Windows that forced me to reinstall Chrome, and unfortunately, after reinstalling, all my Tampermonkey scripts were lost. I had been collecting those scripts for years, and I would really appreciate any help you can provide to recover them.

Thanks in advance!

@KulaGGin
Copy link

KulaGGin commented Aug 4, 2025

@guillmz read my comment above. If you didn't back up the folder AppData\Local\Chrome, then you can't recover them, because your chrome installation probably overrode the folder I mentioned above, especially if you installed tampermonkey again and those scripts are not there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment