-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
96 lines (84 loc) · 3.65 KB
/
popup.js
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
document.addEventListener('DOMContentLoaded', function() {
const scriptInput = document.getElementById('scriptInput');
const executeBtn = document.getElementById('executeBtn');
const resultDiv = document.getElementById('result');
executeBtn.addEventListener('click', function() {
const script = scriptInput.value.trim();
if (!script) {
resultDiv.innerHTML = '<div class="error">Please paste a script first.</div>';
return;
}
try {
const moduleMap = extractModuleMap(script);
const filenames = generateFilenames(moduleMap);
// Create header with copy button
const headerHtml = `
<div class="header-with-copy">
<span>Generated JavaScript Files (${filenames.length} files):</span>
<button class="copy-button" id="copyButton">
<svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M8 4v12a2 2 0 002 2h8a2 2 0 002-2V7.242a2 2 0 00-.602-1.43L16.083 2.57A2 2 0 0014.685 2H10a2 2 0 00-2 2z" />
<path d="M16 18v2a2 2 0 01-2 2H6a2 2 0 01-2-2V9a2 2 0 012-2h2" />
</svg>
Copy
</button>
</div>
`;
// Create file list
const fileListHtml = filenames.map(filename =>
`<div class="file-item">${filename}</div>`
).join('');
// Combine header and file list
resultDiv.innerHTML = headerHtml + fileListHtml;
// Add copy functionality
const copyButton = document.getElementById('copyButton');
copyButton.addEventListener('click', function() {
const textToCopy = filenames.join('\n');
navigator.clipboard.writeText(textToCopy).then(() => {
copyButton.classList.add('copy-success');
copyButton.innerHTML = `
<svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M20 6L9 17l-5-5" />
</svg>
Copied!
`;
setTimeout(() => {
copyButton.classList.remove('copy-success');
copyButton.innerHTML = `
<svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M8 4v12a2 2 0 002 2h8a2 2 0 002-2V7.242a2 2 0 00-.602-1.43L16.083 2.57A2 2 0 0014.685 2H10a2 2 0 00-2 2z" />
<path d="M16 18v2a2 2 0 01-2 2H6a2 2 0 01-2-2V9a2 2 0 012-2h2" />
</svg>
Copy
`;
}, 2000);
}).catch(err => {
console.error('Failed to copy text: ', err);
resultDiv.innerHTML = '<div class="error">Failed to copy to clipboard.</div>';
});
});
} catch (error) {
resultDiv.innerHTML = `<div class="error">Error: ${error.message}</div>`;
}
});
});
function extractModuleMap(script) {
// Extract the module map from the script
const moduleMapMatch = script.match(/\{[\s\S]*\}/);
if (!moduleMapMatch) {
throw new Error('Invalid script format. Please provide a valid module map.');
}
// Clean up the module map string to ensure it's valid JSON
const moduleMapStr = moduleMapMatch[0]
.replace(/(\d+):/g, '"$1":') // Add quotes around numeric keys
.replace(/'/g, '"'); // Replace single quotes with double quotes
// Parse the module map
return JSON.parse(moduleMapStr);
}
function generateFilenames(moduleMap) {
// Generate filenames from the module map
return Object.entries(moduleMap).map(([id, hash]) => {
return `${id === "76" ? "common" : id}.${hash}.js`;
});
}
// ... rest of existing code ...