randydev commited on
Commit
3fe39f8
·
verified ·
1 Parent(s): 24dc15b

Create savefrom.js

Browse files
Files changed (1) hide show
  1. lib/savefrom.js +81 -0
lib/savefrom.js ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import got from "got";
2
+ import vm from "vm";
3
+ import { DEFAULT_HEADERS } from "./constant.js";
4
+ import { generateHash } from "./util.js";
5
+ import { SavefromArgsSchema, SavefromSchema } from "../lib/types/savefrom-v1.js";
6
+
7
+ export default async function savefrom(url) {
8
+ SavefromArgsSchema.parse(arguments);
9
+
10
+ const form = {
11
+ sf_url: url,
12
+ sf_submit: "",
13
+ new: "2",
14
+ lang: "en",
15
+ app: "",
16
+ country: "en",
17
+ os: "Windows",
18
+ browser: "Chrome",
19
+ channel: "main",
20
+ "sf-nomad": "1",
21
+ url,
22
+ ts: Date.now(),
23
+ _ts: 1720433117117,
24
+ _tsc: 0,
25
+ _s: generateHash(url),
26
+ _x: 1,
27
+ };
28
+
29
+ const data = await got
30
+ .post("https://worker.savefrom.net/savefrom.php", {
31
+ headers: {
32
+ ...DEFAULT_HEADERS,
33
+ "content-type": "application/x-www-form-urlencoded",
34
+ origin: "https://en.savefrom.net",
35
+ referer: "https://en.savefrom.net/",
36
+ },
37
+ form,
38
+ })
39
+ .text();
40
+
41
+ const context = {
42
+ results: null,
43
+ parent: { document: { location: {} } },
44
+ frameElement: {},
45
+ atob: (base64) => Buffer.from(base64, "base64").toString(),
46
+ _decodeURIComponent: (uri) => {
47
+ const decoded = decodeURIComponent(uri);
48
+ if (/showResult/.test(decoded)) {
49
+ context.results = decoded;
50
+ return "true";
51
+ }
52
+ return decoded;
53
+ },
54
+ };
55
+
56
+ vm.createContext(context);
57
+ new vm.Script(`decodeURIComponent=_decodeURIComponent;${data}`).runInContext(context);
58
+
59
+ const executed =
60
+ context.results?.split("window.parent.sf.videoResult.show(")?.[1] ||
61
+ context.results?.split("window.parent.sf.videoResult.showRows(")?.[1];
62
+
63
+ if (!executed) {
64
+ throw new Error("Cannot find result from evaluation!");
65
+ }
66
+
67
+ let json = null;
68
+ try {
69
+ if (context.results?.includes("showRows")) {
70
+ const splits = executed.split('],"');
71
+ const lastIndex = splits.findIndex((v) => v.includes("window.parent.sf.enableElement"));
72
+ json = JSON.parse(splits.slice(0, lastIndex).join('],"') + "]");
73
+ } else {
74
+ json = [JSON.parse(executed.split(");")[0])];
75
+ }
76
+ } catch (e) {
77
+ throw new Error("Cannot parse JSON results data from evaluation!");
78
+ }
79
+
80
+ return SavefromSchema.parse(json);
81
+ }