Update app.js
Browse files
app.js
CHANGED
|
@@ -2151,18 +2151,36 @@ app.post("/playwright", async (req, res) => {
|
|
| 2151 |
}
|
| 2152 |
|
| 2153 |
try {
|
|
|
|
|
|
|
|
|
|
| 2154 |
const vm = new NodeVM({
|
| 2155 |
-
sandbox: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2156 |
require: {
|
| 2157 |
external: ["playwright"],
|
| 2158 |
builtin: [],
|
| 2159 |
},
|
| 2160 |
});
|
| 2161 |
|
| 2162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2163 |
|
| 2164 |
-
|
| 2165 |
-
res.json({ output });
|
| 2166 |
} catch (error) {
|
| 2167 |
res.status(500).json({ output: error.message });
|
| 2168 |
}
|
|
|
|
| 2151 |
}
|
| 2152 |
|
| 2153 |
try {
|
| 2154 |
+
let output = "";
|
| 2155 |
+
|
| 2156 |
+
// Menangkap console.log dari dalam VM
|
| 2157 |
const vm = new NodeVM({
|
| 2158 |
+
sandbox: {
|
| 2159 |
+
console: {
|
| 2160 |
+
log: (...args) => {
|
| 2161 |
+
output += args.map(a => String(a)).join(" ") + "\n";
|
| 2162 |
+
}
|
| 2163 |
+
}
|
| 2164 |
+
},
|
| 2165 |
require: {
|
| 2166 |
external: ["playwright"],
|
| 2167 |
builtin: [],
|
| 2168 |
},
|
| 2169 |
});
|
| 2170 |
|
| 2171 |
+
// Membungkus kode agar menangkap error dengan baik
|
| 2172 |
+
const wrappedScript = `
|
| 2173 |
+
(async () => {
|
| 2174 |
+
try {
|
| 2175 |
+
${code}
|
| 2176 |
+
} catch (err) {
|
| 2177 |
+
console.log("Error:", err.message);
|
| 2178 |
+
}
|
| 2179 |
+
})();
|
| 2180 |
+
`;
|
| 2181 |
|
| 2182 |
+
await vm.run(wrappedScript, import.meta.url);
|
| 2183 |
+
res.json({ output: output.trim() });
|
| 2184 |
} catch (error) {
|
| 2185 |
res.status(500).json({ output: error.message });
|
| 2186 |
}
|