Greasy Fork

Greasy Fork is available in English.

Get all downloadable content from MIT OpenCourseWare

Get a button to download all the content from a course and lot to console

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @license      MIT
// @name         Get all downloadable content from MIT OpenCourseWare
// @match        https://ocw.mit.edu/courses/*
// @description  Get a button to download all the content from a course and lot to console
// @version 0.0.1.20221203155027
// @namespace http://greasyfork.icu/users/941655
// ==/UserScript==

var res = [];

const mainButton = document.createElement("button");
mainButton.innerText = "Get all links";
mainButton.style.marginLeft = "auto";

console.log("Script loaded");

const parseLecture = async (linkElement) => {
  const page = new DOMParser().parseFromString(
    await (await fetch(linkElement.getAttribute("href"))).text(),
    "text/html"
  );
  return {
    title: `${linkElement.parentElement.getAttribute(
      "data-title"
    )}${linkElement.textContent.trim()}`,
    link: page
      .querySelector("a.download-file")
      .getAttribute("href"),
  };
}

const getLecture = async (lectureElement, name) => {
  const linkElements = lectureElement.querySelectorAll("a");
  const lectureTasks = [];

  linkElements.forEach((linkElement) => {
    lectureTasks.push(parseLecture(linkElement));
  });

  return { links: await Promise.all(lectureTasks), name };
}



const main = async () => {
  console.log("Starting...");

  const tasks = [];
  let idx = 1;
  for (const lectureElement of document.querySelectorAll(".card-body tbody tr")) {
    const name = `${idx++}. ${lectureElement.querySelector("a").textContent.trim()}`;
    tasks.push(getLecture(lectureElement, name));
  }

  res = await Promise.all(tasks);
  console.log("Done!");
  console.table(res);
}

mainButton.onclick = main;

document.addEventListener("readystatechange", () => {
  console.log("Adding button...");
  document.querySelector(".course-banner-content").appendChild(mainButton);
});