I recently started development on a Tampermonkey script, which is hosted on OpenUserJs. It seems that I'm going to invest a bit more time in the future on this script by keep it up to date and extend his features when time is there. The first lines I wrote on the Tampermonkey editor which is integrated in chrome (edit button of a script).
But I don't like it, the most thing I'm missing is some kind of autocomplete/intellisense. Visual Studio is much better here, so I switched to VS. The problem: After any changes, I have to copy the hole code and paste it in the Tampermonkey editor (Google Chrome). Thats annoying and not very flexible, since I can't really split my code in multiple js files when the script grows.
So is there a way to automate this? My imagination would be: I save the js file in VS (ctrl + s), then the script is loaded in my local development instance of google chrome for testing purpose.
Extension:
I want to publish alpha/beta releases as hosted version on OpenUserJs. So I can test the release easily on different systems. And I also have at least one system, where I do the real update process over the OpenUserJs repo like my end users will do. I think this is important, I already saw some differences according to my manual workflow (c&p in the OpenUserJs editor).
My preferable soultion would be some kind of branches like I know from git. So that I install the script from OpenUserJs like my users do with the production one, but I can choose somewhere to get e.g. the branch development instead of master. OpenUserJs seems to support github as source base, but no kind of branches. I can't imagine, that there is no solution for such issues, which at least every developer with bigger scripts should have...
I found my way to it, so it's not official or anything. And it's easier than it looks, I just wanted to be thorough. We are just instructing the browser and Tampermonkey (TM) to load the script from our file system, which we'll then edit directly.
Coding to instant updates
Save your script file wherever you want in your filesystem. Save the entire thing, including the
==UserScript==
header. This works in all desktop OS's, but since I'm using macOS, my path will be:/Users/me/Scripts/SameWindowHref.user.js
Now, go to the TM extension's dashboard, open the script in question in its editor, and delete everything except the entire
==UserScript==
headerAdd to the header a
@require
property pointing to the script's absolute path.At this point, TM's editor should look something like this:
Update: It seems that using the
file://
URI scheme at the beginning of your path is now required. On windows systems would be:For macOS and *nix, we'll need three slashes in a row:
Execution Contexts (advanced)
If you have multiple JavaScript files called with
@require
(like jQuery or when fragmenting a massive script into smaller pieces for a better experience), don't skip this part.The
@require
paths can reference*.user.js
or directly*.js
files, and any UserScript-style comment headers in these files have no effect.From the main script's
==UserScript==
header, all@require
files are text-concatenated in the order specified, with a single newline separating each file. This amalgamation runs as one large script. This means any global function or variable in any file will also be global in all your userscript's files, which isn't ideal. Errors in one file may influence how subsequent files run. Additionally, to enable strict mode on all of your files,'use strict';
must be the first statement of the first file listed with@require
.After all
@require
files run, your main UserScript (the one accessed by TamperMonkey's editor) is run in a separate context. If you want strict mode, you must also enable it here.Workflow
Now every time that script matches (
@match
) the website you are visiting, TamperMonkey will directly load and run the code straight from the file on disk, pointed by the@require
field.I use VSCode (arguably the best multiplatform code editor ever. And free), so that's where I work on the script, but any text editor will do. It should look like this:
Notice how TM's editor and your IDE/Editor have the same header.
Every change in the code is saved automatically in VSCode, so if yours doesn't: remember to save. Then you'll have to reload the website to load the changes.
Bonus tips!
You can easily automate reloading the site on file change using a one-liner from browser-sync's CLI, to mention one tool, and have a great experience.
If you're not using git, you should consider using it with your userscripts, even if you are the sole developer. It will help keep track of your progress, sanely work on different features at the same time, roll back mistakes, and help you automatically release new updates to your users!
And please share all your creations here and here
Working with GitHub or other SCMs
You have to add an
@updateURL
tag followed by the URL with the raw file from GitHub or whatever provider you chose. GitHub's example:Note that a
@version
tag is required to make update checks work. The vast majority of users don't need the@downloadURL
tag, so unless your script has a massive follower base, use@updateURL
.TM will check for updates as it's configured from the settings tab:
Externals sets how often the scripts called from your script's
@require
are checked to update (jQuery, for example).You can also "force" an update check:
Using external libraries (like jQuery)
It must be present at least in TM's editor to load it. However, I recommend keeping both headers (the TM's and the file on disk's header) the same to avoid confusion. Simply
@require
it like this to use it:Documentation
Take a look at TM's documentation page, it's very concise, and with a quick read, you'll have a clear picture of what you can do. Happy hacking!