Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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)
@sp4ce
Copy link

sp4ce commented Oct 5, 2015

Hello,

I forked you script and make it work with 3.11: https://gist.github.com/sp4ce/a649517a429d72ffc442

@derjanb
Copy link
Author

derjanb commented Jan 11, 2016

@sp4ce Merged. Thanks a lot.

@Kerrigan29a
Copy link

Kerrigan29a commented Feb 9, 2016

Hi.

I've added in my fork the usual path in Mac: https://gist.github.com/Kerrigan29a/3ca4177bd2e1042e5c14

Thanks for this useful extension

@Enissay
Copy link

Enissay commented Feb 15, 2017

Ummm, I have deleted my chrome profile by mistake, but I managed to get a previous version of chrome's folder and trying to get my scripts back.

I've installed python for Windows but it doesn't seem to be working, or at least I couldn't make it work if someone can help please ?

@MrTact
Copy link

MrTact commented Apr 11, 2017

Forked & modded for Python3, in case that's more your poison of choice: https://gist.github.com/MrTact/74bb630325032cb562313281890a1af5

@calraith
Copy link

calraith commented May 23, 2018

I couldn't compile leveldb in Windows 10. In case it helps @Enissay or anyone else, I wrote a quick and dirty PowerShell alternative to this Python script. https://gist.github.com/calraith/5ea698d022dfe77d3927e75af7cdea75

@derjanb
Copy link
Author

derjanb commented Jul 19, 2018

@calraith Thank you!

@ImSeaWorld
Copy link

ImSeaWorld commented Nov 21, 2018

Aight. Took me a solid day plus an hour and a half to get it working. After it's all working properly it doesn't spit out my scripts. The only solution and it doesn't work. Good fucking god.

@etaderhold
Copy link

etaderhold commented Jul 18, 2020

Thanks so much for this script! I was so worried I had lost my scripts, but I was able to run this against a Time Machine backup to get them back.

@Aceralon
Copy link

Aceralon commented Aug 1, 2020

For Windows 10 user using python3 they can try wsl. https://gist.github.com/Aceralon/d94a562840b858adc8585d7e44cbaa96

@needforsuv
Copy link

needforsuv commented Oct 6, 2020

https://github.com/happynear/py-leveldb-windows

for windows users. Just rename .ldb to .sst because I guess the fork doesn't work with .ldb yet (at least with 2.7 binaries)

example:
C:\Users\user>C:\Python27\python.exe C:\Users\user\Desktop\extract_tampermonkey_script.py "C:\Users\user\Desktop\dhdgffk
kebhmkfjojejmpbldmpobfkfo"
Writing to _0d456ca5_3601_4638_a417_25b70ba320ca.user.js
Writing to _2185129a_bd9d_4ed6_ae59_5e1b43dd5ecf.user.js
Writing to _5f0393a6_c0da_4069_b646_e505507724c6.user.js
Writing to _623ee855_0804_476a_922f_40b6ed938746.user.js
Writing to _66a053d9_70f0_4dc3_a490_0eb149ce9849.user.js
Writing to _6a5f7258_e74d_4383_8516_714c014c1bc1.user.js
Writing to _a1669f5f_d6f4_4f96_8e05_2c6582d8d21d.user.js
Writing to _a791fe14_381d_48a5_abe5_34c73db716b7.user.js
Writing to _bbdf06d1_0336_4dd1_acc0_9f2086548bcd.user.js
Writing to _bc2a3b39_40a2_40a5_812e_177af8f969b6.user.js
Writing to _e1af85c1_fd7a_40c2_8313_25ce1bb4c1c2.user.js
Writing to _f2e744c8_e237_45ba_ae22_bf63a1f80295.user.js

just

@ipatch
Copy link

ipatch commented Nov 27, 2020

is this suppose to work with python 3.9

@kitsune7
Copy link

kitsune7 commented Mar 30, 2021

@ipatch, @MrTact made a fork of this that I think should work with python 3.9.

@lamchau
Copy link

lamchau commented Apr 13, 2021

Couldn't get the macOS leveldb python package to compile, but wrote a node-based solution for anyone else who gets stuck.

@OlehSuvorovSvitla
Copy link

OlehSuvorovSvitla commented Sep 22, 2021

Hello,

My windows 10 was crashed and now I'm trying to extract Tampermonkey script from extension.
Found 000003 file (which contains them), but cannot run python script above due to errors like leveldb is missing.

Can someone please run that scripts for me?
Uploaded file to: https://1drv.ms/u/s!ApdatwDcvb1ijGud-awXAi5aIFFN?e=hYyutS (as 7z, not accepted unpacked)
Mirror: https://drive.google.com/file/d/1v73eb8TNtZ8d5OlPje2OXjS8aGI9F_FH/view (raw text)

@DonaldDuck313
Copy link

DonaldDuck313 commented Oct 31, 2021

For it to work in Python 3, you need to change line 19 to m = pattern.match(str(k)) and line 24 to print("Writing to %s" % full_name)

@iamqiz
Copy link

iamqiz commented Nov 25, 2021

@OlehSuvorovSvitla
on windows, i cannt install leveldb , so i use wsl to run the python;
here is result from your file
https://1drv.ms/u/s!Aso5a3Kpi4YwgXekt87RSxpi-BTY?e=GTwciO

@OlehSuvorovSvitla
Copy link

OlehSuvorovSvitla commented Nov 25, 2021

@iamqiz oh, thank you very much for this work!

I tested extracting the 000003 file for ~500kb for me and placing it in the extension's folder - and it also worked somehow o_O
So my tampermonkey after all the restarts start to see older scripts.
Now I enabled sync of files, hopefully, it will not happen again

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