Just use this git repo as a template.
- Allow Tampermonkey's access to local file URIs tampermonkey/faq
- install deps with
npm i
ornpm ci
. npm run dev
to start your development.
Now you will see 2 files in ./dist/
dist/index.dev.user.js
: You should install this userscript in your browser. It's a simple loader that loaddist/index.debug.js
on matched web page.dist/index.debug.js
: This is the development build witheval-source-map
. It will be automatically loaded bydist/index.dev.user.js
via@require file://.../dist/index.debug.js
metadata, Don't add it to your userscript manager.
- edit src/index.ts, you can even import css or less files. You can use scss if you like.
- go wo https://www.example.com/ and open console, you'll see it's working.
livereload is default enabled, use this Chrome extension
Everytime you change your metadata config,
you'll have to restart webpack server and install newly generated dist/index.dev.user.js
UserScript in your browser again.
If you prefer some other bundler like rollup, you can use some of these packages directly.
userscript-metadata-webpack-plugin
you can call GM.xmlHttpRequest
directly or use a fetch API based on GM.xmlHttpRequest
https://github.com/Trim21/gm-fetch
use typescript as normal, see example
There are two ways to using a package on npm.
like original UserScript way, you will need to add them to your user script metadata's require section , and exclude them in config/webpack.config.base.cjs
just install packages with npm and import them in your code, webpack will take care them.
npm run build
dist/index.prod.user.js
is the final script. you can manually copy it to greasyfork for deploy.
There is a limitation in greasyfork, your code must not be obfuscated or minified.
If you don't need to deploy your script to greasyfork, enable minify as you like.
github actions will deploy production userscript to gh-pages branch.
You can auto use greasyfork's auto update function.
you may find enabling source map not working well in production code, because Tampermonkey will add extra lines (all your @require
) before your script. I don't know if there is a good fix for this, You need to use webpack config devtool
with eval
prefix to make it work as expected, so source map is disabled in this production build.