CoolFace
Modelpublic

rise112/ai_forms

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
content.js67 linesDownload Raw Back to root
1// content.js
2
3async function autoFillForms() {
4  console.log("Auto-fill started.");
5  let questionElements = document.querySelectorAll('.office-form-question-title');
6  if (questionElements.length === 0) {
7    questionElements = document.querySelectorAll('.office-form-question-title-format-choice');
8  }
9  if (questionElements.length === 0) {
10    questionElements = document.querySelectorAll('[aria-labelledby*="question-title"]');
11  }
12
13  console.log("Question Elements:", questionElements);
14  const questions = Array.from(questionElements).map(el => el.textContent.trim());
15  console.log("Questions:", questions);
16
17  for (let i = 0; i < questions.length; i++) {
18    const question = questions[i];
19    try {
20      const answer = await getGoogleAnswer(question); // Use Google API here
21      if (answer) {
22        const inputFields = document.querySelectorAll('input[type="text"], textarea');
23        console.log("Input Fields:", inputFields);
24        if (inputFields[i]) {
25          console.log("Setting input field:", i, "to:", answer);
26          inputFields[i].value = answer;
27        } else {
28          const radioButtons = document.querySelectorAll('input[type="radio"]');
29          console.log("Radio Buttons:", radioButtons);
30          radioButtons.forEach(radio => {
31            console.log("Radio Label:", radio.parentElement.textContent);
32            if (radio.parentElement.textContent.includes(answer)) {
33              console.log("Checking Radio Button:", radio);
34              radio.checked = true;
35            }
36          });
37        }
38      }
39    } catch (error) {
40      console.error("Error fetching answer:", error);
41    }
42  }
43}
44
45async function getGoogleAnswer(question) {
46  console.log("Fetching Google answer for:", question);
47  const apiKey = "YOUR_GOOGLE_CUSTOM_SEARCH_API_KEY"; // Replace with your API key
48  const searchEngineId = "YOUR_GOOGLE_SEARCH_ENGINE_ID"; // Replace with your search engine ID
49  const apiUrl = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${searchEngineId}&q=${encodeURIComponent(question)}`;
50
51  try {
52    const response = await fetch(apiUrl);
53    const data = await response.json();
54    console.log("Google API Response:", data);
55
56    if (data.items && data.items.length > 0) {
57      return data.items[0].snippet || data.items[0].title; // Extract snippet or title
58    } else {
59      return "Answer not found on Google.";
60    }
61  } catch (error) {
62    console.error("Google API error:", error);
63    return "Error fetching from Google.";
64  }
65}
66
67window.addEventListener('load', autoFillForms);