-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
215 lines (184 loc) · 8.41 KB
/
popup.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
let updateInterval;
let defaultDiscription;
// request data from the active tab and update the popup when the popup is opened
document.addEventListener('DOMContentLoaded', function() {
requestActiveTabDataAndUpdateUI();
updateInterval = setInterval(requestActiveTabDataAndUpdateUI, 2500);
updateOnce();
});
// Clear the update interval when the popup is closed
window.addEventListener('unload', function() {
clearInterval(updateInterval);
});
function requestActiveTabDataAndUpdateUI() {
chrome.runtime.sendMessage({ action: 'getActiveTabData' }, (response) => {
if (response) {
updatePopup(response);
}
});
}
function updateOnce() {
chrome.runtime.sendMessage({ action: 'getActiveTabData'}, (response) => {
if (response) {
updatePopup(response);
const defaultDiv = document.getElementById('column1');
defaultDiscription = defaultDiv.querySelector('.column-description').textContent;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { default: defaultDiscription });
});
}
});
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
// console.log(message.countdown_value, message.malicious_link_count, message.prechecked_value, message.popup_value);
});
// Update popup function
function updatePopup(data) {
let countdown_value = data.countdown_value;
let malicious_link_value = data.malicious_link_count;
let prechecked_value = data.prechecked_value;
let popup_value = data.popup_value;
let stock_value = data.stock_value;
let image_value = data.image_value;
let darkpatterns = [
{name:'countdown', value: countdown_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/countdown'},
{name:'hidden info', value: malicious_link_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/hidden'},
{name:'preselected', value: prechecked_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/preselection'},
{name:'popup', value: popup_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/popup'},
{name:'stock', value: stock_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/toyemotion'},
{name:'image', value: image_value, url: 'https://infs3202-6844f4bb.uqcloud.net/7381/countdown'}
]
darkpatterns.sort((a, b) => b.value - a.value);
// updateColumnData function
function updateColumnData(columnId, title, description, url) {
let column = document.getElementById(columnId);
if(column) {
let columnTitle = column.querySelector('.column-title');
let columnDescription = column.querySelector('.column-description');
if(columnTitle) columnTitle.textContent = title;
if (columnDescription) columnDescription.textContent = description;
if (columnDescription) columnDescription.href = url;
// Set the height based on conditions for columns 0 to 2
let titleValue = parseInt(title, 10);
let columnHead = column.querySelector('.col-head');
let columnEnd = column.querySelector('.col-end');
if (parseInt(title, 10) === 0 || description === 'popup') {
columnHead.classList.add('no-click');
columnEnd.style.color = "#a0a0a0";
} else {
columnHead.classList.remove('no-click');
columnEnd.style.color = "#fff";
}
if(columnId === "column0" || columnId === "column1" || columnId === "column2") {
if(titleValue > 70) {
columnHead.style.height = '120px';
} else {
let height = 40 + 1 * titleValue;
columnHead.style.height = `${height}px`;
}
}
}
}
// loop through the darkpatterns array
darkpatterns.forEach((item, index) => {
let columnId = "column" + (index + 1);
updateColumnData(columnId, item.value, item.name, item.url);
});
const riskImageElement = document.getElementById('riskImage');
const riskElement = document.getElementById('riskLevel');
const colorElement = document.querySelector('.gradient-bg');
const riskDesp = document.getElementById('riskDesp');
// Set risk level
if ((countdown_value + malicious_link_value + prechecked_value + stock_value + image_value) > 50) {
riskImageElement.src = 'src/alien.png';
riskElement.textContent = "Dangerous";
riskDesp.textContent = "This website has been flagged as dangerous. It poses significant risks to your online safety.";
changeGradientBgColor('ff914d','ff3131');
riskElement.style.animation = 'breathe 1s infinite';
} else if ((countdown_value + malicious_link_value + prechecked_value + stock_value + image_value) > 25) {
riskImageElement.src = 'src/star.png';
riskElement.textContent = "Attention";
riskDesp.textContent = "This website has been evaluated as risky. Be mindful of sharing sensitive information.";
changeGradientBgColor('FFDE59', 'FF914D');
riskElement.style.animation = 'breathe 2s infinite';
}
}
// change gradient background color
function changeGradientBgColor(colorA,colorB) {
let style = document.createElement('style');
style.innerHTML = `
.gradient-bg {
background: linear-gradient(to bottom, #${colorA}, #${colorB}) !important;
}
`;
document.head.appendChild(style);
}
// add click event to toggle floating
document.addEventListener('DOMContentLoaded', () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { command: "toggleFloatingButton" });
});
});
document.addEventListener('DOMContentLoaded', function() {
// Column switching
const columns = document.querySelectorAll('.column-all');
const colHeads = document.querySelectorAll('.col-head');
colHeads.forEach(colHead => {
colHead.addEventListener('click', function() {
columns.forEach(column => column.classList.remove('gradient-bg'));
// column-all
let parentColumn = colHead.closest('.column-all');
if (parentColumn) {
parentColumn.classList.add('gradient-bg');
let description = parentColumn.querySelector('.column-description').textContent;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { type: description });
});
}
});
});
// Default to first column
columns[0].classList.add('gradient-bg');
// website link button
document.getElementById('website-button').href = `https://infs3202-6844f4bb.uqcloud.net/7381/`;
// document ai button
document.getElementById('docai').addEventListener('click', function() {
var notification = document.getElementById('ai-notify');
notification.textContent = 'Google document AI is analysing the website, pls wait.';
notification.style.backgroundColor = '#EE5622';
notification.style.display = 'block';
setTimeout(function() {
notification.style.display = 'none';
}, 1500); // hides after 1.5 sec
chrome.runtime.sendMessage({msg: "capture"}, async function(response) {
let img = response.imgSrc.split(',')[1];
console.log(img);
await fetch('http://localhost:8081/post/imgDetect', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
img64: img
})
})
.then(response => response.text())
.then(fullText => {
console.log(fullText);
analyseText(fullText);
notification.textContent = 'Analysis complete!';
notification.style.backgroundColor = '#4CAF50';
notification.style.display = 'block';
setTimeout(function() {
notification.style.display = 'none';
}, 1500); // hides after 1.5 sec
})
.catch(err => console.log(err));
});
});
});
function analyseText(fullText) {
let trimContents = fullText.split('Paragraph text:\n').filter(Boolean).map(s => s.trim());
let splitContents = trimContents[0].split('\n');
console.log(splitContents);
}