diff --git a/build.ts b/build.ts index dad0706..4bdcbf7 100644 --- a/build.ts +++ b/build.ts @@ -1,12 +1,32 @@ /* eslint-env node */ import esbuild from 'esbuild'; +import packageJson from './package.json'; const isProduction = process.env.NODE_ENV === 'production'; +// TODO configure auto-update +// @updateURL https://raw.githubusercontent.com/yourusername/yourrepository/main/youruserscript.user.js +// @downloadURL https://raw.githubusercontent.com/yourusername/yourrepository/main/youruserscript.user.js + +const banner = ` +// ==UserScript== +// @name ${packageJson.name} +// @version ${packageJson.version} +// @description ${packageJson.description} +// @author ${packageJson.author} +// @match * +// @grant GM_registerMenuCommand +// ==/UserScript== +`; + await esbuild.build({ entryPoints: ['./src/index.ts'], - outfile: './dist/index.js', + outfile: './dist/scraper.user.js', + + banner: { + js: banner, + }, target: ['es2019'], bundle: true, diff --git a/package.json b/package.json index 53ac21a..a7aefad 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ "lint": "eslint --cache --ext .ts src/", "fmt": "prettier --write src/", "check-fmt": "prettier --check src/", - "test": "", - "prepare": "npm run build" + "test": "" }, "author": "mbme", "license": "GPL-3.0-or-later", diff --git a/src/index.ts b/src/index.ts index 1e50dd2..d9ae942 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,17 @@ import { BrowserScraper } from './browser-scraper'; declare global { interface Window { _scraper: BrowserScraper; + GM_registerMenuCommand?: (menuItem: string, cb: () => void) => void; } } -window._scraper = new BrowserScraper(); -window._scraper.injectScraperUI(); +function initScraper() { + window._scraper = new BrowserScraper(); + window._scraper.injectScraperUI(); +} + +if (window.GM_registerMenuCommand) { + window.GM_registerMenuCommand('Run scraper', initScraper); +} else { + initScraper(); +} diff --git a/src/test-utils.ts b/src/test-utils.ts index 3b5208a..0874f3b 100644 --- a/src/test-utils.ts +++ b/src/test-utils.ts @@ -11,7 +11,10 @@ const DEBUG = process.env.DEBUG === 'true'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -export const scraperScript = await fs.readFile(path.join(__dirname, '../dist/index.js'), 'utf-8'); +export const scraperScript = await fs.readFile( + path.join(__dirname, '../dist/scraper.user.js'), + 'utf-8', +); let browser: Browser | undefined; let context: BrowserContext | undefined;