-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
178 lines (153 loc) · 4.3 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
//Script che viene eseguito nel momento in cui viene aperta l'estensione
console.log("Esecuzione Popup Script");
/*
!TODO Implementare funzionalità frecce nella lista (<) (>)
!TODO Implementare cambiamento focus allo scorrimento della lista
!TODO Implementare Aggiornamento numero DP nella lista (#)
*/
const listContent = $(".list__index");
const listDecrement = $("#list__dec");
const listIncrement = $("#list__inc");
const switchStatus = $("#switch__status");
const darkPattern_Type = $("#DP_Type");
const currentWebsite = $("#CUR__Website");
let currentIndex = 1;
let maxIndex = 1;
//Retrieving all the elements of DB at the start
retrieveAllDatabase();
updateTextList();
//Updating Database when switch change status
switchStatus.on("change", () => {
console.log("Stato Switch " + switchStatus.is(":checked"));
chrome.runtime.sendMessage({
message: "update",
payload: {
name: "switchValue",
value: switchStatus.is(":checked"),
},
});
chrome.runtime.sendMessage({
message: "content__retrieve",
payload: "switchValue",
});
});
listIncrement.on("click", (e) => {
e.preventDefault();
nextItemList();
});
listDecrement.on("click", (e) => {
e.preventDefault();
previousItemList();
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "delete_success") {
//Delete Query
} else if (request.message === "update_success") {
//Update Query
retrieveAllDatabase();
}
if (request.message.includes("retrieve_success")) {
//Retrieve Query
if (request.message.includes("switchValue")) {
// console.log("[DEBUG] " + request.payload.value);
$("#switch__status").prop("checked", request.payload.value);
}
if (request.message.includes("numDarkPatternIdentified")) {
updateCounterList(request.payload.value);
}
}
});
// Functions for the list management
function updateCounterList(numDarkPatternIdentified) {
if (numDarkPatternIdentified >= 0) {
maxIndex = numDarkPatternIdentified;
currentIndex = 0;
updateTextList();
}
}
//Functions for the scrolling of the list
function nextItemList() {
//Click (>): Change Focus on the object
//Send a message to content to change focus
if (switchStatus.is(":checked")) {
if (currentIndex + 1 > maxIndex) {
currentIndex = 0;
} else {
currentIndex = currentIndex + 1;
}
console.log("Current " + currentIndex);
scrollToElement(currentIndex);
updateTextList();
}
//Send Message to Content with new value [currentIndex]
}
function previousItemList() {
//Click (<): Change Focus on the object
//Send a message to content to change focus
if (switchStatus.is(":checked")) {
if (currentIndex - 1 < 0) {
currentIndex = maxIndex;
} else {
currentIndex = currentIndex - 1;
}
console.log("Current " + currentIndex);
scrollToElement(currentIndex);
updateTextList();
}
}
function updateTextList() {
if (switchStatus.is(":checked")) {
let newString = currentIndex + " out of " + maxIndex;
listContent.text(newString);
darkPattern_Type.text("Hidden Information");
//Update Current Site URL
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
let url = tabs[0].url;
var page = url.substring(0, url.indexOf("/", 9) + 1);
console.log(url);
console.log(page);
currentWebsite.text(page);
});
} else {
listContent.text("Activate Switch to track DP");
darkPattern_Type.text(" ");
currentWebsite.text("...");
}
}
function sendMessageContent() {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {
message: "retrieve_success " + request.payload,
payload: res,
});
});
}
function retrieveAllDatabase() {
chrome.runtime.sendMessage({
message: "retrieve",
payload: "switchValue",
});
chrome.runtime.sendMessage({
message: "retrieve",
payload: "numDarkPatternIdentified",
});
chrome.runtime.sendMessage({
message: "retrieve",
payload: "darkPatternIdentified",
});
}
function testPrint() {
console.log("Max: " + maxIndex);
console.log("current" + currentIndex);
}
function scrollToElement(element) {
console.log("TEST " + element);
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {
message: "scrollTo " + element,
payload: element,
});
});
}