-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
170 lines (147 loc) · 6.26 KB
/
Copy pathcontent_script.js
File metadata and controls
170 lines (147 loc) · 6.26 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let placeInfo;
// Function to create the "Analyze the review" option
function createAnalyzeButton(reviewText, rating, time) {
const analyzeButton = document.createElement('button');
analyzeButton.textContent = 'Analyze';
analyzeButton.style.cursor = 'pointer';
analyzeButton.style.padding = '6px 12px';
analyzeButton.style.fontSize = '14px';
analyzeButton.style.color = '#ffffff';
analyzeButton.style.backgroundColor = '#5fce72';
analyzeButton.style.border = 'none';
analyzeButton.style.borderRadius = '4px';
analyzeButton.style.marginLeft = 'auto';
analyzeButton.setAttribute('data-review-text', reviewText);
analyzeButton.setAttribute('data-rating', rating);
analyzeButton.setAttribute('data-time', time);
analyzeButton.addEventListener('click', () => {
const reviewText = analyzeButton.getAttribute('data-review-text');
const rating = analyzeButton.getAttribute('data-rating');
const time = analyzeButton.getAttribute('data-time');
analyzeReview(reviewText, rating, time, analyzeButton);
});
return analyzeButton;
}
// Function to create a loading animation
function createLoadingAnimation() {
const loadingContainer = document.createElement('div');
loadingContainer.style.display = 'flex';
loadingContainer.style.alignItems = 'center';
const spinner = document.createElement('div');
spinner.style.border = '4px solid #f3f3f3';
spinner.style.borderTop = '4px solid #3498db';
spinner.style.borderRadius = '50%';
spinner.style.width = '24px';
spinner.style.height = '24px';
spinner.style.animation = 'spin 2s linear infinite';
const loadingText = document.createElement('span');
loadingText.textContent = 'Analysis result loading...';
loadingText.style.marginLeft = '8px';
loadingText.style.animation = 'fadeinout 2s infinite';
const style = document.createElement('style');
style.innerHTML = `
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes fadeinout {
0%, 100% { opacity: 0; }
50% { opacity: 1; }
}
`;
document.head.appendChild(style);
loadingContainer.appendChild(spinner);
loadingContainer.appendChild(loadingText);
return loadingContainer;
}
// Function to create the popover container
function createPopoverContainer() {
const popover = document.createElement('div');
popover.style.position = 'absolute';
popover.style.backgroundColor = '#fff';
popover.style.border = '1px solid #ccc';
popover.style.borderRadius = '4px';
popover.style.padding = '16px';
popover.style.width = '400px';
popover.style.zIndex = '1000';
popover.style.display = 'none';
popover.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.1)';
const arrow = document.createElement('div');
arrow.style.position = 'absolute';
arrow.style.left = '10px';
arrow.style.top = '-10px';
arrow.style.border = '5px solid transparent';
arrow.style.borderBottomColor = '#ccc';
popover.appendChild(arrow);
return popover;
}
// Function to show the popover container with the analysis result
function showPopoverContainer(popover, analyzeButton, result, explanation) {
document.body.appendChild(popover);
let ratingColor = 'green';
if (result <= 3) ratingColor = 'red';
else if (result <= 6) ratingColor = 'orange';
popover.innerHTML += `
<h2 style="color: ${ratingColor}; margin-top: 0;">Reliability Rating: ${result}/10</h2>
<p>${explanation}</p>
<button style="margin-top: 10px; margin-bottom: 5px; cursor: pointer; padding: 6px 12px; font-size: 14px; color: #fff; background-color: #2196f3; border: none; border-radius: 4px;">Done</button>
<footer style="color: #636362;font-size: 10px;">Disclaimer: Since we do not have any information about the reviewer, we cannot be completely sure about their authenticity. It is also important to note that this is only one review and other reviews should also be considered for a more accurate assessment.</footer>
`;
popover.style.display = 'block';
popover.style.left = `400px`; // 400
popover.style.top = `100px`; // 100
const doneButton = popover.querySelector('button');
doneButton.addEventListener('click', () => {
popover.style.display = 'none';
});
}
// Function to analyze the review using OpenAI API
async function analyzeReview(reviewText, rating, time, analyzeButton) {
const popover = createPopoverContainer();
document.body.appendChild(popover);
popover.style.display = 'block';
const loadingAnimation = createLoadingAnimation();
popover.style.left = `400px`;
popover.style.top = `100px`;
popover.appendChild(loadingAnimation);
message = { action: 'analyze_review', place: placeInfo, text: reviewText, rating: rating, time: time };
console.log(message);
chrome.runtime.sendMessage(message, (response) => {
if (response.success) {
popover.remove();
const newpopover = createPopoverContainer();
console.log(response.result);
showPopoverContainer(newpopover, analyzeButton, response.result.rate, response.result.explanation);
} else {
popover.remove();
alert('Failed to analyze the review. Please try again later.');
}
});
}
// Function to add "Analyze the review" option to review menus
function addAnalyzeButtonToMenus() {
if (document.querySelector('div.TIHn2')) {
placeInfo = document.querySelector('div.TIHn2').outerText;
}
const reviewMenus = document.querySelectorAll('button.PP3Y3d.S1qRNe');
for (const menu of reviewMenus) {
let review = menu.parentNode.parentNode.parentNode.parentNode;
if (review) {
reviewText = review.querySelector('span.wiI7pd').outerText;
rating = review.querySelector('span.kvMYJc').ariaLabel;
time = review.querySelector('div.DU9Pgb').outerText.split('\n')[0];
if (!menu.previousElementSibling || !menu.previousElementSibling.classList.contains('analyze-review-button')) {
const analyzeButton = createAnalyzeButton(reviewText, rating, time);
let bar = review.querySelector('div.DU9Pgb');
const lastChild = bar.lastElementChild;
if (lastChild.tagName == 'BUTTON') {
bar.replaceChild(analyzeButton, lastChild);
} else {
bar.appendChild(analyzeButton);
}
}
}
}
}
// Periodically check for new review menus and add the "Analyze the review" option
setInterval(addAnalyzeButtonToMenus, 1000);