Skip to content

Instantly share code, notes, and snippets.

@derjanb
Last active February 14, 2026 16:29
Show Gist options
  • Select an option

  • Save derjanb/9f6c10168e63c3dc3cf0 to your computer and use it in GitHub Desktop.

Select an option

Save derjanb/9f6c10168e63c3dc3cf0 to your computer and use it in GitHub Desktop.
#!/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)
@ueffel

ueffel commented Aug 18, 2022

Copy link
Copy Markdown

you need the whole directory not just a single file.

@iamqiz

iamqiz commented Aug 18, 2022

Copy link
Copy Markdown

@githubabcdefg as far as i know , window cannot run above script directly, but you can run it in window WSL,
additionly, above script use python2, if you use python3, try this py3 version:
https://gist.github.com/derjanb/9f6c10168e63c3dc3cf0?permalink_comment_id=3400197#gistcomment-3400197 (from Aceralon)

usage:
python extract_tampermonkey_script.py path/to/chrome/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo
if you use WSL in window , the window path is maped to /mnt/ directory,
you can copy the "Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" dir to D:\dhdgffkkebhmkfjojejmpbldmpobfkfo
and run py with: python extract_tampermonkey_script.py /mnt/d/dhdgffkkebhmkfjojejmpbldmpobfkfo
and the output JavaScript file will be placed where the script.py is

@KellyEscobar

Copy link
Copy Markdown

you need the whole directory not just a single file.

Thanks nice person.

@KellyEscobar

Copy link
Copy Markdown

@githubabcdefg as far as i know , window cannot run above script directly, but you can run it in window WSL, additionly, above script use python2, if you use python3, try this py3 version: https://gist.github.com/derjanb/9f6c10168e63c3dc3cf0?permalink_comment_id=3400197#gistcomment-3400197 (from Aceralon)

usage: python extract_tampermonkey_script.py path/to/chrome/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo if you use WSL in window , the window path is maped to /mnt/ directory, you can copy the "Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" dir to D:\dhdgffkkebhmkfjojejmpbldmpobfkfo and run py with: python extract_tampermonkey_script.py /mnt/d/dhdgffkkebhmkfjojejmpbldmpobfkfo and the output JavaScript file will be placed where the script.py is

Since I don't have WSL installed, I'll try it out in VMware, thank you very much for your help.
Thanks again.

@KellyEscobar

KellyEscobar commented Aug 19, 2022

Copy link
Copy Markdown

@githubabcdefg as far as i know , window cannot run above script directly, but you can run it in window WSL, additionly, above script use python2, if you use python3, try this py3 version: https://gist.github.com/derjanb/9f6c10168e63c3dc3cf0?permalink_comment_id=3400197#gistcomment-3400197 (from Aceralon)

usage: python extract_tampermonkey_script.py path/to/chrome/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo if you use WSL in window , the window path is maped to /mnt/ directory, you can copy the "Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo" dir to D:\dhdgffkkebhmkfjojejmpbldmpobfkfo and run py with: python extract_tampermonkey_script.py /mnt/d/dhdgffkkebhmkfjojejmpbldmpobfkfo and the output JavaScript file will be placed where the script.py is

I've tried different versions of Python, and it always seems to be a leveldb error.

1

2

@ueffel

ueffel commented Aug 19, 2022

Copy link
Copy Markdown

you need the leveldb module. you can install it via pip.

$ python3 -m pip install leveldb

@iamqiz

iamqiz commented Aug 19, 2022

Copy link
Copy Markdown

@KellyEscobar here is result from your file:
https://1drv.ms/u/s!Aso5a3Kpi4YwggOXvz9Uj95k_h_B?e=VpMW6v
老哥中国人?哈哈😂

@KellyEscobar

Copy link
Copy Markdown

you need the leveldb module. you can install it via pip.

$ python3 -m pip install leveldb

It's really helped. Thank you very much.

@KellyEscobar

Copy link
Copy Markdown

@KellyEscobar here is result from your file: https://1drv.ms/u/s!Aso5a3Kpi4YwggOXvz9Uj95k_h_B?e=VpMW6v 老哥中国人?哈哈😂

Thanks for your help.

@rakitanc

Copy link
Copy Markdown

@KellyEscobar here is result from your file: https://1drv.ms/u/s!Aso5a3Kpi4YwggOXvz9Uj95k_h_B?e=VpMW6v 老哥中国人?哈哈joy

Hi there hope you are doing well

Need your urgent help. All my userscripts over the years i gathered are gone after the abrupt PC shutdown. Please help me to restore it

Chrome Version 103.0.5060.114 (Official Build) (64-bit)
TM version 4.18.0
Linux OS
Screenshot from 2022-10-19 15-41-54

If anyone can help me in the situation. It would mean a world to me, I am very devastated right now to what just happened suddenly out of nowhere

@iamqiz

iamqiz commented Oct 19, 2022

Copy link
Copy Markdown

@rakitanc pack your extension directory ("/home/<USER>/.config/<BROWSER>/Default/Local Extension Settings/<EXTENSION_ID>" ) , and upload it to web cloud (google/Microsoft) ,share link to me, then i will restore it and send back to you

@rakitanc

rakitanc commented Oct 20, 2022

Copy link
Copy Markdown

@iamqiz Thankyou so much brother. It's rare to find people like you nowadays who are helping this way. Because i was already sad and after not getting any help online I lost my hope actually But so happy to hear from you that finally someone came as a ray of hope

Sp just to update you with a good news..I got my scripts back by simply rebooting the PC. I just tried my luck when i didn't know what to do and Fortunately it worked.

But i would like to be in touch with you as i have one more technical issue that you might help with me. So if you don't mind can you share your Telegram, Discord or anything else so i could share it there

Looking forward to hearing from you

Thanks

@iamqiz

iamqiz commented Oct 20, 2022

Copy link
Copy Markdown

@rakitanc happy to hear you got your script back

@EdFrees

EdFrees commented Nov 4, 2022

Copy link
Copy Markdown

I'm not sure what this is supposed to do. I ran the code, got no errors, but I don't see the extracted scripts anywhere. Could someone post a screenshot of a successful extraction?

@iamqiz

iamqiz commented Nov 5, 2022

Copy link
Copy Markdown

@EdFrees it will print "Writing to xxxx.js" and the "xxxx.js" are extracted scripts,they will be placed at python script directory

@EdFrees

EdFrees commented Nov 5, 2022

Copy link
Copy Markdown

@EdFrees it will print "Writing to xxxx.js" and the "xxxx.js" are extracted scripts,they will be placed at python script directory

Okay. I'm not seeing it Writing anything. There are no js scripts in the folder where the python script is. All it gives is a blank line in the terminal and then I can type again. No errors. There are clearly old Tampermonkey scripts in the folder because when I open some of the files in a text editor I can see parts of the scripts I wrote.

Could the problem be that there are 2 ldb files containing different scripts?

@iamqiz

iamqiz commented Nov 6, 2022

Copy link
Copy Markdown

@EdFrees i guess your tapermonkey dir is not valid leveldb database, the database dir include Manifest-* CURRENT *.ldb(or *.log) files and so on.
details about google leveldb:
https://github.com/google/leveldb/blob/main/doc/impl.md

@EdFrees

EdFrees commented Nov 7, 2022

Copy link
Copy Markdown

@iamqiz I understand. I see the files that are mentioned in the post:
000010.ldb, 000012.ldb, 000013.log, CURRENT, LOCK, LOG, LOG.old, MANIFEST-000011
After I run the script some file names are changed. 000015.log and MANIFEST-000014. The LOG is also adjusted I think.

@EdFrees

EdFrees commented Nov 19, 2022

Copy link
Copy Markdown

Could anyone please help me out trying to extract the scripts?

@optionsx

Copy link
Copy Markdown

Nice! thanks a bunch

@ziqian-L

Copy link
Copy Markdown

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

@iamqiz

iamqiz commented Apr 12, 2023

Copy link
Copy Markdown

@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

ziqian-L commented Apr 12, 2023 via email

Copy link
Copy Markdown

@iamqiz

iamqiz commented Apr 12, 2023

Copy link
Copy Markdown

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

@ziqian-L

ziqian-L commented Apr 12, 2023 via email

Copy link
Copy Markdown

@aleclarson

Copy link
Copy Markdown

Is there something like this for Firefox?

@robinlej

Copy link
Copy Markdown

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

eldepor commented Dec 14, 2024

Copy link
Copy Markdown

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
Copy Markdown

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

guillmz commented Aug 3, 2025

Copy link
Copy Markdown

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!

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