make_qingsuyun_selectable

make the text of exam in qingsuyun selectable

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name			make_qingsuyun_selectable
// @description		make the text of exam in qingsuyun selectable
// @namespace		liudonghua123
// @version        	0.1.5
// @license        	MIT
// @include        	http*://www.qingsuyun.com/*
// @include        	http*://www.qingsuyun.com/h5/p/strat/exam
// @include        	http*://www.qingsuyun.com/h5/m/exam-process
// ==/UserScript==

// https://bobbyhadz.com/blog/javascript-wait-for-element-to-exist
async function waitForElementToExist(selector, interval = 300, timeout = 10000) {
    let element = document.querySelector(selector);
    if (element) {
        console.log('The element exists');
        return element;
    }
    const started_time = new Date();
    return new Promise(function(resolve, reject) {
        const intervalID = setInterval(() => {
            element = document.querySelector(selector);
            if (element) {
                console.log('The element exists');
                clearInterval(intervalID);
                resolve(element);
            }
            if (new Date() - started_time > timeout) {
                console.log('Timeout, clearInterval');
                clearInterval(intervalID);
                resolve(null);
            }
        }, interval);
    });
}

function addStyle(styles) {
    /* Create style element */
    const css = document.createElement('style');
    css.type = 'text/css';
    if (css.styleSheet) {
        css.styleSheet.cssText = styles;
    } else {
        css.appendChild(document.createTextNode(styles));
    }
    /* Append style to the head element */
    document.getElementsByTagName("head")[0].appendChild(css);
}

// For both desktop and mobile version
function do_work() {
    // process body.onselectstart
    document.body.onselectstart = null;
    console.info(`processed body.onselectstart`);

    // https://developer.mozilla.org/en-US/docs/Web/CSS/important
    const userSelectStyle = `
      * {
          user-select: text !important;
          --webkit-user-select: text !important;
      }
	`;
    addStyle(userSelectStyle);

    // make all the text in span selectable
    if (document.querySelector('#exam-progress-main-content-area')) {
        for (const span of document.querySelectorAll('#exam-progress-main-content-area span')) {
            // see also https://developer.mozilla.org/zh-CN/docs/Web/CSS/user-select
            span.style.userSelect = 'text';
        }
        console.info(`processed spans of question-answer-todo-area`);
    }
}

(async () => {
    const element = await waitForElementToExist('#exam-progress-main-content-area');
    if (element) {
        do_work()
    };
})();