-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (67 loc) · 2.08 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
const WebSocket = require("ws");
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const fileListUrl = `http://localhost:9222/json`;
const fileSocketMap = {};
const payload = {
id: 1337,
method: "Runtime.evaluate",
params: { expression: "" },
};
let fileList;
let payloads = "";
let dependencies = '';
const sendPayload = (id, client) => client.send(JSON.stringify(payload));
const buildPayloads = () => {
const dir = path.join(__dirname, "payloads/");
const filenames = fs.readdirSync(dir);
filenames.forEach((filename) => {
const payload = fs.readFileSync(dir + filename).toString();
// We prefix vendor/dep packages with _ and put them at the top of the bundle
if(filename.startsWith('_')) {
dependencies += payload;
} else {
payloads += payload;
}
});
// Wrapper for payloads, so they only run once per file/page
payload.params.expression = `
(function () {
if (typeof window.hasBeenPatched === "undefined") {
${dependencies}
${payloads}
window.hasBeenPatched = true;
}
})();
`;
};
const pollAndInject = async () => {
let response = await fetch(fileListUrl);
fileList = await response.json();
fileList.forEach((file) => {
// Skip official Figma pages
if (
file.title === "Figma" ||
file.title === "DevTools" ||
file.title === "Recently viewed – Figma" ||
file.url === "https://www.figma.com/community"
)
return;
const client = fileSocketMap[file.id];
if (client && client.readyState === WebSocket.OPEN) {
sendPayload(file.id, client);
} else {
const newClient = new WebSocket(file.webSocketDebuggerUrl);
newClient.on("open", async () => sendPayload(file.id, newClient));
fileSocketMap[file.id] = newClient;
}
});
await new Promise((resolve) => setTimeout(resolve, 3000)); // fetch list of files every 3s
await pollAndInject();
};
buildPayloads();
pollAndInject();
console.log(
"Patcher Running...please keep this window open, you can minimize it if you want."
);