-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartfn.ts
110 lines (94 loc) · 2.85 KB
/
chartfn.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { serveFile } from "jsr:@std/http/file-server";
import { dirname, extname } from "jsr:@std/path";
const regex3 = /wp3\.deno\.dev/i;
const regex1 = /https\:\/\/static\.tradingview\.com/g;
// const regex2 = /data\.tradingview\.com/i;
export async function chartfn(req: Request) {
/*
return serveFile(
req,
"./tv.html",
);
*/
const target = "https://tradingview.com/chart";
const nh = new Headers(req.headers);
const value = nh.get("host");
if (value != null) {
nh.set("host", value.replace(regex3, "tradingview.com"));
}
// console.log("/chart nh", nh);
const res = await fetch(target, {
headers: nh,
});
// return res;
let indexhtml = await res.text();
indexhtml = indexhtml.replace(regex1, "");
// indexhtml = indexhtml.replace(regex2, "wp3.deno.dev");
return new Response(indexhtml, res);
}
export async function staticfn(req: Request, pathname: string) {
const target = "https://static.tradingview.com" + pathname;
// const localfile = "/home/ste/tradingview/20240813" + pathname;
// const localdir = dirname(localfile);
// const localfilext = extname(localfile);
const nh = new Headers(req.headers);
["host", "origin", "referer"].forEach((key) => {
const value = nh.get(key);
if (value != null) {
nh.set(key, value.replace(regex3, "tradingview.com"));
}
});
const localfilext = extname(pathname);
if (nh.has("Sec-Fetch-Dest")) {
const destzhi = localfilext == ".css" ? "style" : "script";
nh.set("Sec-Fetch-Dest", destzhi);
}
// console.log("/static nh", nh);
const res = await fetch(target, {
headers: nh,
});
return res;
// let indexhtml = await res.text();
// return new Response(indexhtml, res);
}
//========================================
function tihuan(str: string, strthen: string): string {
return str.replace(regex3, strthen);
}
export async function staticfn2(req: Request, pathname: string) {
const target = "https://static.tradingview.com" + pathname;
// const localfile = "/home/ste/tradingview/20240813" + pathname;
// const localdir = dirname(localfile);
// const localfilext = extname(localfile);
const localfilext = extname(pathname);
const nh = new Headers();
for (var [key, value] of req.headers) {
switch (key) {
case "host": {
nh.set(key, tihuan(value, "tradingview.com"));
break;
}
case "origin": {
nh.set(key, tihuan(value, "tradingview.com"));
break;
}
case "referer": {
nh.set(key, tihuan(value, "tradingview.com"));
break;
}
case "Sec-Fetch-Dest": {
const destzhi = localfilext == ".css" ? "style" : "script";
nh.set(key, destzhi);
break;
}
default: {
nh.append(key, value);
}
}
}
console.log("nh", nh);
const res = await fetch(target, {
headers: nh,
});
return res;
}