|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | + |
| 4 | +async function generateDevScript() { |
| 5 | + try { |
| 6 | + // Import the config using dynamic import since it's now an ES module |
| 7 | + const viteConfigPath = path.resolve(__dirname, '../vite.config.ts'); |
| 8 | + |
| 9 | + // For CommonJS compatibility, we'll read the config manually |
| 10 | + const viteConfigContent = fs.readFileSync(viteConfigPath, 'utf-8'); |
| 11 | + const hostMatch = viteConfigContent.match(/host:\s*['"`]([^'"`]+)['"`]/); |
| 12 | + const portMatch = viteConfigContent.match(/port:\s*(\d+)/); |
| 13 | + |
| 14 | + const host = hostMatch ? hostMatch[1] : 'localhost'; |
| 15 | + const port = portMatch ? portMatch[1] : '6419'; |
| 16 | + const hostPort = `${host}:${port}`; |
| 17 | + |
| 18 | + const codeFilePath = '../tampermonkey.js'; |
| 19 | + const tampermonkeyConfig = fs.readFileSync( |
| 20 | + path.resolve(__dirname, '../tampermonkey.config'), |
| 21 | + 'utf-8', |
| 22 | + ); |
| 23 | + |
| 24 | + const codeContent = ` |
| 25 | + // ==UserScript== |
| 26 | + ${tampermonkeyConfig} |
| 27 | + // ==/UserScript== |
| 28 | +
|
| 29 | + (function () { |
| 30 | + 'use strict'; |
| 31 | + |
| 32 | + GM_xmlhttpRequest({ |
| 33 | + method: 'GET', |
| 34 | + url: 'http://${hostPort}/dist/script.iife.js', |
| 35 | + onload: function(res) { |
| 36 | + if (res && (res.status === 200)) { |
| 37 | + const text = res.responseText; |
| 38 | + |
| 39 | + if (typeof text === 'string') { |
| 40 | + eval(text); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + })() |
| 46 | + `; |
| 47 | + |
| 48 | + const outputPath = path.resolve(__dirname, codeFilePath); |
| 49 | + if (fs.existsSync(outputPath)) { |
| 50 | + fs.rmSync(outputPath); |
| 51 | + } |
| 52 | + |
| 53 | + fs.writeFileSync(outputPath, codeContent); |
| 54 | + console.log('✅ Development Tampermonkey script generated successfully'); |
| 55 | + } catch (error) { |
| 56 | + console.error('Error generating development script:', error); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +generateDevScript(); |
0 commit comments