Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Easy way to download attachments on studenti.it
Browse files Browse the repository at this point in the history
  • Loading branch information
rospino74 committed Jun 6, 2004
1 parent c463867 commit 960fd1f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
52 changes: 52 additions & 0 deletions src/foreground/studenti/import/getFileDownloadUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 rospino74
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

export default async function getFileDownloadUrl(documentId: string): Promise<string> {
const apiUrl = `https://doc.studenti.it/ajax/download.php`;
const body = `k=${documentId}`;

// Post request to get the download url
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length.toString(),
},
body
});

// Watch out for http errors
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}

const json = await response.json();

// Response object
if (process.env.NODE_ENV !== 'production') {
console.log(json)
}

if (json.esito != 'OK') {
throw new Error(`${response.status} ${response.statusText}: ${json.messaggio}`);
}

if (process.env.NODE_ENV !== 'production') {
if (/^it\b/.test(navigator.language)) {
console.log('%cIndirizzo del download: %c%s', 'color: #7ab700;', 'color: gray;', json.link);
} else {
console.log('%cDownload url: %c%s', 'color: #7ab700;', 'color: gray;', json.link);
}
}

return json.link;
}
36 changes: 32 additions & 4 deletions src/foreground/studenti/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
// limitations under the License.
//

import getFileDownloadUrl from "./import/getFileDownloadUrl";

console.log('%cStudenti.it %cv%s', 'color: #7ab700', 'color: gray; font-style: italic;', process.env.VERSION);

const appuntiRegex = /appunti\/[a-zA-Z0-9\-/]+\.html/gm;
const urlPageIdRegex = /\?h=([a-zA-Z0-9\-]+)/gm;
let pageId: string;

// Check if the page is an appunti page
if (appuntiRegex.test(window.location.href)) {
const nextPageId = getNextPageId();
removeAdvertisingLink(nextPageId);
pageId = getNextPageId();
removeAdvertisingLink(pageId);

// Remove right arrow button
const rightArrowButtons = document.querySelectorAll(".pager ul li:last-child") as NodeListOf<HTMLLIElement>;
rightArrowButtons.forEach(btn => btn.parentElement?.removeChild(btn));
} else {
pageId = urlPageIdRegex.exec(window.location.href)!![1];

const relatedPageButton = document.querySelectorAll(".pager ul li a[href*=correlati]") as NodeListOf<HTMLAnchorElement>;
relatedPageButton.forEach(btn => {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -36,12 +42,34 @@ if (appuntiRegex.test(window.location.href)) {
const li = btn.parentElement;
li?.parentElement?.removeChild(li);
});

// Gets the download button
const downloadButton = document.querySelector<HTMLAnchorElement>("a.download-doc");
if (downloadButton) {
downloadButton.href = '#';
downloadButton?.addEventListener('click', (evt) => {
evt.preventDefault();

if (process.env.NODE_ENV !== 'production') {
if (/^it\b/.test(navigator.language)) {
console.log('%cChiedo url download...', 'color: #7ab700;');
} else {
console.log('%cRequesting download url...', 'color: #7ab700;');
}
}

getFileDownloadUrl(pageId).then(url => {
// Open the url
window.location.href = url;
});
});
}
}

function getNextPageId(): string {
// Grabbing the url from the button
const nextPageUrl = (document.querySelector(".pager ul li:nth-child(2) a") as HTMLAnchorElement).href;
const pageId = /download_2\/([a-zA-Z0-9\-]+)_1\.html/gm;
const pageIdRegex = /download_2\/([a-zA-Z0-9\-]+)_1\.html/gm;

if (process.env.NODE_ENV !== 'production') {
if (/^it\b/.test(navigator.language)) {
Expand All @@ -51,7 +79,7 @@ function getNextPageId(): string {
}
}

return pageId.exec(nextPageUrl)!![1];
return pageIdRegex.exec(nextPageUrl)!![1];
}


Expand Down

1 comment on commit 960fd1f

@grandpa1946
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why tf is this "comitted" in 2004?

Please sign in to comment.