问卷星优化

优化问卷星体验

目前为 2022-12-28 提交的版本。查看 最新版本

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

(function () {
    "use strict";
    function getActivityId() {
        return location.pathname.match(/\/([a-zA-Z0-9]+).aspx/)[1];
    }

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

    function getBlanks() {
        return document.querySelectorAll(
            "span.textCont, input:not([type=hidden]), textarea"
        );
    }

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

    function fillInTheBlanks(answer) {
        getBlanks().forEach((e, i) => {
            if (answer[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(`${getActivityId()} choice`)) {
            return JSON.parse(
                localStorage.getItem(`${getActivityId()} choice`)
            );
        } else {
            return null;
        }
    }

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

    function getBlanksValue() {
        return JSON.stringify(
            [...getBlanks()].map((e) => {
                if (e.tagName === "SPAN") {
                    return e.innerText;
                } else {
                    return e.value;
                }
            })
        );
    }

    function getChoiceValue() {
        let tmp = [];
        [...getChoice()].forEach(
            (e, i) => [...e.classList].includes("checked") && tmp.push(i)
        );
        return JSON.stringify(tmp);
    }

    function setBlanksLastTime() {
        let blanks = getActivityIdBlanks();
        if (blanks) {
            fillInTheBlanks(blanks);
        }
    }

    function setChoiceLastTime() {
        let choice = getActivityIdChoice();
        if (choice) {
            fillInTheChoice(choice);
        }
    }

    function observe(action) {
        let observe_tmp = new MutationObserver((mutationsList) => {
            action(mutationsList);
        });
        observe_tmp.observe(document.documentElement, {
            attributes: true,
            childList: true,
            characterData: true,
            subtree: true,
        });
        return observe_tmp;
    }

    function bindingUpdates() {
        observe(() => {
            localStorage.setItem(`${getActivityId()} input`, getBlanksValue());
            localStorage.setItem(`${getActivityId()} choice`, getChoiceValue());
        });
    }

    function newGoBackButton() {
        if (/activityid=/.test(location.href)) {
            let goBackButton = document.createElement("a");
            goBackButton.href = `/vm/${
                location.href.match(/activityid=([a-zA-Z0-9]+?)&/)[1]
            }.aspx`;
            goBackButton.innerText = "重做";
            goBackButton.style.cssText =
                "display:block;margin:0px auto;width:200px;height:60px;background-color:rgb(0,149,255);color:rgb(255,255,255);line-height:60px;border-radius:5px;font-size:20px;";
            return goBackButton;
        } else {
            return null;
        }
    }

    if (/activityid=/.test(location.href)) {
        window.addEventListener("load", () => {
            let goBackButton = newGoBackButton();
            if (goBackButton) {
                document
                    .querySelector("#divdsc")
                    .appendChild(newGoBackButton());
            }
        });
    } else {
        openUserSelect();
        window.addEventListener("load", () => {
            setChoiceLastTime();
            setBlanksLastTime();
            bindingUpdates();
        });
    }
})();

QingJ © 2025

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