NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Discussions
// @namespace CCAU
// @description Automate course copies
// @match https://*.instructure.com/courses/*/discussion_topics
// @version 0.2.2
// @author Cappiebara
// @grant none
// @license GPL-3.0-or-later
// ==/UserScript==
"use strict";
(() => {
// out/ccau.js
function isAdmin() {
const adminButton = document.querySelector("#global_nav_accounts_link");
return adminButton ? true : false;
}
function getCourseID() {
return window.location.href.match(/courses\/(\d+)/)?.[1] ?? "NO_COURSE_ID";
}
async function isLiveCourse() {
const response = await fetch("https://se.instructure.com/api/v1/courses/" + getCourseID());
const data = await response.json();
if (!data.start_at) {
return new Promise((resolve) => resolve(false));
}
const start_date = new Date(data.start_at);
start_date.setDate(start_date.getDate() + 7);
return start_date < /* @__PURE__ */ new Date();
}
async function ccauConfirm(msg) {
return new Promise((resolve) => {
const didConfirm = confirm("Are you sure you want to " + msg + "?");
resolve(didConfirm);
});
}
function observeDOM(element, callback) {
const observer = new MutationObserver(callback);
observer.observe(element, {
childList: true
});
return observer;
}
function clickButton(sel) {
const element = document.querySelector(sel);
const button = element;
button?.click();
}
// out/discussion.js
async function deleteDiscussion() {
if (!await ccauConfirm("delete all discussions")) {
return;
}
Array.from(document.querySelectorAll(".discussions-index-manage-menu > span > button")).map((e) => e).forEach((b) => {
b.click();
setTimeout(() => clickButton("#delete-discussion-menu-option"), 250);
setTimeout(() => clickButton("#confirm_delete_discussions"), 250);
});
}
function addButton() {
const slot = document.querySelector(".pinned-discussions-v2__wrapper");
const button = document.createElement("a");
button.id = "ccau_deleteDiscussion";
button.textContent = "Delete All Discussions";
button.classList.add("btn");
button.setAttribute("tabindex", "0");
button.addEventListener("click", deleteDiscussion);
if (!slot || slot?.innerHTML.includes(button.id)) {
return;
}
slot?.insertAdjacentElement("afterbegin", button);
}
// out/index.js
async function main() {
if (!isAdmin()) {
throw new Error("Only admins can use CCAU");
}
if (await isLiveCourse()) {
throw new Error("CCAU is disabled in live courses");
}
observeDOM(document.body, () => setTimeout(addButton, 1e3));
}
main();
})();