ID: I202606042110
Status: idea
Tags: Tampermonkey, pastebin
brightspace exporter
I created the script below to download all Brightspace folders of a module.
// ==UserScript==
// @name Brightspace Export Tool
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description Batch export from Brightspace
// @author You
// @match https://brightspace.avans.nl/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let popupCreated = false;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
// querySelectorAll that also pierces shadow roots
function deepQueryAll(root, selector, out = []) {
root.querySelectorAll(selector).forEach((el) => out.push(el));
root.querySelectorAll('*').forEach((el) => {
if (el.shadowRoot) deepQueryAll(el.shadowRoot, selector, out);
});
return out;
}
async function waitFor(fn, timeout = 10000, step = 250) {
const start = Date.now();
while (Date.now() - start < timeout) {
const res = fn();
if (res) return res;
await sleep(step);
}
return null;
}
function getModules() {
const seen = new Set();
return deepQueryAll(document, 'd2l-list-item-nav').filter((el) => {
const href = el.getAttribute('action-href') || '';
if (!href.includes('/units/')) return false;
if (el.hasAttribute('_nested')) return false;
if (el.closest('d2l-list[slot="nested"]')) return false;
if (seen.has(href)) return false;
seen.add(href);
return true;
});
}
function openItem(item) {
const link = item.shadowRoot?.querySelector('a, [role="link"]');
(link || item).click();
}
function findDownloadButton() {
const header = document.getElementById('content-header');
if (!header) return null;
const scope = header.querySelector('.header-button-tray') || header;
const direct = scope.querySelector('.download-content-button');
if (direct) return direct;
const candidates = deepQueryAll(
scope,
'button, a, d2l-button, d2l-button-subtle, d2l-button-icon'
);
return (
candidates.find((el) =>
/download/i.test(
[
el.textContent,
el.getAttribute('text'),
el.getAttribute('aria-label'),
el.getAttribute('title'),
el.className,
].join(' ')
)
) || null
);
}
function createPopup() {
if (popupCreated) return;
const navTree =
document.querySelector('.navigation-container') ||
document.querySelector('.list-container');
if (!navTree) return;
popupCreated = true;
const popup = document.createElement('div');
popup.style.cssText = `
position: fixed; top: 10px; left: 10px; background: white;
border: 2px solid #333; padding: 10px; z-index: 10000;
border-radius: 5px; box-shadow: 0 2px 8px rgba(0,0,0,0.15);
`;
const button = document.createElement('button');
button.textContent = 'Export';
button.style.cssText = `
padding: 8px 12px; background: #007bff; color: white; border: none;
border-radius: 3px; cursor: pointer; font-weight: bold;
`;
popup.appendChild(button);
document.body.appendChild(popup);
button.addEventListener('click', async () => {
button.disabled = true;
button.textContent = 'Processing...';
try {
const items = getModules(navTree);
if (items.length === 0) {
alert('Could not find any d2l-list-item-nav modules');
return;
}
console.log(
'Modules:',
items.map((i) => i.getAttribute('label'))
);
for (let i = 0; i < items.length; i++) {
const label = items[i].getAttribute('label');
button.textContent = `(${i + 1}/${items.length}) ${label}`;
openItem(items[i]);
await sleep(1500);
const downloadBtn = await waitFor(findDownloadButton, 8000);
if (!downloadBtn) {
console.warn('No download button for', label);
continue; // skip instead of aborting everything
}
downloadBtn.click();
if (i < items.length - 1) await sleep(5000);
}
await sleep(5000);
alert('Export completed!');
} catch (error) {
alert('Error: ' + error.message);
} finally {
button.disabled = false;
button.textContent = 'Export';
}
});
}
window.addEventListener('load', () => {
setTimeout(createPopup, 5000);
setInterval(createPopup, 10000);
});
})();