Skip to content

Commit ad8e7cc

Browse files
committed
feat: init apps/tampermonkey
1 parent d907b52 commit ad8e7cc

File tree

11 files changed

+623
-39
lines changed

11 files changed

+623
-39
lines changed

apps/tampermonkey/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
src-node
11+
12+
node_modules
13+
dist
14+
dist-ssr
15+
*.local
16+
tampermonkey.js
17+
*-lock.json
18+
*-lock.yml
19+
*-lock.yaml
20+
21+
# Development script
22+
tampermonkey.js
23+
!dist
24+
# Editor directories and files
25+
.vscode/*
26+
!.vscode/extensions.json
27+
.idea
28+
.DS_Store
29+
*.suo
30+
*.ntvs*
31+
*.njsproj
32+
*.sln
33+
*.sw?

apps/tampermonkey/dist/script.iife.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ==UserScript==
2+
// @name next-i18n-docs tampermonkey
3+
// @namespace https://nextjs.org/
4+
// @version 0.0.1
5+
// @description
6+
// @author
7+
// @match *://*
8+
// @grant GM_xmlhttpRequest
9+
// @run-at document-end
10+
// ==/UserScript==
11+
12+
(function() {
13+
"use strict";
14+
console.log("Tampermonkey script loaded");
15+
})();

apps/tampermonkey/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@next-i18n/tampermonkey",
3+
"description": "Tampermonkey script for Next.js i18n",
4+
"private": true,
5+
"version": "0.0.0",
6+
"type": "module",
7+
"scripts": {
8+
"dev": "vite build --watch --mode development",
9+
"dev:server": "vite",
10+
"build": "vite build",
11+
"build:dev-script": "node script/gen-tampermonkey.cjs",
12+
"type-check": "tsc --noEmit"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^22.7.5",
16+
"typescript": "5.4.5",
17+
"vite": "^5.4.8"
18+
}
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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();

apps/tampermonkey/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Tampermonkey script loaded');

apps/tampermonkey/src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

apps/tampermonkey/tampermonkey.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ==UserScript==
2+
// @name next-i18n-docs tampermonkey
3+
// @namespace https://nextjs.org/
4+
// @version 0.0.1
5+
// @description
6+
// @author
7+
// @match *://*
8+
// @grant GM_xmlhttpRequest
9+
// @run-at document-end
10+
// ==/UserScript==

apps/tampermonkey/tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"module": "ESNext",
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "Bundler",
11+
"resolveJsonModule": true,
12+
"isolatedModules": true,
13+
"noEmit": true,
14+
15+
/* Linting */
16+
"strict": true,
17+
"noUnusedLocals": true,
18+
"noUnusedParameters": true,
19+
"noFallthroughCasesInSwitch": true,
20+
21+
"baseUrl": "./",
22+
"paths": {
23+
"@/*": ["./src/*"]
24+
}
25+
},
26+
"include": ["src"]
27+
}

apps/tampermonkey/vite.config.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { readFileSync } from 'node:fs';
2+
import { resolve } from 'node:path';
3+
import { defineConfig } from 'vite';
4+
5+
// Read Tampermonkey config for banner (already includes ==UserScript== markers)
6+
const banner = readFileSync(resolve(__dirname, 'tampermonkey.config'), 'utf-8');
7+
8+
export default defineConfig(({ mode }) => ({
9+
plugins: [
10+
{
11+
name: 'tampermonkey-banner',
12+
generateBundle(options, bundle) {
13+
// Add banner to all JS files in the bundle
14+
for (const fileName of Object.keys(bundle)) {
15+
if (fileName.endsWith('.js')) {
16+
const file = bundle[fileName];
17+
if (file.type === 'chunk') {
18+
file.code = `${banner}\n\n${file.code}`;
19+
}
20+
}
21+
}
22+
},
23+
},
24+
],
25+
server: {
26+
host: 'localhost',
27+
port: 6419,
28+
},
29+
build: {
30+
minify: false,
31+
outDir: 'dist',
32+
emptyOutDir: false,
33+
lib: {
34+
entry: resolve(__dirname, 'src/main.ts'),
35+
name: 'script',
36+
fileName: 'script',
37+
formats: ['iife'],
38+
},
39+
rollupOptions: {
40+
external: [],
41+
output: {
42+
globals: {},
43+
},
44+
},
45+
watch: mode === 'development' ? {} : undefined,
46+
},
47+
css: {
48+
preprocessorOptions: {
49+
less: {
50+
javascriptEnabled: true,
51+
},
52+
},
53+
},
54+
resolve: {
55+
alias: {
56+
'@/*': resolve(__dirname, 'src'),
57+
},
58+
},
59+
define: {
60+
'process.env.NODE_ENV': JSON.stringify(
61+
process.env.NODE_ENV || 'development',
62+
),
63+
},
64+
}));

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"docs:dev": "pnpm --filter @next-i18n/docs dev",
1919
"docs:build": "pnpm --filter @next-i18n/docs build",
2020
"docs:start": "pnpm --filter @next-i18n/docs start",
21+
"tampermonkey:dev": "pnpm --filter @next-i18n/tampermonkey dev",
22+
"tampermonkey:build": "pnpm --filter @next-i18n/tampermonkey build",
2123
"docs:update-search-index": "pnpm --filter @next-i18n/docs update-search-index",
2224
"docs:submit-sitemap": "pnpm --filter @next-i18n/docs submit-sitemap",
2325
"crawler": "node packages/crawler/dist/index.js"

0 commit comments

Comments
 (0)