Skip to content

Commit

Permalink
chore: format files, reduce permissions and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
harsilspatel committed Apr 2, 2020
1 parent c51bba6 commit aa016fc
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 305 deletions.
6 changes: 2 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Moodle Downloader",
"description": "A chrome extension for downloading Moodle resources",
"version": "1.4.8",
"version": "1.5.0",
"icons": {
"16": "assets/icon16.png",
"24": "assets/icon24.png",
Expand All @@ -20,8 +20,6 @@
"permissions": [
"activeTab",
"downloads",
"storage",
"https://*/",
"http://*/"
"storage"
]
}
90 changes: 60 additions & 30 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// but getting the window object is hard.
// Instead, we can grab the session key from the logout button.
// Note that var is used here as this script can be executed multiple times.
var sesskey = (
new URL(document.querySelector("a[href*='login/logout.php']").href)
var sesskey = new URL(
document.querySelector("a[href*='login/logout.php']").href
).searchParams.get("sesskey");

function getDownloadOptions(url) {
Expand All @@ -29,7 +29,8 @@ function getDownloadOptions(url) {
const downloadUrl =
urlObj.origin +
urlObj.pathname.slice(undefined, urlObj.pathname.lastIndexOf("/")) +
"/download_folder.php?id=" + id;
"/download_folder.php?id=" +
id;
return {
url: downloadUrl,
method: "POST",
Expand All @@ -46,50 +47,79 @@ function getDownloadOptions(url) {
var SUPPORTED_FILES = new Set(["File", "Folder", "URL", "Page"]);

function getFilesUnderSection() {
return Array.from(document.getElementsByClassName('content'))
return Array.from(document.getElementsByClassName("content"))
.map(content => {
const sectionEl = content.querySelector("h3.sectionname");
if (!sectionEl)
return [];
if (!sectionEl) return [];
const section = sectionEl.textContent.trim();
return Array.from(content.getElementsByClassName("activity"))
.map(activity => ({
instanceName: activity.getElementsByClassName("instancename")[0],
archorTag: activity.getElementsByTagName("a")[0],
})).filter(({ instanceName, archorTag }) =>
instanceName !== undefined && archorTag !== undefined
).map(({ instanceName, archorTag }) => ({
instanceName: activity.getElementsByClassName(
"instancename"
)[0],
archorTag: activity.getElementsByTagName("a")[0]
}))
.filter(
({ instanceName, archorTag }) =>
instanceName !== undefined && archorTag !== undefined
)
.map(({ instanceName, archorTag }) => ({
name: instanceName.firstChild.textContent.trim(),
downloadOptions: getDownloadOptions(archorTag.href),
type: instanceName.lastChild.textContent.trim(),
section: section
})).filter(activity => SUPPORTED_FILES.has(activity.type));
}).reduce((x, y) => x.concat(y), []);
}))
.filter(activity => SUPPORTED_FILES.has(activity.type));
})
.reduce((x, y) => x.concat(y), []);
}

function getFilesUnderResources(tableBody) {
return Array.from(tableBody.children) // to get files under Resources tab
.filter(resource => resource.getElementsByTagName('img').length != 0)
.map(resource => (resource = {
name: resource.getElementsByTagName('a')[0].textContent.trim(),
downloadOptions: getDownloadOptions(resource.getElementsByTagName('a')[0].href),
type: resource.getElementsByTagName('img')[0]['alt'].trim(),
section: resource.getElementsByTagName('td')[0].textContent.trim()}))
.map((resource, index, array) => {
resource.section =
resource.section ||
(array[index-1] && array[index-1].section) ||
"";
return resource})
.filter(resource => SUPPORTED_FILES.has(resource.type))
.filter(resource => resource.getElementsByTagName("img").length != 0)
.map(
resource =>
(resource = {
name: resource
.getElementsByTagName("a")[0]
.textContent.trim(),
downloadOptions: getDownloadOptions(
resource.getElementsByTagName("a")[0].href
),
type: resource.getElementsByTagName("img")[0]["alt"].trim(),
section: resource
.getElementsByTagName("td")[0]
.textContent.trim()
})
)
.map((resource, index, array) => {
resource.section =
resource.section ||
(array[index - 1] && array[index - 1].section) ||
"";
return resource;
})
.filter(resource => SUPPORTED_FILES.has(resource.type));
}

function getFiles() {
const courseName = document.getElementsByTagName('h1')[0].innerText || document.getElementsByClassName('breadcrumb-item')[2].firstElementChild.title || document.querySelector("header#page-header .header-title").textContent.trim() || "";
const courseName =
document.getElementsByTagName("h1")[0].innerText ||
document.getElementsByClassName("breadcrumb-item")[2].firstElementChild
.title ||
document
.querySelector("header#page-header .header-title")
.textContent.trim() ||
"";

const tableBody = document.querySelector("div[role='main'] > table.generaltable.mod_index > tbody");
const allFiles = tableBody === null ? getFilesUnderSection() : getFilesUnderResources(tableBody);
allFiles.forEach(file => file.course = courseName);
const tableBody = document.querySelector(
"div[role='main'] > table.generaltable.mod_index > tbody"
);
const allFiles =
tableBody === null
? getFilesUnderSection()
: getFilesUnderResources(tableBody);
allFiles.forEach(file => (file.course = courseName));
console.log(allFiles);
return allFiles;
}
Expand Down
103 changes: 48 additions & 55 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,66 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<script src="./popup.js"></script>
</head>
<body>
<!-- <style>
.demo-card-wide.mdl-card {
background: rgb(231, 242, 255);
}
</style> -->


<div class="demo-card-wide mdl-card mdl-shadow--2dp">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">moodleDownloader 📚</h2>
</div>
</div>

<div class="mdl-card__actions mdl-card--border">

<small style="font-size:75%; padding-left: 1%;">Click and drag or use ⌘ key to select multiple options:</small>
<div style="padding-left: 1%">
<div class="mdl-textfield mdl-js-textfield">
<input style="font-size:75%;" class="mdl-textfield__input" type="text" id="search">
<label style="font-size:75%;" class="mdl-textfield__label" for="search">search... (regex supported)</label>
</div>
</div>
<select multiple class="mdl-textfield__input" id="resourceSelector">
<div style="padding-left: 1%">
<div class="mdl-textfield mdl-js-textfield">
<input style="font-size:75%;" class="mdl-textfield__input" type="text" id="search">
<label style="font-size:75%;" class="mdl-textfield__label" for="search">search... (regex supported)</label>
</div>
</div>
<select multiple class="mdl-textfield__input" id="resourceSelector">
</select>
<br>
<div style="padding-left: 1%;" id=footer>
<div style="zoom: .9">
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="organize">
<input type="checkbox" id="organize" class="mdl-checkbox__input" checked>
<span style="font-size:75%;" class="mdl-checkbox__label">Organize into folder (experimental)</span>
</label>
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="replaceFilename">
<input type="checkbox" id="replaceFilename" class="mdl-checkbox__input">
<span style="font-size:75%;" class="mdl-checkbox__label">Replace filename with Moodle name</span>
</label>
<br>
<div style="padding-left: 1%;" id=footer>
<div style="zoom: .9">
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="organize">
<input type="checkbox" id="organize" class="mdl-checkbox__input" checked>
<span style="font-size:75%;" class="mdl-checkbox__label">Organize into folder (experimental)</span>
</label>
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="replaceFilename">
<input type="checkbox" id="replaceFilename" class="mdl-checkbox__input">
<span style="font-size:75%;" class="mdl-checkbox__label">Replace filename with Moodle name</span>
</label>

<label for="resourcesSection" >
<br>
<br>
<span style="font-size:75%;">Hint: Head to the Resouces to download all resources at once!</span>
<br>
<br>
</label>
<label for="resourcesSection" >
<br>
<br>
<span style="font-size:75%;">Hint: Head to the Resouces to download all resources at once!</span>
<br>
<br>
</label>

<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored" id="downloadResources">Download</button>
<a href="">
<div id="shareLink" class="icon material-icons">share</div>
</a>
<div style="display: inline;">
<a id="sourceCode" href="https://github.com/harsilspatel/moodleDownloader">
<img src="../assets/github120.png" alt="Souce code" align="right" style="width: 9%; padding-right: 2%;">
</a>
</div>
</div>
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored" id="downloadResources">Download</button>
<a href="">
<div id="shareLink" class="icon material-icons">share</div>
</a>
<div style="display: inline;">
<a id="sourceCode" href="https://github.com/harsilspatel/moodleDownloader">
<img src="../assets/github120.png" alt="Souce code" align="right" style="width: 9%; padding-right: 2%;">
</a>
</div>
</div>

<div hidden='hidden' id=feedbackDiv>
<br>
<small id=feedbackPrompt>You just downloaded 50th file using this tool! 🎉 <br/> <br/> I have spent more than <b>30 hours</b> to learn & develop this extension, could you please rate my efforts? 😬</small>
<button class="mdl-button mdl-js-button mdl-button--accent" id=sure>
Sure! 😎
</button>
<button style='position:absolute; right: 5%;' class="mdl-button mdl-js-button mdl-button--primary" id=nah>
Nah! 😒
</button>
</div>
</div>
<div hidden='hidden' id=feedbackDiv>
<br>
<small id=feedbackPrompt>You just downloaded 50th file using this tool! 🎉 <br/> <br/> I have spent more than <b>30 hours</b> to learn & develop this extension, could you please rate my efforts? 😬</small>
<button class="mdl-button mdl-js-button mdl-button--accent" id=sure>
Sure! 😎
</button>
<button style='position:absolute; right: 5%;' class="mdl-button mdl-js-button mdl-button--primary" id=nah>
Nah! 😒
</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Loading

0 comments on commit aa016fc

Please sign in to comment.