Fakultas Ilmu Komputer UI

Commit 5513dfa5 authored by wajdifarid's avatar wajdifarid
Browse files

Refactor code to use functions

parent 936fc8f5
......@@ -43,8 +43,8 @@ const assignmentsThisMonth = document.getElementsByClassName("day hasevent calen
var d = new Date();
const today = d.getDate();
var assignmentsYesterdayUrls = null
var assignmentsTodayUrls = null
var assignmentsYesterdayUrl = null
var assignmentsTodayUrl = null
var assignmentsNextWeekUrls = []
// put assignments to their timelines
......@@ -52,36 +52,59 @@ for (let index = 0; index < assignmentsThisMonth.length; index++) {
const element = assignmentsThisMonth[index];
assignmentDate = parseInt(element.children[0].textContent);
if ( assignmentDate === (today - 1)) {
assignmentsYesterdayUrls = element.children[0].href;
assignmentsYesterdayUrl = element.children[0].href;
} else if (assignmentDate === today) {
assignmentsTodayUrls = element.children[0].href;
assignmentsTodayUrl = element.children[0].href;
} else if (assignmentDate > today && assignmentDate < (today+7)) {
assignmentsNextWeekUrls.push(element.children[0].href);
}
}
// get assignment urls
var xhr = new XMLHttpRequest();
// get today's assignments
var assignments = null
var assignmentsTodayHTMLElement = document.createElement("ul");
xhr.onload = function() {
assignments = this.response.getElementsByClassName("box card-header clearfix");
assignments.forEach(element => {
assignmentsTodayHTMLElement.appendChild(htmlToAssignment(element));
var assignmentsTodayHTMLElement = null;
if (assignmentsTodayUrl) {
getAssignments(assignmentsTodayUrl, function (response) {
assignmentsTodayHTMLElement = response;
})
}
var assignmentsYesterdayHTMLElement = null;
if (assignmentsYesterdayUrl){
getAssignments(assignmentsYesterdayUrl, function (response) {
assignmentsYesterdayHTMLElement = response;
});
};
}
xhr.open('GET', today);
xhr.responseType = "document";
xhr.send();
var assignmentsNextWeekHTMLElement = document.createElement("div");
if (Array.isArray(assignmentsNextWeekUrls) && assignmentsNextWeekUrls.length) {
for (let index = 0; index < assignmentsNextWeekUrls.length; index++) {
const element = assignmentsNextWeekUrls[index];
getAssignments(element, function (response) {
assignmentsNextWeekHTMLElement.append(response);
});
}
}
// add listener to send assignments data to popup
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var todayResponse = "Alhamdulillah tidak ada tugas hari ini";
if (assignmentsTodayUrl) {
todayResponse = assignmentsTodayHTMLElement.outerHTML;
}
var yesterdayResponse = "Alhamdulillah kemarin tidak ada tugas";
if (assignmentsYesterdayUrl) {
yesterdayResponse = assignmentsYesterdayHTMLElement.outerHTML;
}
var nextWeekresponse = "Alhamdulillah minggu depan tidak ada tugas";
if (Array.isArray(assignmentsNextWeekUrls) && assignmentsNextWeekUrls.length) {
nextWeekResponse = assignmentsNextWeekHTMLElement.outerHTML;
}
sendResponse({
message: assignments[0].outerHTML,
assignmentsToday: assignmentsTodayHTMLElement.outerHTML,
assignmentsYesterday: "Kemaren",
assignmentsNextWeek: "Mingdep",
assignmentsToday: todayResponse,
assignmentsYesterday: yesterdayResponse,
assignmentsNextWeek: nextWeekResponse,
});
});
......@@ -19,4 +19,26 @@ function htmlToAssignment(assignment) {
assignment_li.append(document.createTextNode(course))
assignment_li.append(document.createTextNode(time))
return assignment_li
}
function getAssignments(url, callback){
var xhr = new XMLHttpRequest();
// get today's assignments
var assignments = null
var assignmentsHTMLElement = document.createElement("ul");
xhr.onload = function() {
assignments = this.response.getElementsByClassName("box card-header clearfix");
assignmentsDate = this.response.getElementsByClassName("current")[1];
assignmentsHTMLElement.appendChild(assignmentsDate);
for (let index = 0; index < assignments.length; index++) {
const element = assignments[index];
assignmentsHTMLElement.appendChild(element);
}
if (callback) {
callback(assignmentsHTMLElement);
}
};
xhr.open('GET', url);
xhr.responseType = "document";
xhr.send();
}
\ No newline at end of file
......@@ -20,6 +20,8 @@ $(document).ready(function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: "ready"}, function(response) {
// update popup.html
document.getElementById("cetakSini").innerHTML = response.message
document.getElementById("assignmentsToday").innerHTML = response.assignmentsToday;
document.getElementById("assignmentsYesterday").innerHTML = response.assignmentsYesterday;
document.getElementById("assignmentsNextWeek").innerHTML = response.assignmentsNextWeek;
});
});
......@@ -20,8 +20,7 @@
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<ul>
<li id="cetakSini"></li>
<ul id="assignmentsToday">
</ul>
</div>
</div>
......@@ -36,7 +35,7 @@
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
<ul id="assignmentsNextWeek"></ul>
</div>
</div>
</div>
......@@ -50,7 +49,7 @@
</div>
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
<ul id="assignmentsYesterday"></ul>
</div>
</div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment