Prolific Filter and Sorter

Allows users to sort studies.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Prolific Filter and Sorter
// @namespace    https://gist.github.com/Kadauchi
// @version      0.1.0
// @description  Allows users to sort studies.
// @author       Kadauchi
// @include      https://app.prolific.co/*
// ==/UserScript==

const options = {
    peripheral_requirements: [], // audio, camera, download, microphone
    sort: 'average_reward_per_hour', // average_reward_per_hour, average_completion_time, reward
};

let results = {};

function handleSorting() {
    const sorted = results.sort((a, b) => {
        return b[options.sort] - a[options.sort];
    });

    const list = document.querySelector('.list');

    sorted.forEach((result) => {
        const study = document.querySelector(`[data-testid="study-${result.id}"]`);
        list.append(study);
    });
}

function handleFiltering() {
    results.forEach((result) => {
        const studyFiltered = options.peripheral_requirements.some((requirement) => result.peripheral_requirements.includes(requirement));

        if (studyFiltered) {
            const study = document.querySelector(`[data-testid="study-${result.id}"]`);
            study.style.opacity = '0.25';
        }
    });
}

function initListener() {
    const open = XMLHttpRequest.prototype.open;

    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', () => {
            try {
                const json = JSON.parse(this.responseText);

                if (json.results && json.results.length > 0) {
                    results = json.results;
                }
            } catch {}
        });

        open.apply(this, arguments);
    };
}

function initObserver() {
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.addedNodes) {
                const studyAdded = [...mutation.addedNodes].some((node) => node.className === 'list');

                if (studyAdded) {
                    handleSorting();
                    handleFiltering();
                }
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
}


initListener();
initObserver();