讨论区投票

为Quicker网站的讨论区添加投票功能

Version au 07/12/2024. Voir la dernière version.

Vous devrez installer une extension telle que Tampermonkey, Greasemonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Violentmonkey pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey ou Userscripts pour installer ce script.

Vous devrez installer une extension telle que Tampermonkey pour installer ce script.

Vous devrez installer une extension de gestionnaire de script utilisateur pour installer ce script.

(J'ai déjà un gestionnaire de scripts utilisateur, laissez-moi l'installer !)

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension telle que Stylus pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

Vous devrez installer une extension du gestionnaire de style pour utilisateur pour installer ce style.

(J'ai déjà un gestionnaire de style utilisateur, laissez-moi l'installer!)

// ==UserScript==
// @name         讨论区投票
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.1
// @description  为Quicker网站的讨论区添加投票功能
// @author       HDG
// @license      MIT
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAcjSURBVHhe5ZpNjhxFEEb7EFyCEyBOwMZH4A54gcQGIwvxIwQSGzaWhQQH8AnYmBtY3iCxBYFYGQYJVkyTL6e/6q+iI7Myy72bkp4iM6emOl5kVlbNz+Gdhw/uNengfSIdHOXw/su3D49++fDw+I+nh09ePa98dvPT4Yt/fzs8vb25KlyTa+tz+Ew+u+SQ5TZKOtijSvPhJPT98XhVvrXoxPP8nCcFciGnHcVIBzOqOJXPktmDC/ZAULGFn0uOE4VIByN1qWUSM5Cg4ihRtMc3p6jvKzlnLpF00LnarEtKbR+LuFjst6AAgj7fV3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Eb+KxLnrjaB0aXvsTVvtoKAG9HWPZAu3wws7Z1tN4LVk8CREaXe8buArhc7Edc/nT/I9M7tOT9fofVm+HeWXemC4BEjLEtJO4FKMmzdHtHJk97mXUS35KPoi12rYAomuHSapNYEWi9DMUlfzHrgPiovLczWEXDBUDE5b2dIWnBWBFovRLrMefyxOmNjvMUWyCuOFSAKJchySiu8fJhrY0vW/IUapl1hK615AXyzQLwO8EZecjEBckXEcTiEZf8atZJcERccQtJg/rdAkhuqxAS9baPFZm48WVLfvrxNouLq53+UpRfi49Kb8GHFCHf+HzJ04aLjS6TBq4XY48oHUl/Lc4fRnoFkJy3WxQh3/g005p1ijD0eJOQt3tIUG0f8681/zKUFUBSI+JQpLTx6RHn8tOzrnYPl1XskRaAP47GAkiqJ48E0CbZIoa43++ID290kvJ2DxeL/RbpH0f58zgFiII9JC+KnH7YQdw3umXWEWvJAxKKPRCJbZfskf55nH+Q0EzH2AIRxZIASx9piU/NeowjLELWzvqR7B8kaqc8HzelBTLOafYR16xvPt5INPZF7APJt+Io9g5QnVcd7QOZsBPly4WZackPb3RCgjMgoziD3f/VedXx26BFlIciyowjP/R4czhPMbYFice4F1v+1dk7dYDHYasIEvb2SRLxRd7GU5CIcQuSj3EWe/wtvhcD/KtsVgAXjvB1JUrfZbfQ90HsA4nHtoS8PcLIv8rWQR6JXgQXVbsFEooZJK0oYl+QdGy70Az26Fu5ZoOw3ApRsIVLZpB81pdgi5r8Ke4lWfoiHRT1ByQVoTf7klLbxyJRMIOkW3EW+8EnIx106kpoySOkOINEvS1IOhsTsd+jM/MiHYzUPQHRrBCS8naLKNaC5BVje5TGPR9JBzPqOwKrIcqPiivGtlMTD22XGoEcw7O+RzrYoxaCN0Z+g+TFcFxeSKwFyce2i8WvO+RCThPiIh0cpRaD24MPp/LAxklC/FxxTbgm19bn8Jl89g5pJx28T6SD94cHh/8B+4IkSOCqA5kAAAAASUVORK5CYII=
// @match        https://getquicker.net/Common/Topics/ViewTopic/*
// @match        https://getquicker.net/QA/Question/*
// @match        https://getquicker.net/Common/Topics/ViewTopic/*
// ==/UserScript==

(function () {
    'use strict';
    const floors = document.querySelectorAll(".p-3 .media");
    floors.forEach(floor => {
        const text = floor.querySelector(".answer-body").innerText;
        var match = text.match(/^\s*【投票】\s*\n(.*?)\s*\n【说明】\s*\n(.*?)\s*\n【选项】\s*\n((?:\d+-[^\s].{0,19}\s*\n?){1,9})$/);
        if (match) {
            const [, title, description, options] = match;
            const optionsRegex = /\d+-([^\n]+)/g;
            const optionsMatches = options.match(optionsRegex);
            const optionsArray = optionsMatches.map(option => {
                return { number: option.match(/\d+/)[0], content: option.split('-')[1].trim() };
            });

            const author = floor.querySelector(".username .user-link").href;
            const innerFloors = floor.querySelectorAll(".hover-container.d-flex");
            let votes = {};
            innerFloors.forEach(iFloor => {
                const iText = iFloor.querySelector(".flex-grow-1 .user-content").innerText;
                var iMatch = iText.match(/^【投给】(\d)$/);
                if (iMatch) {
                    const voter = iFloor.querySelector("div.font12 > .user-link").href;
                    const option = iMatch[1];
                    if (!votes.hasOwnProperty(voter) && voter != author) {
                        votes[voter] = option;
                    }
                    removeItem(iFloor);
                }
            });

            const votingInterface = document.createElement('div');
            votingInterface.className = 'voting-interface';
            votingInterface.innerHTML = `
            <h4 calss="voteTitle">${title}</h4>
            <p>${description}</p>
            <hr>
            <ol>
                ${optionsArray.map(option => `<li data-option="${option.number}" floor="${floor.querySelector(".floor-parent a").innerText.slice(1)}">${option.content}(${Object.values(votes).filter(x => x == option.number).length} Vote)</li>`).join('')}
            </ol>
            <hr>
            <p>共有${Object.keys(votes).length}位网友参与</p>
        `;
            const answerBody = floor.querySelector(".answer-body");
            answerBody.innerHTML = '';
            answerBody.appendChild(votingInterface);

            const optionsList = votingInterface.querySelectorAll('li');
            optionsList.forEach(option => {
                option.addEventListener('click', function () {
                    const option = this.getAttribute('data-option');
                    const floor = floors[this.getAttribute('floor') - 1];
                    floor.querySelector(".cursor-pointer").click();
                    setTimeout(function () {
                        const textbox = document.querySelector(".card-block");
                        triggerInputEvent(textbox, `【投给】${option}`);
                    }, 100);
                });
            });
        }
    });
})();

function removeItem(element) {
    const parentElement = element.parentNode;
    parentElement.removeChild(element);
}

function triggerInputEvent(element, text) {
    element.textContent = text;
    let event = new Event('input', { bubbles: true });
    element.dispatchEvent(event);
}