-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.js
110 lines (97 loc) · 3.07 KB
/
content.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(function() {
let form = document.querySelector('form');
let formElements = form.querySelectorAll('*');
function decodeHtmlEntities(str) {
const element = document.createElement('div');
element.innerHTML = str;
return element.textContent;
}
function parseFormData(questions) {
// Your existing parseFormData function remains the same
return questions.map(question => {
try {
const formData = {
text: '',
description: '',
options: [],
imgUrl: question['img-url'],
imgText: null
};
question = decodeHtmlEntities(question['data-params']);
const textMatch = question.match(/^\%\.\@\.\[\d+,"([^"]+)"/);
if (textMatch) {
formData.text = textMatch[1];
}
const descriptionMatch = question.match(/^\%\.\@\.\[\d+,"[^"]+","([^"]+)"/);
if (descriptionMatch) {
formData.description = descriptionMatch[1];
}
const optionsSection = question.match(/\[\[\d+,\[(.*?)\]\]\]/);
if (optionsSection) {
const optionsMatches = optionsSection[1].split(/],\[/);
formData.options = optionsMatches.map(option => {
const match = option.match(/"([^"]+)"/);
return match ? match[1] : null;
}).filter(opt => opt !== null);
}
return formData;
} catch (error) {
console.error("Error parsing question:", error);
return {
text: '',
description: '',
options: [],
imgUrl: null,
imgText: null
};
}
});
}
const questions = [];
const listItems = form.querySelectorAll('div[role="listitem"]');
listItems.forEach(item => {
const data = {
'data-params': '',
'img-url': null,
}
const questionDiv = item.querySelector('div[data-params]');
const imgDiv = item.querySelector('img');
if(imgDiv) data['img-url'] = imgDiv.getAttribute('src');
if(questionDiv) data['data-params'] = questionDiv.getAttribute('data-params');
if (questionDiv || imgDiv) {
questions.push(data);
}
});
const parsedData = parseFormData(questions);
// const url = 'http://localhost:3000';
const url = "https://imagetotext-5y6r.onrender.com";
// Make API call and store response
fetch(url + "/api/gemini/content", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ question: parsedData }),
})
.then(response => response.json())
.then(data => {
console.log(data);
// Store the response in Chrome storage
const formUrl = window.location.href;
const storageData = {};
storageData[formUrl] = data;
chrome.storage.local.set(storageData);
// Send the data back to popup.js
chrome.runtime.sendMessage({
action: "apiResponse",
data
});
})
.catch(error => {
console.error("Error making API call:", error);
chrome.runtime.sendMessage({
action: "apiResponseError",
error: "Failed to fetch data. Please try again."
});
});
})();