Skip to content

Commit 71f48fa

Browse files
committed
完成基本下载功能
1 parent cdba1c7 commit 71f48fa

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed

icons/tronclass-dl-32.png

2.73 KB
Loading

icons/tronclass-dl-48.png

7.51 KB
Loading

js/background.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function getQueryVariable(query, variable)
2+
{
3+
var query = query.substring(1);
4+
var vars = query.split("&");
5+
for (let i in vars) {
6+
let pair = vars[i].split("=");
7+
if(pair[0] == variable){ return pair[1]; }
8+
}
9+
return false;
10+
}
11+
12+
chrome.runtime.onMessage.addListener(request => {
13+
if (request.from === 'content') {
14+
let filename = getQueryVariable(decodeURI(request.url), 'name');
15+
chrome.downloads.download({
16+
url: request.url,
17+
filename: filename,
18+
});
19+
}
20+
});

js/content.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const courseId = window.location.pathname.split('/')[2];
2+
3+
let xhr = new XMLHttpRequest(), xhrDl = new XMLHttpRequest();
4+
var uploads = [];
5+
6+
xhr.onload = function() {
7+
const data = JSON.parse(this.responseText);
8+
var coursewares = data['activities'];
9+
for (let c of coursewares) {
10+
if (c['uploads'].length > 0) {
11+
for (let u of c['uploads']) {
12+
uploads.push(u);
13+
}
14+
}
15+
}
16+
};
17+
xhr.onerror = function() {
18+
console.log('An error occurred fetching the JSON');
19+
};
20+
21+
xhr.open('GET', `http://courses.cuc.edu.cn/api/courses/${courseId}/activities`, true);
22+
xhr.send();
23+
24+
xhrDl.onload = function() {
25+
const data = JSON.parse(this.responseText);
26+
var url = data['url'];
27+
chrome.runtime.sendMessage({from: 'content', url: url});
28+
};
29+
30+
function startDownload(reference_id) {
31+
xhrDl.open('GET', `http://courses.cuc.edu.cn/api/uploads/reference/document/${reference_id}/url?preview=true`, true);
32+
xhrDl.send();
33+
}
34+
35+
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
36+
if (request.from === 'popup' && request.subject === 'getUploads') {
37+
sendResponse({uploads: uploads});
38+
}
39+
if (request.from === 'popup' && request.subject === 'startDownloads') {
40+
startDownload(request.reference_id);
41+
}
42+
});

manifest.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
3+
"manifest_version": 2,
4+
"name": "CUC-Tronclass-Downloader",
5+
"version": "0.0.1",
6+
7+
"description": "CUC 畅课课件下载(支持所有格式) Download coursewares for CUC Tronclass (all types available)",
8+
"homepage_url": "https://github.com/YanhuiJessica/CUC-Tronclass-Downloader",
9+
10+
"icons": {
11+
"32": "icons/tronclass-dl-32.png",
12+
"48": "icons/tronclass-dl-48.png"
13+
},
14+
15+
"permissions": [
16+
"activeTab", "downloads",
17+
"*://courses.cuc.edu.cn/*",
18+
"*://cdn.staticfile.org/font-awesome/*"
19+
],
20+
21+
"browser_action": {
22+
"default_icon": {
23+
"32": "icons/tronclass-dl-32.png"
24+
},
25+
"default_title": "Tronclass-dl",
26+
"default_popup": "popup/index.html"
27+
},
28+
29+
"content_scripts": [
30+
{
31+
"matches": ["*://courses.cuc.edu.cn/course/*"],
32+
"js": ["js/content.js"]
33+
}
34+
],
35+
36+
"background": {
37+
"scripts": ["js/background.js"]
38+
}
39+
}

popup/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<script src="popup.js" type="text/javascript"></script>
7+
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css">
8+
</head>
9+
<body>
10+
<div id="download">
11+
<table>
12+
<thead>
13+
<tr>
14+
<th>
15+
<!-- <input type="checkbox" id="select-all"> -->
16+
</th>
17+
<th>课件名</th>
18+
<th style="cursor: pointer;"><i id="dlbtn" class="fa fa-download"></i></th>
19+
</tr>
20+
</thead>
21+
<tbody id="uploads"></tbody>
22+
</table>
23+
</div>
24+
25+
<script src="popup.js" type="text/javascript"></script>
26+
</body>
27+
</html>

popup/popup.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function onError(error) {
2+
console.log(`Error: ${error}`);
3+
}
4+
5+
var uploads;
6+
window.addEventListener('load', function() {
7+
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
8+
chrome.tabs.sendMessage(tabs[0].id, {from: 'popup', subject: 'getUploads'},
9+
function(response) {
10+
uploads = response.uploads;
11+
var tb = document.getElementById('uploads');
12+
if (tb.innerHTML.length == 0) {
13+
for (let u in uploads) {
14+
var tr = document.createElement('tr');
15+
tr.innerHTML = `
16+
<td><input type="radio" name="courseware" value="${u}" /></td>
17+
<td>${uploads[u].name}</td>
18+
<td></td>
19+
`;
20+
tb.appendChild(tr);
21+
}
22+
}
23+
});
24+
});
25+
});
26+
27+
document.addEventListener("click", (e) => {
28+
if (e.target.id == "dlbtn") {
29+
var checkboxes = document.getElementsByName('courseware');
30+
var ids = [];
31+
for (var i = 0; i < checkboxes.length; i++) {
32+
if (checkboxes[i].checked) {
33+
ids.push(checkboxes[i].value);
34+
}
35+
}
36+
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
37+
for (let i of ids) {
38+
chrome.tabs.sendMessage(
39+
tabs[0].id,
40+
{from: 'popup', subject: 'startDownloads', reference_id: uploads[i].reference_id}
41+
).then().catch(onError);
42+
}
43+
});
44+
}
45+
})

0 commit comments

Comments
 (0)