107 lines
4.2 KiB
JavaScript
107 lines
4.2 KiB
JavaScript
// =============================================================================
|
|
// Westpac Statement Download Helper
|
|
// =============================================================================
|
|
// PASTE + ENTER, then click Download PDF buttons normally.
|
|
// Instead of opening in a new tab, the PDF saves as a properly named file.
|
|
//
|
|
// TO STOP: just refresh the page (F5)
|
|
// =============================================================================
|
|
|
|
(function () {
|
|
'use strict';
|
|
|
|
let downloadCount = 0;
|
|
|
|
// Get the currently selected account name for the filename
|
|
function getAccountName() {
|
|
const select = document.getElementById('AccountGlobalId');
|
|
if (!select) return 'Account';
|
|
const text = select.options[select.selectedIndex].text.trim();
|
|
// Extract just the account name (before the number/balance)
|
|
// e.g., "Altitude Qantas Black Card xxxx xxxx xx77 8032 -$10,272.88"
|
|
// → "Altitude-Qantas-Black-Card"
|
|
// e.g., "Westpac Choice 733-501 714878 $0.00"
|
|
// → "Westpac-Choice"
|
|
const match = text.match(/^(.+?)\s{2,}/);
|
|
if (match) {
|
|
return match[1].trim().replace(/\s+/g, '-');
|
|
}
|
|
return text.split(/\s{2,}/)[0].trim().replace(/\s+/g, '-');
|
|
}
|
|
|
|
// Parse date from the statement row
|
|
function getDateFromRow(row) {
|
|
const dateCell = row.querySelector('td.date span[data-bind*="FormattedDate"]');
|
|
if (!dateCell) return null;
|
|
const text = dateCell.textContent.trim(); // e.g., "22 Feb 2026"
|
|
const match = text.match(/(\d{1,2})\s+(\w{3})\s+(\d{4})/);
|
|
if (!match) return null;
|
|
const [, day, monthShort, year] = match;
|
|
const monthNum = new Date(`${monthShort} 1, 2000`).getMonth() + 1;
|
|
const monthNames = ['', 'January', 'February', 'March', 'April', 'May', 'June',
|
|
'July', 'August', 'September', 'October', 'November', 'December'];
|
|
return {
|
|
year,
|
|
monthNum: String(monthNum).padStart(2, '0'),
|
|
monthName: monthNames[monthNum]
|
|
};
|
|
}
|
|
|
|
// Intercept clicks on Download PDF links
|
|
document.addEventListener('click', function (e) {
|
|
const link = e.target.closest('td.tf-actions a.btn');
|
|
if (!link) return;
|
|
|
|
const href = link.getAttribute('href');
|
|
if (!href || !href.includes('getpdfstatement')) return;
|
|
|
|
// Prevent the default (opening in new tab)
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
|
|
const row = link.closest('tr');
|
|
const dateInfo = row ? getDateFromRow(row) : null;
|
|
const accountName = getAccountName();
|
|
|
|
let filename;
|
|
if (dateInfo) {
|
|
filename = `Westpac-${accountName}-${dateInfo.year}-${dateInfo.monthNum}-${dateInfo.monthName}.pdf`;
|
|
} else {
|
|
filename = `Westpac-${accountName}-${Date.now()}.pdf`;
|
|
}
|
|
|
|
console.log(`⏳ Downloading ${filename}...`);
|
|
|
|
// Fetch the PDF and save it
|
|
fetch(href, { credentials: 'include' })
|
|
.then(response => {
|
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
return response.blob();
|
|
})
|
|
.then(blob => {
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
setTimeout(() => URL.revokeObjectURL(url), 2000);
|
|
downloadCount++;
|
|
console.log(`✅ #${downloadCount} ${filename} (${(blob.size / 1024).toFixed(0)} KB)`);
|
|
})
|
|
.catch(err => {
|
|
console.error(`❌ Failed to download ${filename}:`, err);
|
|
console.log(' Opening in new tab as fallback...');
|
|
window.open(href, '_blank');
|
|
});
|
|
}, true);
|
|
|
|
console.log('');
|
|
console.log('✅ Westpac download helper active!');
|
|
console.log('👆 Click Download PDF buttons normally.');
|
|
console.log(' Each click: PDF saves as a named file (no new tab).');
|
|
console.log(' To stop: refresh (F5)');
|
|
console.log('');
|
|
})();
|