Greasy Fork

Greasy Fork is available in English.

Whee1-智慧树课程问答垃圾话生成器

半自动回答习惯分问题,不能自动做题查题,就是把问题重复一遍再发出去,1.1新增做过的问题标记的功能,用红色标记

当前为 2021-05-19 提交的版本,查看 最新版本

// ==UserScript==
// @name         Whee1-智慧树课程问答垃圾话生成器
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  半自动回答习惯分问题,不能自动做题查题,就是把问题重复一遍再发出去,1.1新增做过的问题标记的功能,用红色标记
// @author       ch3cknull
// @require      https://unpkg.com/axios/dist/axios.min.js
// @match        https://qah5.zhihuishu.com/qa.html
// ==/UserScript==
(function() {
	const e = document.createEvent("MouseEvents");
	e.initEvent("click", true, true);
    const input = document.createEvent("HTMLEvents");
    input.initEvent("input", true, false);
    const state = (document.URL.includes('home'))?'home':'detail'
	const MY_ANSWER_API = "https://creditqa.zhihuishu.com/creditqa/web/qa/myAnswerList"
    const HOT_QUESTION = "https://creditqa.zhihuishu.com/creditqa/web/qa/getHotQuestionList"
    const NEW_QUESTION = "https://creditqa.zhihuishu.com/creditqa/web/qa/getRecommendList"
    const TOP_QUESTION = "https://creditqa.zhihuishu.com/creditqa/web/qa/getTopicList"
    const ELT_QUESTION = "https://creditqa.zhihuishu.com/creditqa/web/qa/getEssenceList"

    const CASLOGC = document.cookie.split(';')
        .filter(item => item.includes('CASLOGC'))
        .toString()
        .trim().replace(/\"/g, "'").split('=')[1]

    const uuid = JSON.parse(decodeURIComponent(CASLOGC)).uuid
    let params = {
    	uuid: uuid, dateFormate: new Date() * 1,
    	pageIndex: 0, pageSize: 200
	}
    document.URL.split('?')[1].split('&').forEach(i => params[i.split('=')[0]] = i.split('=')[1])
	params.courseId = document.URL.split('/')[6].split('?')[0]

	function Grama(mode) {
		const question = document.querySelector('.question-content').children[0].innerText
		const ans = question.replace(/\?|。|!|!|?|\.|{是|对}{吗|嘛|么}|什么|/g, "").replace(/嘛|吗|么/g, '')
        	.replace(/是{否|不是}/g, '是').replace(/你们|你/g, '我').replace(/有没有/, '有').replace(/能不能/,'能')
            .replace(/[\(|(][\u4E00-\u9FA5A-Za-z0-9_]+[\)|)]/g, '')
        const answer = {
            positive: [
                `你好,我认为${ans}`,
                `是的,${ans}`,
            ],
            negative: [
                `不对,${ans}是不对的`
            ],
            nonsence: [
                `我们需要辩证看待,有些是好的,有些是不好的`,
                `这不是绝对的,我们要理性看待`,
                `没有绝对是好的,还是坏的`
            ]
        }
		let arr = Object.values(answer).flat()
        if (Object.keys(answer).includes(mode)) arr = answer[mode]
		return arr[parseInt(Math.random()*100)%arr.length] + "。"
	}

	function myRender() {
		return `<div class=wheel-pannel><button class="wheel-button wheel-positive">是的</button><button class="wheel-button wheel-negative">不是</button><button class="wheel-button wheel-nonsence">中立</button></div><style>.wheel-pannel{position:fixed;right:300px;top:120px;width:80px;height:30px;z-index:3000}.wheel-button{margin:5px 0;padding:0 5px;height:30px;width:80px;border:0;outline:0;border-radius:8px;color:#f0f8ff;font-size:16px;letter-spacing:1px}.wheel-positive{background:#7fd826}.wheel-negative{background:#d82626}.wheel-positive:hover{background:#99e051}.wheel-negative:hover{background:#e05151}.wheel-nonsence{background:black}.wheel-nonsence:hover{background:#333333}</style>`
	}

	function binding() {
        if (state === "detail") {
            document.querySelector('.wheel-pannel').addEventListener('click', (e) => {
                const text = document.querySelector('textarea')
                const mode = e.target.classList[1].split('wheel-')[1]
                text.innerText = Grama(mode)
                text.dispatchEvent(input)
            })
            document.querySelector('.set-btn').addEventListener('click', (e) => {
            	const questionId = location.hash.split('/')[4].split('?')[0]
            	let answered = getMydata()
                answered.push(questionId)
            	localStorage.setItem('answered', JSON.stringfy(answered))
            })
        } else {
            document.querySelector('.tab-container').addEventListener('click', (e) => {
                let text = e.target.innerText
                let url
                if (text == "热门") url = HOT_QUESTION
                if (text == "最新") url = NEW_QUESTION
                diff(url)
            })
        }
	}
    async function diff(url) {
	  	let myAnswers, panelAnswer
	  	// get data
	  	myAnswers = await getMydata()
	  	await axios.get(url, {params: params}).then( res => {
         	panelAnswer = res.data.rt.questionInfoList
      	// diff
      		let arr = panelAnswer.map(item => item.questionId)
        		.filter(item => myAnswers.includes(item))
      		let ans = panelAnswer.filter( item => arr.includes(item.questionId))
          		.map(item => `${item.userDto.username}${item.content}`)
      		patch(ans)
      })
	}
	async function patch(res) {
	  // iterate dom list and add marks
      	const list = Array.from(document.querySelectorAll('.question-item'))
	  	list.forEach( item => {
	    	const flag = item.querySelector('.user-name').title + item.querySelector('.question-content').title
	    	let condition = res.includes(flag)
	    	if (condition) {
	      		const child = item.querySelector(".question-content")
	      		child.innerText += "(已作答)"
	      		child.style.color = 'red'
	    	}
	  })
	}
	async function getMydata() {
	    let answered = JSON.parse(localStorage.getItem('answered'))
    	if (answered == null || answered == []) {
	        await axios.get(MY_ANSWER_API, {params:params}).then( res => {
	    		answered = res.data.rt.myAnswers.map(item => item.qid) || []
	    		localStorage.setItem('answered', JSON.stringify(answered))
	    	})
    	}
	    return answered
	}
	window.onload = () => {
		if (state == 'home') setTimeout(home, 1000)
		else setTimeout(detail, 1000)

        async function detail() {
			const btn = document.querySelector('.my-answer-btn')
	        if (btn == null) return
			btn.dispatchEvent(e)
	        setTimeout(() => {
	            const text = document.querySelector('textarea')
	            if (!text) return
	            text.innerText = Grama("default")
	            text.dispatchEvent(input)
	    		const dialog = document.querySelector('.el-dialog__header')
		        dialog.innerHTML += myRender()
	        	binding()
			    text.focus()
	        }, 0)
        }
        async function home() {
        	binding()
            diff(HOT_QUESTION)
        }
    }
})();