-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
1921 lines (1742 loc) · 61.7 KB
/
background.js
File metadata and controls
1921 lines (1742 loc) · 61.7 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let startDate;
var likesDate;
var bgDate;
let endexp = false;
let userpid;
let group;
let change_bgcolor = false;
let change_bgcolor_condition2 = false;
let ifstartexp = false;
let activetime = 0;
let activetime_start_date = new Date().toLocaleDateString();
let survey;
let endDate;
const groupschoice = ['mlen','mley','mhen','mhey'];
/**
* chrome.storage API to store, retrieve, and track changes to user data.
*/
chrome.storage.local.get(
[
'userpid',
'group',
'change_bgcolor',
'change_bgcolor_condition2',
'ifstartexp',
'endexp',
'activetime',
'activetime_start_date',
'survey',
'startDate',
'endDate'
],
function (result) {
if (result.userpid === null || result.userpid === undefined) {
console.log('userpid has not been stored yet');
} else {
userpid = result.userpid;
}
if (result.group === null || result.group === undefined) {
console.log('group has not been stored yet');
} else {
group = result.group;
}
if (result.activetime === null || result.activetime === undefined) {
console.log('activetime has not been stored yet');
} else {
activetime = result.activetime;
}
if (result.survey === null || result.survey === undefined) {
console.log('survey has not been stored yet');
} else {
survey = result.survey;
}
if (result.activetime_start_date === null || result.activetime_start_date === undefined) {
console.log('activetime_start_date has not been stored yet');
} else {
activetime_start_date = result.activetime_start_date;
}
if (result.endexp === null || result.endexp === undefined) {
console.log('endexp has not been stored yet');
} else {
endexp = result.endexp;
}
if (result.startDate === null || result.startDate === undefined) {
console.log('startDate has not been stored yet');
} else {
startDate = new Date(result.startDate);
}
if (result.endDate === null || result.endDate === undefined) {
console.log('endDate has not been stored yet');
} else {
endDate = new Date(result.endDate);
}
if (
result.change_bgcolor === null ||
result.change_bgcolor === undefined
) {
console.log('change_bgcolor has not been stored yet');
} else {
change_bgcolor = result.change_bgcolor;
}
if (
result.change_bgcolor_condition2 === null ||
result.change_bgcolor_condition2 === undefined
) {
console.log('change_bgcolor_condition2 has not been stored yet');
} else {
change_bgcolor_condition2 = result.change_bgcolor_condition2;
}
if (
result.ifstartexp === null ||
result.ifstartexp === undefined
) {
console.log('ifstartexp has not been stored yet');
} else {
ifstartexp = result.ifstartexp;
}
}
);
/**
* Listens for tab URL updates. If the new URL contains "reddit.com" and is a post,
* it sends a message to invoke content scripts on that tab.
*/
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url) {
console.log("The tab URL has changed");
if (changeInfo.url.includes("reddit.com")) {
if (changeInfo.url.includes("/r/") || changeInfo.url.includes("/comments/")) {
console.log("The URL is a post and we want to call content js ");
chrome.tabs.sendMessage(tabId, { message: "run_my_code" }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
// Handle the error or perform other actions
} else {
// Process the response or perform other actions
}
});
}
else {
console.log("The URL is the Reddit home page");
}
}
}
});
/**
* Here, we are listening for incoming messages from the popup script
*
* When a message is received with a specific key ("send_userid_from_timerjs") and contains a userId:
* 1. The userId is stored in a local variable for immediate use.
* 2. The userId is also stored persistently using chrome.storage.local to ensure its availability across sessions.
* 3. The current tab is forcibly refreshed.
* 4. Additional functions (`insertdata` and `read_csv`) are invoked with the received userId.
*/
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.message === "send_userid_from_timerjs" && message.userId) {
// Do something with the user ID
userpid = message.userId.trim();
// store the userpid on local so it does not disappear later
chrome.storage.local.set({ userpid: userpid }, function () {
console.log('userpid stored successfully.');
});
group = groupschoice[Math.floor(Math.random() * groupschoice.length)];
// store the group condition on local so it does not disappear later
chrome.storage.local.set({ group: group }, function () {
console.log('group stored successfully.');
});
function forceRefreshTab(tabId) {
chrome.tabs.reload(tabId);
}
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs && tabs[0]) {
//document.documentElement.style.visibility = 'hidden';
forceRefreshTab(tabs[0].id);
}
});
insertdata(userpid.trim(), group.trim());
console.log(`Background Received user ID from timer js: ${message.userId}`);
}
});
//give all setup to content js when the experiment already started and user opened a new tab
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.message === "get_all_setup") {
sendResponse({
ifstartexp: ifstartexp
});
}
});
/**
* Initializes an experiment by setting start and end times, storing them in chrome.storage.
* Sends a message to the content script of the active tab to kick off the experiment.
*/
function setExp() {
startDate = new Date();
chrome.storage.local.set({ startDate: startDate.toString() }, function () {
console.log('startDate stored successfully.');
});
//add 5 seconds
endDate = new Date(startDate.getTime() + 60000);
ifstartexp = true;
chrome.storage.local.set({ ifstartexp: ifstartexp }, function () {
console.log('ifstartexp stored successfully.');
});
chrome.storage.local.set({ endDate: endDate.getTime() }, function () { });
// start the experiment(listening the upvote and downvote buttons)
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length === 0) {
console.error("No active tabs found");
return;
}
// Send a message to the content script
chrome.tabs.sendMessage(tabs[0].id, { message: "start experiment" }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
// Handle the error or perform other actions
} else {
// Process the response or perform other actions
}
});
});
}
/**
* function used to check the time, and see if the experiment has ended
* if the time is passed the limit for when the experiment ends
* 1. Set a variable endexp to true and store it in chrome.storage
* 2. A user will fill out the qualtrics survey and the extension will be uninstalled
*/
function checkTime() {
var now = new Date();
//console.log("endexp: ", endexp);
if (now > endDate && endexp === false) {
endexp = true;
chrome.storage.local.set({ endexp: endexp }, function () {
console.log('endexp stored successfully.');
});
console.log("exp ended from background");
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length === 0) {
console.error("No active tabs found");
return;
}
// Send a message to the content script
chrome.tabs.sendMessage(tabs[0].id, { message: "exp_ended" }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
// Handle the error or perform other actions
} else {
// Process the response or perform other actions
}
});
});
// Set the badge text
chrome.action.setBadgeText({ text: 'Click' });
// Set the badge background color
chrome.action.setBadgeBackgroundColor({ color: '#FF0000' });
clearInterval(intervalId); // Stop the interval after handling the event
}
}
const intervalDuration = 1000 * 60; // Check every minute. Adjust as needed.
const intervalId = setInterval(checkTime, intervalDuration);
const unintervalId = setInterval(uninstall, intervalDuration);
/**
* function used to uninstall the extension after the experiment has ended
*/
function uninstall() {
var now = new Date();
var newEndDate = new Date(endDate); // Clone the original date
newEndDate.setMinutes(newEndDate.getMinutes() + 30); // Adds 30 minutes
if (now > newEndDate) {
chrome.management.uninstallSelf({}, function () {
if (chrome.runtime.lastError) {
console.error("Error uninstalling:", chrome.runtime.lastError);
}
});
}
}
/*
* Listener for requests to retrieve the experiment's start time.
* Responds with the `startDate` when a "get_time" message is received.
*/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === "get_time") {
sendResponse({ value: startDate });
}
}
);
/*
* Listener for requests signaling the end of the experiment.
// Responds with the `endexp` value when an "end_exp" message is received.
*/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === "end_exp") {
sendResponse({ value: endexp });
}
}
);
/*
* // Listener for requests to retrieve the user's ID (uid).
// Responds with the `userpid` and logs the request when a "need_uid_from_backgroun" message is received.
*/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === "need_uid_from_background") {
sendResponse({ value: userpid });
console.log("recived request from timer js for uid: " + userpid);
}
}
);
// call background setExp function from timer.js this is send response
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === "call_function") {
setExp();
}
}
);
// open db as the function name
function openDB() {
openRequest.onsuccess = function () {
let db = openRequest.result;
console.log('Successfully opened database');
// Your database operations here
};
openRequest.onerror = function (error) {
console.error('Failed to open database:', error);
};
}
// Listen for messages from the content script and send back userid
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'getGroup') {
// reply immediately with the current `group`
sendResponse({ group });
// no `return true` needed here because sendResponse is sync
}
});
// Listen for messages from the content script for insert data into database
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.message === "voteComment") {
console.log("Received data from content script: ", request.data);
insertUserVoteComments(userpid, request.data.action, request.data.comment, request.data.post);
sendResponse({ message: "voteCommentSuccess" });
} else if (request.message === "replyPost") {
console.log("Received data from content script: ", request.data);
insertUserReplyPosts(userpid, request.data.content, request.data.post, request.data.like, request.data.time);
sendResponse({ message: "replyPostSuccess" });
} else if (request.message === "votePost") {
console.log("Received data from content script: ", request.data);
insertUserVotePosts(userpid, request.data.action, request.data.post);
sendResponse({ message: "votePostSuccess" });
} else if (request.message === "replyComment") {
insertUserReplyComments(userpid, request.data.content, request.data.comment, request.data.post);
sendResponse({ message: "replyCommentSuccess" });
} else if (request.message === "updateViewedPost") {
console.log("Received data from content script: ", request.data);
updateUserViewedPost(userpid, request.data.post_url);
sendResponse({ message: "updateViewedPostSuccess" });
}
else if (request.message === "updateUserVoteFakeComment") {
console.log("Received data for fake comment from content script: ", request.data);
// Call function to update vote on fake comment
updateUserVoteOnFakeComment(userpid, request.data.useraction, request.data.action_fake_comment, request.data.action_fake_post);
sendResponse({ message: "updateUserVoteFakeComment" });
}
else if (request.message === "updateUserVoteFakePost") {
console.log("Received data for fake post from content script: ", request.data);
// Call function to update vote on fake post
updateUserVoteOnFakePost(userpid, request.data.useraction, request.data.fakePostId);
sendResponse({ message: "updateUserVoteFakePost" });
}
else if (request.message === "deleteUserVoteFakeComment") {
console.log("Received data for fake comment from content script: ", request.data);
// Call function to delete vote on fake comment
deleteUserVoteOnFakeComment(userpid, request.data.action_fake_comment, request.data.action_fake_post);
sendResponse({ message: "deleteUserVoteFakeComment" });
}
else if (request.message === "deleteUserVoteFakePost") {
console.log("Received data for fake post from content script: ", request.data);
// Call function to delete vote on fake post
deleteUserVoteOnFakePost(userpid, request.data.fakePostId);
sendResponse({ message: "deleteUserVoteFakePost" });
}
else if (request.message === "insertUserReplyFakeComments") {
console.log("Received data to insert user reply into fake comments:", request);
// Process the variables received from the content script
insertUserReplyFakeComments(userpid, request.commentId, request.commentContent, request.fakePostId);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}
else if (request.message === "deleteUserReplyFakeComment") {
console.log("Received data to delete user reply on fake comment:", request.data);
// Call function to delete user reply on fake comment
deleteUserReplyOnFakeComment(
userpid, // User ID
request.data.replyTo, // Fake comment ID being replied to
request.data.replyFakePost, // The post that contains the fake comment
request.data.replyContent // The reply content
);
sendResponse({ message: "deleteUserReplyFakeComment" });
}
else if (request.message === "insertUserReplyToFakePost") {
console.log("Received data to insert user reply into fake post:", request);
// Call the function to send the user's reply to the fake post
sendUserReplyToFakePost(userpid, request.fakePostId, request.replyContent);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}
else if (request.message === "removeUserReplyFromFakePost") {
console.log("Received request to remove user reply from fake posts:", request);
// Process the variables received from the content script
removeUserReplyFromFakePost(userpid, request.fakePostId, request.replyContent);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}else if (request.message === "removeUserVoteFromPost") {
console.log("Received request to remove user vote from real post:", request);
// Process the variables received from the content script
deleteUserVoteOnPost(userpid, request.postId);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}else if (request.message === "addUserVoteToPost") {
console.log("Received request to add user vote to real post:", request);
// Process the variables received from the content script
updateUserVoteOnPost(userpid, request.userAction, request.postId);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}else if (request.message === "removeUserVoteFromComment") {
console.log("Received request to remove user vote from real comment:", request);
// Process the variables received from the content script
deleteUserVoteOnComment(userpid, request.commentId, request.postId);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}else if (request.message === "addUserVoteToComment") {
console.log("Received request to add user vote to real comment:", request);
// Process the variables received from the content script
updateUserVoteOnComment(userpid, request.userAction,request.commentId,request.postId);
// Send a response back to the content script confirming success
sendResponse({ success: true });
}
else if (request.message === "deleteUserReplyRealComment") {
console.log("Received data to delete user reply on real comment:", request.data);
// Call function to delete user reply on real comment
deleteUserReplyOnComment(
userpid, // User ID
request.data.replyTo.trim(), // Real comment ID being replied to
request.data.replyPost, // The post that contains the real comment
request.data.replyContent.trim() // The reply content
);
sendResponse({ message: "deleteUserReplyRealComment" });
}
else if (request.message === "addUserReplyRealComment") {
console.log("Received data to add user reply on real comment:", request.data);
// Call function to add user reply on real comment
updateUserReplyOnComment(
userpid, // User ID
request.data.replyContent.trim(), // The content of the reply
request.data.replyTo.trim(), // Real comment ID being replied to
request.data.replyPost // The post that contains the real comment
);
sendResponse({ message: "addUserReplyRealComment" });
}
else if (request.message === "addUserReplyRealPost") {
console.log("Received data to add user reply on real post:", request.data);
// Call function to add user reply on real post
updateUserReplyOnPost(
userpid, // User ID
request.data.replyContent.trim(), // The content of the reply
request.data.replyPost // The post ID
);
sendResponse({ message: "addUserReplyRealPost" });
}
else if (request.message === "deleteUserReplyRealPost") {
console.log("Received data to delete user reply on real post:", request.data);
// Call function to delete user reply on real post
deleteUserReplyOnPost(
userpid, // User ID
request.data.replyPost, // The post ID
request.data.replyContent.trim() // The reply content to delete
);
sendResponse({ message: "deleteUserReplyRealPost" });
}
});
/**
* Sends a POST request to insert user-specific data into a remote database.
* The data includes actions like commenting on posts and even comments,
*
* @param {string} uid - The unique identifier of the user.
*/
function insertdata(uid,group) {
//var insert_date= new Date();
fetch("https://outerlimits.onrender.com/api/insert", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
usergroup: group,
userInteractions: {
votes: {
onPosts: [], // Votes on real posts
onComments: [], // Votes on real comments
onFakePosts: [], // Votes on fake posts
onFakeComments: [] // Votes on fake comments
},
replies: {
onPosts: [], // Replies to real posts
onComments: [], // Replies to real comments
onFakePosts: [], // Replies to fake posts
onFakeComments: [] // Replies to fake comments
}
},
browser_history: [], // Browser history
active_onReddit: [], // Reddit activity log
surveypopup_selections: [], // Survey popup selections
viewed_posts: [] // List of viewed posts
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert data");
}
})
.then(data => {
console.log("Data inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Send a POST request to log user's voting actions on Reddit comments into the database.
* The data includes the user's action, the comment they interacted with, and the post.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} action - The type of vote action the user performed (e.g., upvote or downvote).
* @param {string} comment - the contents of the comment the user voted on.
* @param {string} post - the post the user has just interacted with.
*/
function insertUserVoteComments(uid, action, comment, post) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserVote_onComments", { // Updated API route
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
user_vote_onComments: [{
action_date: insert_date,
user_action: action,
action_comment: comment,
action_post: post
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert user vote on comments ");
}
})
.then(data => {
console.log("User vote on comments inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Sends a POST request to log details of user's interactions with fake comments on Reddit into a remote database.
* The data encompasses the fake comment details such as its ID, the user who made the comment, its content,
* and its associated post URL, among other attributes.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} comment_id - The unique ID of the fake comment.
* @param {string} user_name - The username who made the comment.
* @param {string} comment_content - Content of the fake comment.
* @param {number} insert_index - The position/index where the comment should be inserted.
* @param {string} post_url - The URL of the associated Reddit post.
* @param {number} like - Number of likes/upvotes for the comment.
* @param {Date} time - Time when the comment was made.
*/
function insertFakeComments(uid, comment_id, user_name, comment_content, insert_index, post_url, like, time, profile) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateuserFakeComment_infakepost", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
user_comment_in_fake_post
: [{
fake_comment_id: comment_id,
user_name: user_name,
content: comment_content,
where_to_insert: insert_index,
post_url: post_url,
like: like,
time: insert_date,
profile: profile
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert fake comments on comments ");
}
})
.then(data => {
console.log("fake comments inserted successfully:", data);
})
.catch(error => {
console.error('Error:', error);
// Check if the error has a response
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response text:', error.response.statusText);
} else {
console.error('No response from server.');
}
});
}
/**
* Sends a POST request to record details of fake Reddit posts into a remote database.
* This function captures and logs various attributes of the fake post such as its URL, title,
* content, associated image, and the position or index where it appears. This aids in tracking
* user interactions with artificially injected content on Reddit.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} fakepost_url - The URL of the fake Reddit post.
* @param {number} fakepost_index - The index on the HTML where the post should appear.
* @param {string} fakepost_title - The title of the fake post.
* @param {string} fakepost_content - Content or body of the fake post.
* @param {string} fakepost_image - The image associated with the fake post.
*/
function insertFakePosts(uid, fakepost_url, fakepost_index, fakepost_title, fakepost_content, fakepost_image) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateFakePost", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
fake_post: [{
fakepost_url: fakepost_url,
fakepost_index: fakepost_index,
fakepost_title: fakepost_title,
fakepost_content: fakepost_content,
fakepost_image: fakepost_image
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert fake post");
}
})
.then(data => {
console.log("Fake post inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Add a listener for messages from the content script to insert fake posts into the database.
*/
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.message === "insert user reply in fake comments to db") {
// Process the variables received from the content script
insertUserReplyFakeComments(userpid, request.commentId, request.commentContent);
alert("profle :", request.profile);
// Send a response back to the content script if needed
sendResponse({ success: true });
}
});
/**
* Sends a POST request to record user replies to fake comments on Reddit into a remote database.
* This function captures and logs details of the user's interaction with a fake comment,
* tracking attributes such as the comment's ID, the Reddit username of the user, the content
* of their reply, whether they liked the comment, and the time of the interaction.
* It aids in analyzing user reactions to artificially injected comments on Reddit.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} comment_id - The ID of the fake comment the user replied to.
* @param {string} userRedditName - The Reddit username of the user.
* @param {string} comment_content - The content of the user's reply.
* @param {boolean} like - Indicates if the user liked the fake comment or not.
*/
function insertUserReplyFakeComments(uid, comment_id, comment_content, fake_post_id) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserReply_onFakeComments", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
user_reply_onFakeComments: [{
reply_to: comment_id, // The fake comment being replied to
reply_content: comment_content, // The user's reply content
reply_fake_post: fake_post_id, // The fake post ID
action_date: insert_date // Capture the current timestamp
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert user reply to fake comments");
}
})
.then(data => {
console.log("User reply to fake comments inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Sends a POST request to record users' replies to posts, tracking attributes such as the
* user's unique ID, the ID of the post they replied to, the content of their reply, whether
* they liked the post, and the time of the interaction.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} content - The content of the user's reply.
* @param {string} post - The ID of the post the user replied to.
* @param {boolean} like - Indicates if the user liked the post or not.
* @param {Date} time - The time of the interaction.
*/
function insertUserReplyPosts(uid, content, post, like, time) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserReply_Posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
user_reply_onPosts: [{
action_date: insert_date,
reply_content: content,
reply_post: post,
like: like,
time: time
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert user vote on comments ");
}
})
.then(data => {
console.log("User vote on comments inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Sends a POST request to recteord a user's vo on a post. It tracks attributes such as the
* user's ID, the action they took (e.g., upvote, downvote), and the ID of the post
* they voted on.
*
* @param {string} uid - The unique identifier of the user.
* @param {string} action - The action the user took (e.g., "upvote", "downvote").
* @param {string} post - The ID of the post the user voted on.
*/
function insertUserVotePosts(uid, action, post) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserVote_Posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: uid,
user_vote_onPosts: [{
action_date: insert_date,
user_action: action,
action_post: post
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to insert user vote on Posts");
}
})
.then(data => {
console.log("User vote on posts inserted successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Sends a POST request to recteord a user's vo on a post. It tracks attributes such as the
* user's ID, the action they took (e.g., upvote, downvote), and the ID of the post
* they voted on.
*
* @param {string} userid - The unique identifier of the user.
* @param {string} useraction - The action the user took.
* @param {string} fakeContent - The content which the user is placing a fake vote on.
*/
function updateUserVoteOnFakePost(userid, useraction, fakePostId) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserVote_onFakePosts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: userid,
user_vote_onFakePosts: [{
action_date: insert_date,
user_action: useraction,
action_fake_post: fakePostId
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to update user vote on fake post");
}
})
.then(data => {
console.log("User vote on fake post updated successfully:", data);
})
.catch(error => {
console.error(error);
});
}
function updateUserVoteOnFakeComment(userid, useraction, fakeCommentId, action_fake_post) {
var insert_date = new Date();
fetch("https://outerlimits.onrender.com/api/updateUserVote_onFakeComments", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: userid,
user_vote_onFakeComments: [{
action_date: insert_date,
user_action: useraction,
action_fake_comment: fakeCommentId,
action_fake_post: action_fake_post
}]
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to update user vote on fake comment");
}
})
.then(data => {
console.log("User vote on fake comment updated successfully:", data);
})
.catch(error => {
console.error(error);
});
}
/**
* Sends a POST request to record a user's removal of a vote entirely on a post. It tracks attributes such as the
* user's ID, the action they took, and the ID of the post
* they voted on.
*
* @param {string} userid - The unique identifier of the user.
* @param {string} fakeContent - The fake content which the user is removing a vote on.
*/
function deleteUserReplyOnFakeComment(userid, replyTo, replyFakePost, replyContent) {
fetch("https://outerlimits.onrender.com/api/removeUserReply_onFakeComments", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: userid,
reply_to: replyTo, // Fake comment the user replied to
reply_fake_post: replyFakePost, // The fake post containing the comment
reply_content: replyContent // The content of the reply
})
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Failed to delete user reply on fake comment");
}
})
.then(data => {
console.log("User reply on fake comment deleted successfully:", data);
})
.catch(error => {
console.error("Error deleting user reply on fake comment:", error);
});
}
function deleteUserVoteOnFakePost(userid, fakePostId) {
fetch("https://outerlimits.onrender.com/api/removeUserVote_onFakePosts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userid: userid,
action_fake_post: fakePostId
})
})