Using Tampermonkey to add question banks V2

An improved version of the script was created to add a button ("Add banks") to the right-hand menu so that when one clicks on this button 4 question banks are added (with predefined names). In this way refreshing the pages - does not cause question banks to be created.


// ==UserScript==
// @name        Add Question Bank
// @description Generates a question bank of a given name
// @include     http://*/courses/*/question_banks
// @require     https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.js
// @require     http://code.jquery.com/jquery-3.3.1.min.js
// @require     https://code.jquery.com/ui/1.11.4/jquery-ui.min.js
// @version     10
// @grant       none
// @run-at      document-idle
// ==/UserScript==
/* globals $ */
(function() {
  'use strict';

  // get the validation token and do a post

  var userData = {};
  
  createBank('Test bank20');
  createBank('Test bank21');
  createBank('Test bank22');
  createBank('Test bank23');


  function createBank(title) {
      var token=""
      var courseid=getCourseId()
      var url='http://canvas.docker/courses/'+courseid+'/question_banks'

      if ($('#right-side-wrapper').length) {
          $('#right-side-wrapper').click() // click the button
          token=$("#edit_bank_form input[name=authenticity_token]").val() // output the token just to see it
          console.info("token", token);
          $("#edit_bank_form #assessment_question_bank_title").val(title) // set the title of the new question bank
          $.post( url, $('form#edit_bank_form').serialize(), function(data) {
              var result = data;
              var id=result.assessment_question_bank.id;
              console.info("back from POST, the new id is ", id, " and the full result is ", result)
          },
                 'json' // I expect a JSON response
          );
    return;
    }
  }
 
  function getCourseId() {
    var courseId = null;
    try {
      var courseRegex = new RegExp('/courses/([0-9]+)');
      var matches = courseRegex.exec(window.location.href);
      if (matches) {
        courseId = matches[1];
      } else {
        throw new Error('Unable to detect Course ID');
      }
    } catch (e) {
      errorHandler(e);
    }
    return courseId;
  }


  function errorHandler(e) {
    console.log(e.name + ': ' + e.message);
  }
})();



The results can be seen in the following image:

New add banks button and results

Once again, thanks to James Jones (https://community.canvaslms.com/people/james@richland.edu) for his canvancements.