-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
107 lines (90 loc) · 3.07 KB
/
Copy pathcontent.js
File metadata and controls
107 lines (90 loc) · 3.07 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
// Function to send message to background script to take screenshot
function requestScreenshot() {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: "takeScreenshot" }, (response) => {
if (response && response.success) {
resolve(response.dataUrl);
} else {
reject(response.error);
}
});
});
}
// Function to call the API with the base64 image data
function callDarkPatternDetectionAPI(base64Data) {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
base64_image: base64Data,
});
var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
return fetch(
"https://dark-dgir.onrender.com/detectDarkPattern",
requestOptions
)
.then((response) => response.text())
.then((result) => {
// Extracting DarkPattern value and Explanation from the respon
return result;
})
.catch((error) => error);
}
// Function to store data in the database
function storeInDatabase(url, explanation) {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"website-url": url,
"website-info": explanation,
});
var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
fetch("https://dark-database.onrender.com/api/websites", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log("error", error));
}
// Example of using the screenshot functionality
document.addEventListener("DOMContentLoaded", function () {
const screenshotButton = document.getElementById("screenshotButton");
const loadingIndicator = document.getElementById("loadingIndicator");
screenshotButton.addEventListener("click", async function () {
loadingIndicator.style.display = "block"; // Show loading spinner
try {
// Get the active tab URL
chrome.tabs.query({ active: true, currentWindow: true }, async function(tabs) {
var currentTab = tabs[0];
var url = currentTab.url;
// Request screenshot
try {
const dataUrl = await requestScreenshot();
var parts = dataUrl.split(',');
var base64Data = parts[1];
// Call the API with the base64 data
const result = await callDarkPatternDetectionAPI(base64Data);
// Hide the loading indicator immediately before showing the popup message
loadingIndicator.style.display = "none";
// Display the Explanation in an alert message
alert(result);
// Store in database if DarkPattern is 1
if (result[0] === '1') {
storeInDatabase(url, result);
}
} catch (error) {
alert("Error: " + error);
}
});
} catch (error) {
alert("Error: " + error);
}
});
});