Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anmol #11

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"r.lsp.promptToInstall": false
}
2 changes: 1 addition & 1 deletion api/home/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

urlpatterns = [
path("", views.index, name="home"),

path("/popup_detect", views.popup_detect, name="popup_detect"),
]
1 change: 1 addition & 0 deletions api/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
def index(request):
return render(request, "index.html")

def popup_detect(request):
22 changes: 22 additions & 0 deletions api/mlApi/migrations/0002_darkpatternsdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.1 on 2024-01-20 09:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mlApi', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='DarkPatternsData',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('website_url', models.URLField()),
('dark_pattern_label', models.CharField(max_length=255)),
('dark_text', models.TextField()),
],
),
]
73 changes: 69 additions & 4 deletions cognigaurd-web/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
const sendWebsiteData = (dat , type) => {
const websiteData = {
img: dat,
type:type
// Add more data as needed
};

// Send data to API
fetch(apiUrl +"price-manipulation/", {

method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(websiteData),
})
.then(response => {
if (!response.ok) {
throw new Error("Error sending website data to API");
}
return response.json();
})
// .then(apiResponse => {
// console.log("API Response:", apiResponse);
// })
.catch(error => {
console.error("Error:", error);
});
};

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo,tab) {
if (changeInfo.status == 'complete' && !tab.url.startsWith('chrome://')) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['popup_detectV2.js']
});
}
});

let lastMessageTime = Date.now();

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.message === "open_popup") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {"message": "open_popup"});
});
}
}
);

const currentTime = Date.now();


if (currentTime - lastMessageTime < 3000) {
lastMessageTime = currentTime;
return; // Ignore the message if it's within 1 second of the previous message
}

lastMessageTime = currentTime;

if (request.message == "center popup" || request.message == "side popup") {
const tabId = sender.tab.id;

chrome.tabs.captureVisibleTab(null, {}, function (dataUrl){
console.log('Popup detected on ' + sender.tab.url);
sendWebsiteData(dataUrl, request.message);
});

// console.log('Popup count for tab ' + tabId + ': ' + popup_cnt[tabId]);
} else {
console.log(request.message);
sendWebsiteData(dataUrl, request.message);
}
});



4 changes: 3 additions & 1 deletion cognigaurd-web/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"activeTab",
"storage",
"tabs",
"notifications"
"scripting",
"notifications",
"activeTab"

],
"host_permissions": [
Expand Down
30 changes: 30 additions & 0 deletions cognigaurd-web/popup_detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
observers=new IntersectionObserver(function(entries){
entries.forEach(function(entry){
// console.log(entry);
if(entry.isIntersecting){
if(Math.abs(entry.boundingClientRect.left+(entry.boundingClientRect.width/2)- window.innerWidth/2) < 15 && Math.abs(entry.boundingClientRect.top+(entry.boundingClientRect.height/2) - window.innerHeight/2) < 15){
// console.log(entry)
}
}
});
},{threshold:1});

target=null;
document.querySelectorAll("*").forEach(function(target){
observers.observe(target);
});


console.log(document.getElementsByClassName("grid grid-cols-7 md:grid-cols-12 mx-auto h-full relative").getboundingClientRect);

// once finalizing a popup element eliminate the intersectionobserver then just observe it to reduce computataion
// add more filters to prevent false positive
// like : keyword matching in text
/*
z index
centering
close button
modal
background opacity

*/
44 changes: 44 additions & 0 deletions cognigaurd-web/popup_detectV2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
prev_node = "";

observer = new MutationObserver(function (mutations) {
mutations.forEach((mutation) => {
if (mutation.attributeName === "style") {
try {
let elems = document.querySelectorAll("." + mutation.target.className);

if (prev_node === mutation.target.className) return;

if (elems.length <= 30) {
Array.from(elems).forEach((elem) => {

temp = elem.getBoundingClientRect();

if (Math.abs(temp.left + temp.width / 2 - window.innerWidth / 2) <30 && Math.abs(temp.top + temp.height / 2 - window.innerHeight / 2) < 30) {
chrome.runtime.sendMessage({
message: "center popup"
});
return;
}

else {
chrome.runtime.sendMessage({
message: "side popup"
});

return;
}
});
}
}

catch (err) {}
prev_node = mutation.target.className;
}
});
});

observer.observe(document, {
attributes: true,
childList: true,
subtree: true,
});
16 changes: 16 additions & 0 deletions node_modules/.bin/browsers

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/browsers.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/browsers.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node_modules/.bin/escodegen

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/escodegen.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/escodegen.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node_modules/.bin/esgenerate

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/esgenerate.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading