Discuz自动回复

Discuz自动回复按钮,可自选回复语句。修改自https://gf.qytechs.cn/scripts/4635

目前為 2021-02-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Discuz自动回复
// @namespace    http://tampermonkey.net/
// @version      2.00
// @description  Discuz自动回复按钮,可自选回复语句。修改自https://gf.qytechs.cn/scripts/4635
// @author       backrock12
// @license      GPL License
// @include      http*forum.php?*
// @include      http*thread*.html
// @require      https://gf.qytechs.cn/scripts/421868-gbk-js/code/GBKjs.js?version=901431
// ==/UserScript==

(function() {
  "use strict";

  /* 自定义参数 */
  const selectnum = 5; //下拉数量
  const istitle = true; //是否显示标题  true/false
  //回复语
  /* 例子
   {
    key: "www.baidu.com",   //匹配的网址,可以填写正则表达式
    value: [
      "baidu 1",  //对应回复语
      "baidu 2",
      "baidu 3",
      "baidu 4",
      "baidu 5",
    ]
  } */

  const messages = [
    {
      //默认回复,请勿去掉
      key: "default",
      value: [
        "十分感谢分享",
        "楼主是个好人",
        "楼主一生平安",
        "感谢楼主分享,顶贴支持",
        "好东西啊,谢谢楼主分享",
        "收藏了。谢谢楼主分享"
      ]
    }
    /*     {
      key: "www.baidu.com",
      value: [
        "baidu 1",
        "baidu 2",
        "baidu 3",
        "baidu 4",
        "baidu 5",
      ]
    }, */
  ];

  /* 系統參數 */
  let isready = true;
  const keyword = "回复可见|隐藏";
  let GBK = null;
  let curmessage;

  /* ---------- */

  initialize();

  /* ---------- */

  function autoReply(re_message) {
    if (!isready) return;
    isready = false;

    let default_message;

    let htitle = document.querySelector(".ts");
    if (htitle) {
      htitle = htitle.innerText;
    } else {
      htitle = document.querySelector(".thread_subject");
      if (htitle) htitle = htitle.innerText;
    }

    if (!htitle) htitle = document.title;

    default_message = re_message
      ? re_message
      : curmessage[Math.floor(Math.random() * curmessage.length)];

    let fastpost_textarea = document.querySelectorAll("#fastpostmessage");
    let fastpost_submit = document.querySelectorAll("#fastpostsubmit");
    let fastpost_verify = document.querySelectorAll(
      'input[name="seccodeverify"]'
    );

    if (fastpost_textarea.length == 0 || fastpost_submit.length == 0) {
      alert("未找到快速回复表格!");
      isready = true;
      return;
    }

    let message = istitle ? default_message + "\r\n" + htitle : default_message;
    fastpost_textarea[0].innerHTML = message;

    console.log(message);

    //xhr不需要,不过输入验证码的情况填上也是方便点的
    if (fastpost_verify.length > 0) {
      alert("需要输入验证码!");

      let h = document.body.scrollHeight;
      window.scroll(0, h);
      fastpost_verify[0].focus();
      isready = true;
      return;
    }

    //xhr发帖
    let form = document.querySelectorAll("#fastpostform")[0];
    let url = form.action;
    let hidden = form.querySelectorAll('input[type="hidden"]');
    let data = "";
    for (let i = 0; i < hidden.length; i++) {
      data += hidden[i].name + "=" + hidden[i].value + "&";
    }

    let charset =
      typeof wrappedJSObject == "object"
        ? wrappedJSObject.document.characterSet
        : document.characterSet;

    let mmessage;
    if (charset.toLowerCase() == "gbk") {
      if (!GBK) GBK = GBKfunction();
      mmessage = GBK.URI.encodeURI(message);
    } else {
      mmessage = encodeURIComponent(message);
    }

    let mdata = "message=" + mmessage + "&replysubmit=replysubmit&" + data;
    let xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(oEvent) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          location.reload();
        } else {
          console.log("Error", xhr.statusText);
        }
      }
    };
    xhr.send(mdata);
    //xhr over

    isready = true;
  }

  function initialize() {
    const list = document.querySelectorAll("div.locked");
    if (list.length > 0) {
      let ulstring = "";

      if (messages.length == 1) {
        curmessage = messages[0].value;
      } else {
        for (let m = 0; m < messages.length; m++) {
          const e = messages[m];
          if (e.key == "default") {
            curmessage = e.value;
          }
          let reg;
          if (Object.prototype.toString.call(e.key) == "[object RegExp]") {
            reg = e.key;
          } else {
            reg = new RegExp(e.key);
          }
          if (reg.test(location.href)) {
            curmessage = e.value;
            break;
          }
        }
      }

      const num = selectnum > curmessage.length ? curmessage.length : selectnum;
      for (let i = 0; i < num; i++) {
        ulstring += `<li><a href="javascript:;">${curmessage[i]}</a></li>`;
      }
      if (ulstring) {
        for (const n of list) {
          if (n.innerHTML.search(new RegExp(keyword)) != -1) {
            let bt = document.createElement("div");
            bt.innerHTML = `
          <div class="autoReply">
          <button class="autoReplybutton" >自动回复</button>
        <div class="autoReplyDiv">
        <button class="autoReplyhead">»</button>
        <div class="autoReplyDownbtn">
          <ul>
            ${ulstring}
          </ul>
        </div>
      </div>
      </div>
        `;

            n.appendChild(bt);
          }
        }

        const allbutton = document.querySelectorAll(".autoReplybutton");
        for (let b of allbutton) {
          b.onclick = function() {
            autoReply(null);
          };
        }

        const alla = document.querySelectorAll(".autoReplyDownbtn ul li a");
        for (let b of alla) {
          b.onclick = function() {
            autoReply(this.innerHTML);
          };
        }

        const cssText = [
          `
        .autoReplybutton {  
        color: rgb(102, 102, 102);
        background-color: rgb(238, 238, 238);
        border: medium none;
        font-weight: 300;
        font-size: 15px;
        text-decoration: none;
        text-align: center;
        line-height: 20px;
        height: 20px;
        padding-left: 15px;
        padding-right: 0px;        
        margin: 0px 0px 0px 5px;
        display: inline-block;
        -moz-appearance: none;
        cursor: pointer;
        box-sizing: border-box;
        transition-property: all;
        transition-duration: 0.3s;
        border-radius: 4px;
        }
        `,
          `    .autoReplyDiv{
            display:inline-block;
        }`,
          `    .autoReplyDiv:hover .autoReplyDownbtn{
            display:block;
             background-color: #f1f1f1; 
        }`,
          `    .autoReplyDiv .autoReplyDownbtn{
            display:none;
            background-color:#f9f9f9; 
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            min-width: 160px;
            position:absolute;
            z-index:9999;
            margin: -2px 2px 2px -40px;
            transition:all .5s .1s;
            -moz-transition:all .5s .1s;
            -ms-transition:all .5s .1s;
            -o-transition:all .5s .1s;
            -webkit-transition:all .5s .1s;
        }`,
          `    .autoReplyDiv .autoReplyDownbtn li{
            line-height:20px;
            text-align:left;
            padding-left:4px;
            margin-left: 0px;
            list-style: none;
        }`,
          `
        .autoReplyDiv .autoReplyDownbtn ul {
        margin: 0px;
        padding: 0px;
        text-decoration: none;
        list-style: none;
        left:auto;
        right:0;
        }`,
          `    .autoReplyDiv .autoReplyDownbtn a:hover{
            text-decoration:underline;
            color:#f00;
            transition:all .5s .1s;
            -moz-transition:all .5s .1s;
            -ms-transition:all .5s .1s;
            -o-transition:all .5s .1s;
            -webkit-transition:all .5s .1s;
        }`,
          `    .autoReplyDiv .autoReplyDownbtn a{
            display:block;
            color:black;
            width:100%;
            font-size: 13px;
        }`,
          `.autoReplyhead {
          color: rgb(102, 102, 102);
          background-color: rgb(238, 238, 238);
          border: medium none;
          font-size: 15px;
          text-decoration: none;
          text-align: center;
          line-height: 20px;
          height: 20px;
          margin-left: -5px;
          padding-left: 20px;
        }`
        ];

        for (const s of cssText) {
          document.styleSheets[0].insertRule(s, 0);
        }
      }
    }
  }

  
})();

QingJ © 2025

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