Skip to content

Instantly share code, notes, and snippets.

@lulzsun
Last active March 11, 2023 16:27
Show Gist options
  • Select an option

  • Save lulzsun/f8eff07e74b40126866d83a17d0fb256 to your computer and use it in GitHub Desktop.

Select an option

Save lulzsun/f8eff07e74b40126866d83a17d0fb256 to your computer and use it in GitHub Desktop.
A browser userscript to prevent the logging of specific Canvas LMS (by Instructure) quiz events.

canTvas

This script prevents Canvas from recieving or logging certain user interactions during quizzes (specifically page_focused and page_blurred).

Install

  1. First you need a userscript manager such as: Tampermonkey
  2. Install the script Here!

Warning

Make sure the script loads before attempting any tomfoolery. You can open up the developer console and check for a console.warn message that looks like this:

[canTvas] Userscript loaded successfully.

If you don't see this, then it may be because the url doesn't fit @match. You will want to change that to fit your needs.

Do note that this script could break depending if Canvas instructure changes the api url:

// testing for quiz events
var regexMatches = new RegExp("*/api/v1/courses/*/quizzes/*/submissions/*/events".replace(/\*/g, "[^ ]*"));

Be cautious and monitor network POST requests to see if any page_focused or page_blurred events are being sent during a quiz. A request payload could look something like this:

Payload

If you do not see any payloads with these events then you are probably safe!

How it works

The script will intercept http POST requests and modify the request body to remove the specified events.

Alternative solutions tend to patch (prototype polluting) browser events that send the window.blur and window.focus events.

However, this script takes a different approach by patching window.XMLHttpRequest to intercept and modify the requests before being sent, which achieves the same result. I feel like this is a cleaner approach and doesn't mess with page functionality relating to blur and focus.

Optionally, you can choose to disable the userscript temporarily by opening up the developer tools and type the following:

window.cantvas = false;
// ==UserScript==
// @name canTvas
// @namespace https://gist.github.com/lulzsun/f8eff07e74b40126866d83a17d0fb256
// @downloadURL https://gist.github.com/lulzsun/f8eff07e74b40126866d83a17d0fb256/raw/canTvas.user.js
// @version 0.2
// @description Prevents the logging of specific canvas lms quiz events.
// @author lulzsun
// @match http*://*.instructure.com/*
// @match http*://*.canvas.*.edu/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=canvaslms.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.cantvas = true;
var oldXHROpen = window.XMLHttpRequest.prototype.open;
var oldXHRSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.open = function (method, url) {
this._url = url;
var args = arguments;
return oldXHROpen.apply(this, args);
};
window.XMLHttpRequest.prototype.send = function (body) {
var url = this._url;
var args = arguments;
// testing for quiz events
var regexMatches = new RegExp("*/api/v1/courses/*/quizzes/*/submissions/*/events".replace(/\*/g, "[^ ]*"));
if (regexMatches.test(url) && window.cantvas === true) {
if (body !== undefined) {
var parsedBody = JSON.parse(body);
var i = parsedBody["quiz_submission_events"].length;
var prevents = 0;
while (i--) {
var element = parsedBody["quiz_submission_events"][i];
if (element["event_type"] === "page_focused" || element["event_type"] === "page_blurred") {
parsedBody["quiz_submission_events"].splice(i, 1);
prevents++;
}
}
if (prevents > 0) console.warn(`[canTvas] '${prevents}' event(s) were prevented.`);
args[0] = JSON.stringify(parsedBody["quiz_submission_events"]);
console.log('on send body:', parsedBody["quiz_submission_events"]);
if (parsedBody["quiz_submission_events"].length <= 0)
return;
}
}
return oldXHRSend.apply(this, args);
};
console.warn(`[canTvas] Userscript loaded successfully.`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment