Skip to content

Commit

Permalink
成功开始下载时,畅课对应课件完成度将标记为「full」
Browse files Browse the repository at this point in the history
  • Loading branch information
YanhuiJessica committed Mar 30, 2022
1 parent c7b3601 commit 1e5a672
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "troncuclass",
"version": "0.2.1",
"version": "0.3.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.1.1",
Expand Down
5 changes: 4 additions & 1 deletion public/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ function getQueryVariable(query, variable)
return false;
}

chrome.runtime.onMessage.addListener(request => {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.from === 'content') {
let filename = getQueryVariable(decodeURI(request.url), 'name');
chrome.downloads.download({
url: request.url,
filename: filename,
}, function(id) {
sendResponse({state: id != undefined});
});
}
return true;
});
29 changes: 24 additions & 5 deletions public/js/content.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const courseId = window.location.pathname.split('/')[2];

let xhr = new XMLHttpRequest(), xhrDl = new XMLHttpRequest();
var uploads = [];
let xhr = new XMLHttpRequest(),
xhrDl = new XMLHttpRequest();
let uploads = [], uindex;

xhr.onload = function() {
const data = JSON.parse(this.responseText);
var coursewares = data['activities'];
for (let c of coursewares) {
if (c['uploads'].length > 0) {
for (let u of c['uploads']) {
uploads.push(u);
uploads.push({cwid: c['id'], fileInfo: u});
}
}
}
Expand All @@ -23,8 +24,25 @@ xhr.send();

xhrDl.onload = function() {
const data = JSON.parse(this.responseText);
var url = data['url'];
chrome.runtime.sendMessage({from: 'content', url: url});
let url = data['url'];
chrome.runtime.sendMessage({from: 'content', url: url},
function(response) {
if (response.state) {
if (uploads[uindex].fileInfo.type != 'video') {
let markReadUrl = `http://courses.cuc.edu.cn/api/course/activities-read/${uploads[uindex].cwid}`;
fetch(markReadUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"upload_id": uploads[uindex].fileInfo.id,
}),
})
}
}
}
);
};

function startDownload(reference_id) {
Expand All @@ -37,6 +55,7 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({uploads: uploads});
}
if (request.from === 'popup' && request.subject === 'startDownloads') {
uindex = request.uindex;
startDownload(request.reference_id);
}
return true; // make sendResponse() work asynchronously
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"manifest_version": 2,
"name": "TronCUClass",
"version": "0.2.1",
"version": "0.3.0",

"description": "CUC 畅课课件下载(支持所有格式) Download coursewares for CUC Tronclass (all types available)",
"homepage_url": "https://github.com/YanhuiJessica/TronCUClass",
Expand Down
9 changes: 7 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TableRow = (props) => {
return (
<tr>
<td><input type="radio" name="courseware" value={props.index} /></td>
<td>{props.upload.name}</td>
<td>{props.upload.fileInfo.name}</td>
<td></td>
</tr>
)
Expand Down Expand Up @@ -42,7 +42,12 @@ const handleClick = () => {
for (let i of ids) {
chrome.tabs.sendMessage(
tabs[0].id,
{from: 'popup', subject: 'startDownloads', reference_id: uploads[i].reference_id}
{
from: 'popup',
subject: 'startDownloads',
reference_id: uploads[i].fileInfo.reference_id,
uindex: i
}
).then().catch(onError);
}
});
Expand Down

0 comments on commit 1e5a672

Please sign in to comment.