Skip to content

Instantly share code, notes, and snippets.

@dybdeskarphet
Created October 14, 2024 00:13
Show Gist options
  • Select an option

  • Save dybdeskarphet/d9f3a7143fb844984a6f3904ac94bed4 to your computer and use it in GitHub Desktop.

Select an option

Save dybdeskarphet/d9f3a7143fb844984a6f3904ac94bed4 to your computer and use it in GitHub Desktop.
Automatically download PDFs from network and extract filename from response headers on the Hitit Unviersity homework pop-up.
// ==UserScript==
// @name Hitit University UBYS Firefox Homework Download Fix
// @namespace mailto:ahmetardakavakci@gmail.com
// @version 1.2
// @description Automatically download PDFs from network and extract filename from response headers on the Hitit Unviersity homework pop-up.
// @author Ahmet Arda Kavakcı
// @match *://ubys.hitit.edu.tr/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Hook into XMLHttpRequest to detect POST requests
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener('load', function() {
// Check if the URL contains "DownloadHomeworkFile"
if (url.includes('DownloadHomeworkFile')) {
// Extract filename from response headers
let filename = 'default.pdf'; // Fallback filename
const disposition = this.getResponseHeader('Content-Disposition');
if (disposition && disposition.includes('filename=')) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(disposition);
if (matches != null && matches[1]) {
filename = matches[1].replace(/['"]/g, ''); // Clean quotes
}
}
// Create a blob from the response
const blob = new Blob([this.response], { type: 'application/pdf' });
const link = document.createElement('a');
const urlBlob = window.URL.createObjectURL(blob);
link.href = urlBlob;
link.download = filename; // Use extracted filename
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(urlBlob);
}
});
// Call the original open method
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
// Hook into Fetch API for modern requests
const originalFetch = window.fetch;
window.fetch = async function(...args) {
const response = await originalFetch(...args);
// Check if the request URL contains 'DownloadHomeworkFile'
if (args[0].includes('DownloadHomeworkFile')) {
const contentDisposition = response.headers.get('Content-Disposition');
let filename = 'default.pdf'; // Fallback filename
// Extract filename from Content-Disposition header
if (contentDisposition && contentDisposition.includes('filename=')) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
if (matches != null && matches[1]) {
filename = matches[1].replace(/['"]/g, ''); // Clean quotes
}
}
response.clone().blob().then(function(blob) {
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename; // Use extracted filename
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
});
}
return response;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment