Skip to content

Commit

Permalink
update popup login logout
Browse files Browse the repository at this point in the history
  • Loading branch information
khengyun committed May 13, 2023
1 parent 1246485 commit 5aabf4b
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 91 deletions.
60 changes: 31 additions & 29 deletions background_script/background.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
console.log("background.js");
importScripts("api.js");
importScripts("helpui.js");



function grade_teammates(params, privateCqId, classroomSessionId, groupId) {
console.log(params);
// console.log(params);

// console.log(
// format_grade_teammates(params, privateCqId, classroomSessionId, groupId)
// );


console.log(params);
// console.log(params);
const ans = post_api({
url: `https://fugw-edunext.fpt.edu.vn:${PORT}/api/v1/grade/grade-teammates`,
body: {"gradeTeammatesList":format_grade_teammates(params, privateCqId, classroomSessionId, groupId)},
body: {
gradeTeammatesList: format_grade_teammates(
params,
privateCqId,
classroomSessionId,
groupId
),
},
});
ans
.then((data) => {})
.then((data) => {
console.log(data)

})
.catch((e) => {
console.log(e);
});
}

function get_grade(params) {
// param = {"privateCqId":privateCqId,"sessionId":sessionId,"groupId":groupID}

const ans = post_api({
url: `https://fugw-edunext.fpt.edu.vn:${PORT}/api/v1/grade/get-grade`,
body: {
Expand All @@ -38,16 +43,17 @@ function get_grade(params) {
.then((data) => {
if (data.data.gradeResponseList.length != 0) {
// console.log(params.privateCqId, params.sessionId, params.groupId);
console.log(data.data);
// console.log(data.data);

privateCqId = params.privateCqId;
sessionId = params.sessionId;
groupId = params.groupId;
grade_teammates(
data.data.gradeResponseList,
privateCqId,sessionId, groupId
privateCqId,
sessionId,
groupId
);

}
})
.catch((e) => {
Expand Down Expand Up @@ -94,11 +100,10 @@ function list_group() {
// console.log(element3);

get_grade({
"privateCqId": privateCqId,
"sessionId": sessionId,
"groupId": groupID,
privateCqId: privateCqId,
sessionId: sessionId,
groupId: groupID,
});

}

// Iterate over the students in the current group.
Expand Down Expand Up @@ -213,24 +218,21 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
console.log(USER_INFOR);
subjects_in_the_semester("DEFAULT");

chrome.runtime.sendMessage({type: 'popup', message: "hello vietnam"}, function(response) {
console.log('Received response from background:', response);
});
// send mess from background script to popup script
messtopopup({ type: "background", message: USER_INFOR });
//send mess from background script to content script
messtocontent(details,token)


})
.catch((e) => {
console.log(e.message);
});
chrome.tabs.sendMessage(details.tabId, {
token: token,
details: details,
});

}
},
{
urls: ["https://fugw-edunext.fpt.edu.vn:8443/api/auth/token"],
urls: [`https://fugw-edunext.fpt.edu.vn:${PORT}/api/auth/token`],
},
["requestHeaders"]
);


107 changes: 72 additions & 35 deletions background_script/helpui.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
console.log("helpui.js")
function check_course_list(params) {
var boolean = false;

USER_COURSE.forEach(element => {
if (element.id == params)
{
boolean = true;
}
})
return boolean;
}

function check_course_list(params) {
var boolean = false;

USER_COURSE.forEach((element) => {
if (element.id == params) {
boolean = true;
}
});
return boolean;
}

// convert questions to json
// convert questions to json
function question_format(params) {
for (let i = 0; i < params.length; i++) {
const element = params[i];
params[i].questions = JSON.parse(element.questions);
}

for (let i = 0; i < params.length; i++) {
const element = params[i];
params[i].questions = JSON.parse(element.questions);
}

return params;
return params;
}


function check_group_user(params) {
var boolean = false;
params.listStudentByGroups.forEach((element) => {
Expand All @@ -42,24 +36,67 @@ function format_grade_teammates(
classroomSessionId,
groupId
) {
console.log(params);
console.log(privateCqId, classroomSessionId,groupId);
// console.log(params);
// console.log(privateCqId, classroomSessionId, groupId);
var teamlist = params;
var newParams = [];
console.log(params);

for (let index = 0; index < teamlist.length; index++) {
newParams.push({
"id": teamlist[index].id,
"hardWorking": 5,
"goodKnowledge": 5,
"teamWorking": 5,
"userIsGraded": teamlist[index].userIsGraded ? teamlist[index].userIsGraded : 0,
"groupId": groupId ? groupId : 0,
"privateCqId": privateCqId,
"classroomSessionId": teamlist[index].classroomSessionId ? teamlist[index].classroomSessionId : 0,
"userIsGradedId": teamlist[index].userIsGraded ? teamlist[index].userIsGraded : 0,
id: teamlist[index].id,
hardWorking: 5,
goodKnowledge: 5,
teamWorking: 5,
userIsGraded: teamlist[index].userIsGraded
? teamlist[index].userIsGraded
: 0,
groupId: groupId ? groupId : 0,
privateCqId: privateCqId,
classroomSessionId: teamlist[index].classroomSessionId
? teamlist[index].classroomSessionId
: 0,
userIsGradedId: teamlist[index].userIsGraded
? teamlist[index].userIsGraded
: 0,
});
};
}
// console.log(newParams);
return newParams;
}
}

// declare mess to post to popup script
function messtopopup(params) {
chrome.runtime.sendMessage({
message: params.message,
type: params.type,
});
}
// declare mess to post to popup script
function messtocontent(params, token) {
chrome.tabs.sendMessage(params.tabId, {
token: token,
details: params,
});
}


// background script listen message from popup script and content script
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request);
if(request.type == 'content_to_background') {

const ans = post_api({url: `https://fugw-edunext.fpt.edu.vn:${PORT}/api/auth/token`})
ans.then((data) => {
console.log(data)
messtopopup({type: "background", message: data.data})
})
}
if(request.type == 'popup_to_background') {

const ans = post_api({url: `https://fugw-edunext.fpt.edu.vn:${PORT}/api/auth/token`})
ans.then((data) => {
console.log(data)
messtopopup({type: "background", message: data.data})
})
}
})
18 changes: 12 additions & 6 deletions content/content.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
console.log("some thing here")
console.log("content script is running")
// Listen for messages from the content script
TOKEN = "123";

function logMessage(message) {
console.log(message)

}
function sendmess(params) {
console.log(params)
chrome.runtime.sendMessage(
params
)}



function logMessage(message) {
console.log(message)
token = localStorage.getItem("token")
console.log(`this is token: ${token}`)
sendmess({type: "content_to_background", data: token})
}

chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
Expand Down
33 changes: 31 additions & 2 deletions popup/active_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,37 @@ chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
})

let url = tabs[0].url;
if (!url.includes('https://fu-edunext.fpt.edu.vn/')) {
document.getElementById('container').innerHTML = '<div>Please Switch To <a href="https://fu.edunext.vn"> EDUNEXT </a> Tab To Edit Settings</div>'
console.log(tabs)
if (url == 'https://fu-edunext.fpt.edu.vn/login') {
localStorageAction({type: "set", key: "user_infor", value: null});
document.getElementById('container').innerHTML = '<div>Please Login To <a href="https://fu-edunext.fpt.edu.vn" > EDUNEXT </a></div>'
}

else {

chrome.runtime.sendMessage({
type: "popup_to_background"
})


// if (JSON.parse(localStorage.getItem("user_infor"))) {
// USER_INFOR = JSON.parse(localStorageAction({
// type: "get",
// key: "user_infor",
// }))

// document.getElementById(
// "container"
// ).innerHTML = `<p>Email: ${USER_INFOR.email} </br>
// Name: ${USER_INFOR.name} </br>
// RollNumber: ${USER_INFOR.rollNumber} </br>
// </p>`;
// } else {
// document.getElementById("container").innerHTML = '<div>Please Login To <a href="https://fu-edunext.fpt.edu.vn" > EDUNEXT </a></div>'
// ;
// }
}


});

9 changes: 1 addition & 8 deletions popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ <h4 class="title">AutoNext</h4>
</div>

<div class="container" id="container">
<div class="alert alert-warning" id="table">





</div>




</div>
Expand Down
59 changes: 48 additions & 11 deletions popup/popup.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
const version = chrome.runtime.getManifest().version
const version = chrome.runtime.getManifest().version;
const elmColors = document.getElementsByName("apply_button");
document.getElementById('version').innerText = `v${version}`
document.getElementById("version").innerText = `v${version}`;

var USER_INFOR = {};
// receive mess from background

function localStorageAction(params) {
if (params.type == "get") {
return localStorage.getItem(params.key);
}
if (params.type == "set") {
localStorage.setItem(params.key, JSON.stringify(params.value));
}
}

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.type === 'popup') {
// Handle the message from the popup script here
console.log('Received message from popup:', message);

// Send a response back to the popup script
sendResponse({type: 'background', message: USER_INFOR});
}
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === "background") {
console.log(request.message);
USER_INFOR = request.message;

//save to local storage
localStorageAction({ type: "set", key: "user_infor", value: USER_INFOR });
loadLocaltoPopup();

}
});

function loadLocaltoPopup(){
if (JSON.parse(localStorage.getItem("user_infor"))) {
USER_INFOR = JSON.parse(
localStorageAction({
type: "get",
key: "user_infor",
})
);

document.getElementById(
"container"
).innerHTML = `<p>Email: ${USER_INFOR.email} </br>
Name: ${USER_INFOR.name} </br>
RollNumber: ${USER_INFOR.rollNumber} </br>
</p>`;
} else {
document.getElementById("container").innerHTML =
'<div>Please Login To <a href="https://fu-edunext.fpt.edu.vn" > EDUNEXT </a></div>';
}
}

document.addEventListener("DOMContentLoaded", function () {
loadLocaltoPopup();
});

0 comments on commit 5aabf4b

Please sign in to comment.