Dizy / Brightspace Print Extension

// ==UserScript==
// @name         Brightspace Print Extension
// @namespace    https://dizy.cc
// @version      1.0.2
// @description  Add print buttons for Brightspace content and assignments
// @author       Dizy
// @copyright 2022, Dizy (https://openuserjs.org/users/Dizy)
// @license      MIT
// @include      /^https?:\/\/(s\.)?brightspace\./
// @icon         https://www.d2l.com/wp-content/themes/d2l-2016/dist/images/D2L_Favicon_16x16.svg
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_log
// @grant        unsafeWindow
// @require      https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
// @require      https://git.io/waitForKeyElements.js
// ==/UserScript==

(function () {
  'use strict';
  let window = unsafeWindow;
  let currentURL = window.location.href;
  GM_log(currentURL)
  if (currentURL.includes('/dropbox/user/folder_submit_files')) {
    addPrintModeButtonToAssignmentPage();
  }
  else if (currentURL.includes('s.brightspace.com/apps/smart-curriculum/')) {
    addPrintModeButtonToContentPage();
  }

  function addPrintModeButtonToAssignmentPage() {
    $(`
        <button type="button" class="d2l-button" id="z_c" style="float:left;" onclick="printRequirements()">Print Requirements</button>
        `).insertBefore('d2l-floating-buttons #z_b');

    function setPrintMode(on) {
      let hidden = on;

      document
        .querySelector(
          "#d2l_body > header > nav > d2l-navigation > d2l-navigation-main-header"
        )
        .shadowRoot.querySelector("div").hidden = hidden;
      document
        .querySelector(
          "#d2l_body > header > nav > d2l-navigation > d2l-navigation-main-footer"
        )
        .shadowRoot.querySelector("div").hidden = hidden;
      document.querySelector("#z_g").hidden = hidden;
      document.querySelector("#d2l_form > div > h2").hidden = hidden;
      document.querySelector("#d2l_form > div > table").hidden = hidden;
      document.querySelector("#d_content_r > d2l-floating-buttons").hidden = hidden;
    }

    window.printRequirements = function () {
      setPrintMode(true);
      window.print();
      setPrintMode(false);
    }
  }

  function addPrintModeButtonToContentPage() {
    waitForKeyElements('.header-button-tray', function () {
      $(".header-button-tray").prepend(`
            <d2l-button-icon class="print-content-button" icon="tier2:print" title="Print" aria-label="Print" data-js-focus-visible="" type="button" onclick="printContent()"></d2l-button-icon>
            `)

      console.log($(".header-button-tray").length)

      function setPrintMode(on) {
        document.querySelector("div.navigation-container").style.display = on ? "none" : "block";
        document.querySelector("#content-header").style.display = on ? "none" : "flex";
        let contentPanel = document.querySelector("#root-wrapper > div.main-wrapper > div.root > div.main > div.content-panel");
        contentPanel.style.position = on ? "" : "absolute";
        contentPanel.style.height = on ? "auto" : "";
        contentPanel.style.left = on ? "0" : "";
        contentPanel.style.width = on ? "100%" : "";
        document.querySelector("#content-block").style.overflowY = on ? "visible" : "auto";
      }

      window.printContent = function () {
        setPrintMode(true)
        window.print()
        setPrintMode(false)
      }
    })

  }
})();