Есть ли способ конвертировать Javascript в пользовательский скрипт? - PullRequest
0 голосов
/ 04 октября 2019

Я пытаюсь создать бота для игры в протобоул, который поможет ответить на ваши вопросы, который работает на https://protobowl.com/msquizbowl

Я пытался исправить это с помощью всплывающих сообщений, но он не работает. Он должен сообщать вам ответы.

Файл JavaScript.

console.log("Bot> Hello"), console.log("Bot> I'm listening for questions to start now"), console.log("Bot> If you want me to stop, type in 'stopBot()'"), console.log("Bot> Pinging server (buzzing may be delayed until it starts up)..."), $.get("https://protobowl-bot-server.herokuapp.com")
  .done(() => console.log("Bot> Server online!"));
const node = document.getElementById("history"),
  config = {
    attributes: !1,
    childList: !0,
    subtree: !1
  };
let observer = new MutationObserver(a => {
  for (let b of a) "childList" == b.type && 0 < b.addedNodes.length && (console.log("Bot> Searching..."), startGuess())
});

function startGuess() {
  guess()
    .then(a => {
      a || setTimeout(startGuess, 50)
    })
}
observer.observe(node, config);

function guess() {
  return new Promise(a => {
    let b = $(".active .well")
      .children()
      .not(".unread");
    if (3 > b.length) return console.log("Bot> Too early to buzz..."), void a(!1);
    let c = b.text();
    c = c.replace("\u201C", "\"")
      .replace("\u201D", "\"")
      .replace("\u2018", "'")
      .replace("\u2019", "'");
    let d = {
      query: encodeURI(c),
      search_type: "Question",
      difficulty: "middle_school",
      question_type: "Tossup",
      limit: "false",
      download: "json"
    };
    $.get("https://protobowl-bot-server.herokuapp.com", d)
      .done(b => {
        if (1 == b.data.num_tossups_found) {
          let c = b.data.tossups[0].answer.replace(/ *\[[^\]]*]/, "")
            .replace(/"(.*?(\s)*?)*?"/, "")
            .replace(/ *\[[^)]*\ */g, "");
          console.log("Bot> Found answer: " + c), $(".buzzbtn")
            .click(), $(".guess_input")
            .val(c), $(".guess_form")
            .submit();
          let d = $.Event("keydown", {
            keyCode: 78,
            which: 78
          });
          setTimeout(() => $("body")
            .trigger(d), 50), a(!0)
        } else console.log("Bot> Not sure yet..."), a(!1)
      })
  })
}

function stopBot() {
  console.log("Bot> Bye"), observer.disconnect()
}

верхний индекс

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://protobowl.com/msquizbowl
// @grant        none
// ==/UserScript==

(function wasd() {
    'use strict';
    console.log("Bot> Hello"),console.log("Bot> I'm listening for questions to start now"),console.log("Bot> If you want me to stop, type in 'stopBot()'"),console.log("Bot> Pinging server (buzzing may be delayed until it starts up)..."),$.get("https://protobowl-bot-server.herokuapp.com").done(()=>console.log("Bot> Server online!"));const node=document.getElementById("history"),config={attributes:!1,childList:!0,subtree:!1};let observer=new MutationObserver(a=>{for(let b of a)"childList"==b.type&&0<b.addedNodes.length&&(console.log("Bot> Searching..."),startGuess())});function startGuess(){guess().then(a=>{a||setTimeout(startGuess,50)})}observer.observe(node,config);function guess(){return new Promise(a=>{let b=$(".active .well").children().not(".unread");if(3>b.length)return console.log("Bot> Too early to buzz..."),void a(!1);let c=b.text();c=c.replace("\u201C","\"").replace("\u201D","\"").replace("\u2018","'").replace("\u2019","'");let d={query:encodeURI(c),search_type:"Question",difficulty:"middle_school",question_type:"Tossup",limit:"false",download:"json"};$.get("https://protobowl-bot-server.herokuapp.com",d).done(b=>{if(1==b.data.num_tossups_found){let c=b.data.tossups[0].answer.replace(/ *\[[^\]]*]/,"").replace(/"(.*?(\s)*?)*?"/,"").replace(/ *\[[^)]*\ */g,"");console.log("Bot> Found answer: "+c),$(".buzzbtn").click(),$(".guess_input").val(c),$(".guess_form").submit();let d=$.Event("keydown",{keyCode:78,which:78});setTimeout(()=>$("body").trigger(d),50),a(!0)}else console.log("Bot> Not sure yet..."),a(!1)})})}function stopBot(){console.log("Bot> Bye"),observer.disconnect()}


    // Your code here...
})();
...