forked from carcabot/tiktok-signature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (72 loc) · 2.13 KB
/
index.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
const puppeteer = require("puppeteer-extra");
// const devices = require('puppeteer/DeviceDescriptors');
// const iPhonex = devices['iPhone X'];
const pluginStealth = require("puppeteer-extra-plugin-stealth");
puppeteer.use(pluginStealth());
class Signer {
userAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36";
args = [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-infobars",
"--window-position=0,0",
"--ignore-certifcate-errors",
"--ignore-certifcate-errors-spki-list",
"--host-rules=MAP tiktok.com 127.0.0.1"
];
constructor(userAgent, tac) {
if (userAgent) {
this.userAgent = userAgent;
}
if (tac) {
this.tac = tac;
}
this.args.push(`--user-agent="${this.userAgent}"`);
this.options = {
args: this.args,
headless: true,
ignoreHTTPSErrors: true,
userDataDir: "./tmp"
};
}
async init() {
this.browser = await puppeteer.launch(this.options);
this.page = await this.browser.newPage();
// await this.page.emulate(iPhonex);
await this.page.setUserAgent(this.userAgent);
// await this.page.goto('file://' + __dirname + '/index.html', { waitUntil: 'load' });
await this.page.goto("http://tiktok.com:8080/index.html", {
waitUntil: "load"
});
if (this.tac) {
await this.page.evaluate(x => {
window.tac = x;
}, this.tac);
}
return this;
}
async getTac() {
this.browser = await puppeteer.launch(this.options);
this.page = await this.browser.newPage();
await this.page.setUserAgent(this.userAgent);
await this.page.goto("https://www.tiktok.com/trending", {
waitUntil: "load"
});
const data = await this.page.content();
var token = data.match(
new RegExp("<script>tac='" + "(.+?)" + "'</script>", "ig")
);
return token;
}
async sign(str) {
let res = await this.page.evaluate(`generateSignature("${str}")`);
return res;
}
async close() {
await this.browser.close();
this.browser = null;
this.page = null;
}
}
module.exports = Signer;