-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
executable file
·128 lines (107 loc) · 4.7 KB
/
Copy pathinstall.js
File metadata and controls
executable file
·128 lines (107 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const zlib = require('zlib');
const { execSync } = require('child_process');
const ROOT = __dirname;
// ── Generate placeholder icons ─────────────────────────────────────────────────
function crc32(buf) {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let j = 0; j < 8; j++) c = (c & 1) ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
table[i] = c;
}
let crc = 0xFFFFFFFF;
for (let i = 0; i < buf.length; i++) crc = table[(crc ^ buf[i]) & 0xFF] ^ (crc >>> 8);
return (crc ^ 0xFFFFFFFF) >>> 0;
}
function pngChunk(type, data) {
const t = Buffer.from(type);
const len = Buffer.allocUnsafe(4);
len.writeUInt32BE(data.length, 0);
const crc = Buffer.allocUnsafe(4);
crc.writeUInt32BE(crc32(Buffer.concat([t, data])), 0);
return Buffer.concat([len, t, data, crc]);
}
function makePNG(size, r, g, b) {
const sig = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(size, 0);
ihdr.writeUInt32BE(size, 4);
ihdr[8] = 8; ihdr[9] = 2; // 8-bit RGB
const rowLen = 1 + size * 3;
const raw = Buffer.alloc(size * rowLen);
for (let y = 0; y < size; y++) {
raw[y * rowLen] = 0; // filter type None
for (let x = 0; x < size; x++) {
const i = y * rowLen + 1 + x * 3;
raw[i] = r; raw[i + 1] = g; raw[i + 2] = b;
}
}
return Buffer.concat([
sig,
pngChunk('IHDR', ihdr),
pngChunk('IDAT', zlib.deflateSync(raw)),
pngChunk('IEND', Buffer.alloc(0)),
]);
}
const iconsDir = path.join(ROOT, 'extension', 'icons');
for (const size of [16, 48, 128]) {
const iconPath = path.join(iconsDir, `icon${size}.png`);
if (!fs.existsSync(iconPath)) {
// Krunker orange (#F29F00)
fs.writeFileSync(iconPath, makePNG(size, 0xF2, 0x9F, 0x00));
console.log(` Created icon${size}.png`);
}
}
// ── npm install for native host ────────────────────────────────────────────────
const hostDir = path.join(ROOT, 'native-host');
if (!fs.existsSync(path.join(hostDir, 'node_modules'))) {
console.log('Installing native host dependencies…');
execSync('pnpm install', { cwd: hostDir, stdio: 'inherit' });
}
fs.chmodSync(path.join(hostDir, 'host.js'), 0o755);
// ── Args ───────────────────────────────────────────────────────────────────────
const extensionId = process.argv[2];
if (!extensionId || !/^[a-z]{32}$/.test(extensionId)) {
console.error('\nUsage: node install.js <chrome-extension-id>');
console.error(' Load the extension in Chrome first, then copy the 32-letter ID from chrome://extensions');
process.exit(1);
}
// ── Write wrapper shell script (Chrome has a minimal PATH; node won't be found
// via /usr/bin/env on macOS Homebrew installs without this) ─────────────────
// Use `which node` to get the stable symlink path (e.g. /opt/homebrew/bin/node)
// rather than process.execPath which may point to a versioned Cellar path that
// breaks after `brew upgrade node`.
const nodeBin = execSync('which node', { encoding: 'utf8' }).trim();
const runScript = path.join(hostDir, 'run.sh');
fs.writeFileSync(
runScript,
`#!/bin/sh\nexec "${nodeBin}" "${path.join(hostDir, 'host.js')}"\n`,
);
fs.chmodSync(runScript, 0o755);
// ── Write native messaging host manifest ───────────────────────────────────────
const hostManifest = {
name: 'com.krunker.discord_rpc',
description: 'Discord Rich Presence for Krunker.io',
path: runScript,
type: 'stdio',
allowed_origins: [`chrome-extension://${extensionId}/`],
};
// Helium (net.imput.helium) is the target browser
const nativeMsgDir = path.join(
os.homedir(),
'Library', 'Application Support', 'net.imput.helium', 'NativeMessagingHosts',
);
fs.mkdirSync(nativeMsgDir, { recursive: true });
const manifestPath = path.join(nativeMsgDir, 'com.krunker.discord_rpc.json');
fs.writeFileSync(manifestPath, JSON.stringify(hostManifest, null, 2));
console.log('\nInstalled successfully!');
console.log(` Native host manifest → ${manifestPath}`);
console.log('\nNext steps:');
console.log(' 1. Reload the extension on helium://extensions');
console.log(' 2. Open krunker.io and join a game');
console.log(' 3. Discord should show your presence within a few seconds');