Skip to content

Instantly share code, notes, and snippets.

@AoDev
Last active January 13, 2024 17:27
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AoDev/5aa908ee8dd21f3904a0782a7f043718 to your computer and use it in GitHub Desktop.
Scrap and parse a Google form
;(function () {
const QUESTION_WRAPPER_SELECTOR = '.freebirdFormeditorViewItemContent'
const QUESTION_INFO_SELECTOR = '.quantumWizTextinputPapertextareaInput.exportTextarea'
const QUESTION_ANSWER_SELECTOR = '.quantumWizTextinputSimpleinputInput.exportInput'
const SECTION_SELECTOR = '.freebirdFormeditorViewPagePageCard'
const SECTION_HEADER_SELECTOR = '.freebirdFormeditorViewPageTitleAndDescription'
function findElements (selector, element = window.document) {
return [].slice.call(element.querySelectorAll(selector))
}
function scrapSurvey () {
const sections = findElements(SECTION_SELECTOR)
return sections.map((section) => {
// Get section header title and description
const sectionHeader = findElements(SECTION_HEADER_SELECTOR, section)
const sectionHeaderTexts = findElements('textarea', sectionHeader[0])
.reduce((sectionHeaderTexts, textElement) => {
if (textElement.dataset.initialValue) {
sectionHeaderTexts.push(textElement.dataset.initialValue)
}
return sectionHeaderTexts
}, [])
// Get questions for current section
const questionWrappers = findElements(QUESTION_WRAPPER_SELECTOR, section)
const sectionQuestions = questionWrappers.reduce((questions, questionWrapper) => {
const question = findElements(QUESTION_INFO_SELECTOR, questionWrapper)
const questionInfo = question.reduce((questionInfo, questionElement) => {
if (questionElement.dataset.initialValue) {
questionInfo.push(questionElement.dataset.initialValue)
}
return questionInfo
}, [])
if (questionInfo.length === 0) {
return questions
}
const questionData = {
title: questionInfo[0]
}
if (questionInfo[1]) {
questionData.description = questionInfo[1]
}
const answers = findElements(QUESTION_ANSWER_SELECTOR, questionWrapper)
const answerInfo = answers.reduce((answerInfo, answerElement) => {
if (answerElement.dataset.initialValue) {
answerInfo.push(answerElement.dataset.initialValue)
}
return answerInfo
}, [])
questionData.answers = answerInfo
questions.push(questionData)
return questions
}, [])
const sectionData = {
title: sectionHeaderTexts[0]
}
if (sectionHeaderTexts[1]) {
sectionData.description = sectionHeaderTexts[1]
}
if (sectionQuestions.length > 0) {
sectionData.questions = sectionQuestions
}
return sectionData
})
}
function questionsToString (sectionQuestions) {
return sectionQuestions.reduce((textVersion, questionDetails) => {
textVersion += questionDetails.title
if (questionDetails.description) {
textVersion += `\n${questionDetails.description}`
}
textVersion += '\n\n'
if (questionDetails.answers.length > 0) {
questionDetails.answers.forEach((answer) => {
textVersion += `- ${answer}\n`
})
}
textVersion += '\n\n'
return textVersion
},'')
}
function surveyToString (scrappedSurvey) {
return scrappedSurvey.reduce((textVersion, surveySection) => {
textVersion += surveySection.title
if (surveySection.description) {
textVersion += `\n${surveySection.description}`
}
textVersion += '\n\n'
if (surveySection.questions) {
textVersion += questionsToString(surveySection.questions)
}
textVersion += '\n\n'
return textVersion
}, '')
}
const survey = scrapSurvey()
const textVersion = surveyToString(survey)
console.log(textVersion)
})()
@Sterh20
Copy link

Sterh20 commented Jan 11, 2024

Does it work? I am getting nothing:

изображение

@AoDev
Copy link
Author

AoDev commented Jan 13, 2024

@Sterh20 I wrote this code in 2017. Probably, Google forms have changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment