Skip to content

Commit

Permalink
build userscript
Browse files Browse the repository at this point in the history
  • Loading branch information
mbme committed Jul 30, 2024
1 parent 4c5eaa8 commit 5bee9c3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
22 changes: 21 additions & 1 deletion build.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 4 additions & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5bee9c3

Please sign in to comment.