-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
77 lines (65 loc) · 2.83 KB
/
migrate.js
File metadata and controls
77 lines (65 loc) · 2.83 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
const fs = require('fs');
const path = require('path');
const optionsHtmlPath = path.join(__dirname, 'extension', 'options.html');
const optionsJsPath = path.join(__dirname, 'extension', 'options.js');
const dashboardPagePath = path.join(__dirname, 'website', 'src', 'app', 'dashboard', 'page.tsx');
const htmlContent = fs.readFileSync(optionsHtmlPath, 'utf8');
const jsContent = fs.readFileSync(optionsJsPath, 'utf8');
// Extract body inner HTML
const bodyMatch = htmlContent.match(/<body>([\s\S]*?)<script src="options.js"><\/script>[\s\S]*?<\/body>/);
let innerHtml = bodyMatch ? bodyMatch[1] : '';
// Replace icon.png with /icon.png
innerHtml = innerHtml.replace(/src="icon.png"/g, 'src="/icon.png"');
// Replace chrome.storage.local with customStorage
let modifiedJs = jsContent.replace(/chrome\.storage\.local\./g, 'customStorage.');
// Prepare the Next.js page content
const pageContent = `
"use client";
import React, { useEffect } from 'react';
import './dashboard.css';
export default function Dashboard() {
useEffect(() => {
const EXTENSION_ID = "iikfkgaamfbbcglnimebghifbgeofoln";
const customStorage = {
get: (keys, callback) => {
if (window.chrome && window.chrome.runtime) {
window.chrome.runtime.sendMessage(EXTENSION_ID, { action: "getData", keys }, (response) => {
if (window.chrome.runtime && window.chrome.runtime.lastError) {
console.error("Extension not connected:", window.chrome.runtime.lastError);
callback({});
} else {
callback(response || {});
}
});
} else {
console.warn("Chrome runtime not available.");
callback({});
}
},
set: (data, callback) => {
if (window.chrome && window.chrome.runtime) {
window.chrome.runtime.sendMessage(EXTENSION_ID, { action: "setData", data }, (response) => {
if (window.chrome.runtime && window.chrome.runtime.lastError) {
console.error("Extension not connected:", window.chrome.runtime.lastError);
}
if (callback) callback();
});
} else {
console.warn("Chrome runtime not available.");
if (callback) callback();
}
}
};
// Inject JS logic
${modifiedJs}
}, []);
return (
<>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
<div className="dashboard-container" dangerouslySetInnerHTML={{ __html: \`${innerHtml.replace(/`/g, '\\`').replace(/\$/g, '\\$')}\` }} />
</>
);
}
`;
fs.writeFileSync(dashboardPagePath, pageContent);
console.log('Successfully generated page.tsx');