-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetFormData.ts
153 lines (124 loc) Β· 3.75 KB
/
getFormData.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import cheerio from 'cheerio';
enum GoogleFormsFieldTypeEnum {
TEXT = 0,
PARAGRAPH_TEXT = 1,
MULTIPLE_CHOICE = 2,
DROPDOWN = 3,
CHECKBOXES = 4,
SCALE = 5,
GRID = 7,
FILE_UPLOAD = 8,
DATE = 9,
TIME = 10,
}
enum EmailCollectionRuleEnum {
NONE = 1,
VERIFIED = 2,
INPUT = 3
}
interface Form {
title: string;
description: string | null;
collectEmails: "NONE" | "VERIFIED" | "INPUT";
questions: Question[];
error: false;
}
interface Question {
title: string;
description: string | null;
type: "TEXT" | "PARAGRAPH_TEXT" | "MULTIPLE_CHOICE" | "CHECKBOXES" | "DROPDOWN" | "DATE" | "TIME" | "SCALE" | "GRID" | "FILE_UPLOAD"
options: string[];
required: boolean;
id: string;
}
interface Error {
error: true;
message: string;
}
export async function getFormData(id: string): Promise<Form | Error> {
const url = `https://docs.google.com/forms/d/e/${id}/viewform`;
const response = await fetch(url);
if (!response.ok) {
return {
error: true,
message: 'Unable to fetch the form. Check your form ID and try again.'
}
}
const htmlContent = await response.text();
const $ = cheerio.load(htmlContent);
const scriptTags = $('script[type="text/javascript"]');
let fbPublicLoadDataScript: string | undefined;
scriptTags.each((_, tag) => {
const scriptContent = $(tag).html();
if (scriptContent && scriptContent.includes('FB_PUBLIC_LOAD_DATA_')) {
fbPublicLoadDataScript = scriptContent;
return false;
}
});
if (!fbPublicLoadDataScript) {
return {
error: true,
message: 'Unable to find the script tag containing FB_PUBLIC_LOAD_DATA_'
}
}
const beginIndex = fbPublicLoadDataScript.indexOf('[');
const lastIndex = fbPublicLoadDataScript.lastIndexOf(';');
const fbPublicJsScriptContentCleanedUp = fbPublicLoadDataScript
.substring(beginIndex, lastIndex)
.trim();
let jArray: any[];
try {
jArray = JSON.parse(fbPublicJsScriptContentCleanedUp);
} catch (error) {
return {
error: true,
message: 'The script data could not be parsed as JSON'
}
}
const description = jArray[1]?.[0] ?? null;
const title = jArray[3] ?? null;
const collectEmailsCodeValue = jArray[1]?.[10]?.[6] ?? null;
const collectEmailsEnum = EmailCollectionRuleEnum[collectEmailsCodeValue]
const collectEmails = collectEmailsEnum?.toString() ?? "NONE";
const arrayOfFields = jArray[1]?.[1] ?? [];
const form: Form = {
title,
description,
collectEmails: collectEmails as any,
questions: [],
error: false,
};
for (const field of arrayOfFields) {
if (field.length < 4 || !(field[4]?.length)) {
console.log("Continue: Non Submittable field or field without answer was found"); // Logging added
continue;
}
const questionText = field[1] as string;
const questionDescription = field[2] as string;
const questionTypeCodeValue = field[3];
const questionTypeEnum = GoogleFormsFieldTypeEnum[questionTypeCodeValue];
const questionType = questionTypeEnum?.toString();
const answerOptionsList: string[] = [];
const answerOptionsListValue = field[4]?.[0]?.[1] ?? [];
if (answerOptionsListValue.length > 0) {
for (const answerOption of answerOptionsListValue) {
const option = answerOption[0]?.toString();
if (option) {
answerOptionsList.push(option);
}
}
}
const answerSubmissionId = field[4]?.[0]?.[0] as string;
const isAnswerRequired = field[4]?.[0]?.[2] === 1;
const question: Question = {
title: questionText,
description: questionDescription,
type: questionType as any,
options: answerOptionsList,
required: isAnswerRequired,
id: answerSubmissionId
};
form.questions.push(question);
}
return form;
}