-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (51 loc) · 1.19 KB
/
index.js
File metadata and controls
55 lines (51 loc) · 1.19 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
/**
* Executes a string of JavaScript code.
* @param {string} code - JavaScript code to execute.
*/
function loadString(code) {
try {
const fn = new Function('require', code);
fn(require);
} catch (err) {
console.error("❌ Error running code:", err);
}
}
/**
* Alias for loadString (Lua-style).
* @param {string} code - JavaScript code to execute.
*/
const loadstring = loadString;
/**
* Fetches and executes JavaScript from a remote URL.
* @param {string} url - URL pointing to a .js file.
* @returns {Promise<void>}
*/
async function loadStringFromURL(url) {
try {
const response = await fetch(url);
const code = await response.text();
const fn = new Function('require', code);
fn(require);
} catch (err) {
console.error("❌ Error fetching or running code:", err);
}
}
/**
* Smart loader: runs local code or loads from URL.
* @param {string} input - A string of code or a URL.
* @returns {Promise<void>}
*/
async function load(input) {
const isURL = /^https?:\/\//i.test(input);
if (isURL) {
await loadStringFromURL(input);
} else {
loadString(input);
}
}
module.exports = {
loadString,
loadstring,
loadStringFromURL,
load
};