Show Hidden Qualtrics Progress

Shows hidden progress on qualtrics surveys

As of 2019-09-07. See the latest version.

// ==UserScript==
// @name         Show Hidden Qualtrics Progress
// @namespace    mktemp@pm.me
// @version      1.01
// @description  Shows hidden progress on qualtrics surveys
// @author       letsfindcommonground
// @match        https://*.qualtrics.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if(!(window.QSettings && window.QSettings.pt)) {
        return;
    }
    let progress = window.QSettings.pt.ProgressPercent;

    document.querySelector('body').insertAdjacentHTML('afterbegin', `
<span id='tampermonkey-progress-indicator'>${progress}%</span>
`);
    let indicator = document.querySelector('#tampermonkey-progress-indicator');
    indicator.style.cssText = `color: white; background: black; padding: 8px 12px;
position: absolute; left: 0; top: 0; cursor: pointer; opacity: 1; font-family: 'Roboto', sans-serif; font-size: 0.9em;`;
    indicator.addEventListener('click', () => { indicator.style.opacity = (indicator.style.opacity !== '1') ? '1' : '0.2' });

    let oldXHROpen = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        if(url.includes('next?rand=')) {
            this.addEventListener('load', function() {
                const result = JSON.parse(this.response);
                indicator.innerHTML = `${result.ProgressPercent}%`;
            });
        }

        return oldXHROpen.apply(this, arguments);
    };
})();