Skip to content

Instantly share code, notes, and snippets.

@derjanb
Last active April 29, 2024 23:29
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save derjanb/9f6c10168e63c3dc3cf0 to your computer and use it in GitHub Desktop.
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)
@rakitanc
Copy link

rakitanc commented Oct 20, 2022

@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
Copy link

iamqiz commented Oct 20, 2022

@rakitanc happy to hear you got your script back

@EdFrees
Copy link

EdFrees commented Nov 4, 2022

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

iamqiz commented Nov 5, 2022

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

@EdFrees
Copy link

EdFrees commented Nov 5, 2022

@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
Copy link

iamqiz commented Nov 6, 2022

@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
Copy link

EdFrees commented Nov 7, 2022

@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
Copy link

EdFrees commented Nov 19, 2022

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

@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?

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