How to have a userscript in TamperMonkey/GreaseMonkey automatically update a GitHub repo
TL;DR: I edit scripts in TamperMonkey (GreaseMonkey replacement) and have the changes automatically update a master GitHub repo by backing TamperMonkey up to Dropbox, then having an OS X Folder Action perform the GitHub update.
Here’s a pretty arcane trick that hopefully will be of use to some other person, somewhere, someday.
In my GitHub repo I have shared a userscript called “WebCooler-userscript” that gives me flexible web content filtering that uses powerful rules to cleanly and undetectable remove content containing certain hotbutton phrases from my web browsing experience, mostly so I don’t have to see other people’s stoopit opinions or inflammatory “outrage of the days”-type news items about things it does nobody any good for me to stress about.
My main browser nowadays is Opera, and I use the TamperMonkey extension to run GreaseMonkey userscripts. So these instructions will be particular to TamperMonkey and OS X, although I imagine they’re easily adaptable to GreaseMonkey or its other variants. It uses OS X automator so you’re out of luck on other platforms.
TamperMonkey has a nice feature in that it integrates JShint, a javascript bug-highlighting tool, right into its script editor, making it extremely easy to spot errors as you type. The problem, once I decided to share my script on GitHub, was that while TamperMonkey can automatically update scripts from masters on GitHub, editing on Github is both a painful nightmare of too many mouse clicks, and has no way to integrate JShint, leaving my code solely up to the vagaries of my sloppy and thoroughly nonprofessional javascript skills. Ditto for editing using an editor on my computer and pushing to GitHub. No, the only thing that was going to work for me was to be able to edit in TamperMonkey, as I’d always successfully done, and have changes pushed to GitHub.
For a while, I updated the script in TamperMonkey and then copied it and manually clicked over to GitHub to save the new versions in my public repos. This got really tiresome, though, and I felt sure there was a way to make it automatic.
And there was.
TamperMonkey includes a feature to back up your TamperMonkey scripts and settings to several cloud sites, including Dropbox, which I use.
TamperMonkey saves everything in a single .zip file, which, when Dropbox updates my local dropbox folder, is saved in ~/Dropbox/Apps/TamperMonkey:
Since this is a local folder, I realized I could attach a Folder Action to it. Installing the GitHub Mac client gives you command line access to Git (or maybe it’s built into OS X, I try to know as alittle about Git is apoosible) so I created a Folder Action that looked for new .zip files in that folder, expanded them, searched the expanded folder for WebCooler.user.js, copied it to my local GitHub repo, then issued the terminal commands to commit it to my GitHub master, and deleted the expanded folder, leaving only the .zip archive.
A screenshot of the folder action is below. You can download the action from this link: Update WebCooler github when tampermonkey backed up.workflow
A few warnings before trying this yourself:
1.) You will need to replace ~/Dropbox/Documents/Output/Software/WebCooler\ Repo
with the path to your local repo to use this code.
2.) You may need to delete the file [your repo path]/.git/hooks/pre-push
for the Git commands in this to actually work. Why? I don’t know. Me, Git, alittle, apoosible, remember? Maybe it’ll break something if you delete that file. I make no promises.
3.) The pedantic among you are already warming up their typing fingers, getting ready to tell me how this could be done better. I know, I know. It could all be done in terminal, I have unnecessary extra options set in the Automator steps, etc. etc. etc. I know. It worked for me, now I’m ready to move on with my life.
4.) This code is offered without warrantee, guarantee, dungarees, or Haynes beefy tee.This post is offered for entertainment value only. It worked for me, but I have no idea what it will do if you try it. If you use this code, or attempt anything I explain, the results are your responsibility, not mine. You’re on your own. You shouldn’t listen to anything I tell you. You’ll be lucky if you don’t put an eye out. And always remember to back up EVERYTHING up first.
5.) If you try this, and something goes horribly wrong, see #4 immediately above.
Update: NOPE! This doesn’t quite work. Because Git is much too complicated, it turns out that if you haven’t logged into GitHub Desktop, this whole thing doesn’t work. I found a page at https://help.github.com/articles/caching-your-github-password-in-git/ that talks about authenticating from the command line, but I’m too busy to futz with it now. I’ll come back and update this page when I get it working without the GitHub Desktop requirement.
Automator screenshot of the whole shebang: