问卷星优化

优化问卷星体验

目前為 2022-12-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         问卷星优化
// @namespace    http://tampermonkey.net/
// @version      2.0.0
// @description  优化问卷星体验
// @author       share121
// @match        https://ks.wjx.top/*/*.aspx
// @match        https://www.wjx.cn/*/*.aspx
// @icon         https://ks.wjx.top/favicon.ico
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    "use strict";
    function openUserSelect() {
        document.body.style.userSelect = "auto";
        document.body.style.webkitUserSelect = "auto";
    }

    function getBlanks() {
        return document.querySelectorAll(
            "input, textarea, div > div.field-label > div > div > label > span"
        );
    }

    function getChoice() {
        return document.querySelectorAll(
            "div > div.ui-controlgroup > div:is(.ui-checkbox, .ui-radio)"
        );
    }

    function fillInTheBlanks(answer) {
        getBlanks().forEach((e, i) => {
            if (e.tagName === "SPAN") {
                e.innerText = answer[i];
            } else {
                e.value = answer[i];
            }
        });
    }

    function fillInTheChoice(answer) {
        getChoice().forEach((e, i) => {
            answer.includes(i) && e.click();
        });
    }

    function getActivityIdChoice() {
        if (localStorage.getItem(`${activityId} choice`)) {
            return JSON.parse(localStorage.getItem(`${activityId} choice`));
        } else {
            return null;
        }
    }

    function getActivityIdBlanks() {
        if (localStorage.getItem(`${activityId} input`)) {
            return JSON.parse(localStorage.getItem(`${activityId} input`));
        } else {
            return null;
        }
    }

    let activityId = location.pathname.match(/\/([a-zA-Z0-9]+).aspx/)[1];
    openUserSelect();
    window.addEventListener("load", () => {
        let choice = getActivityIdChoice();
        if (choice) {
            fillInTheChoice(choice);
        }
        let blanks = getActivityIdBlanks();
        if (blanks) {
            fillInTheBlanks(blanks);
        }

        getBlanks().forEach((e) => {
            e.addEventListener("blur", () => {
                localStorage.setItem(
                    `${activityId} input`,
                    JSON.stringify(
                        [...getBlanks()].map((e) => {
                            if (e.tagName === "SPAN") {
                                return e.innerText;
                            }
                            return e.value;
                        })
                    )
                );
            });
        });

        getChoice().forEach((e) => {
            e.addEventListener("click", () => {
                let tmp = [];
                [...getChoice()].forEach(
                    (e, i) =>
                        [...e.classList].includes("checked") && tmp.push(i)
                );
                localStorage.setItem(
                    `${activityId} choice`,
                    JSON.stringify(tmp)
                );
            });
        });
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址