Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Pixel Bot 2ch
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.4.88a
  5. // @description  try to take over the world!
  6. // @author       Flyink13, DarkKeks, xi
  7. // @match        https://pixel.vkforms.ru/*
  8. // @downloadURL  https://raw.githubusercontent.com/Extered/pixwars/gh-pages/PixelDefender.js
  9. // @updateURL    https://raw.githubusercontent.com/Extered/pixwars/gh-pages/PixelDefender.js
  10. // @grant        none
  11. // ==/UserScript==
  12.  
  13. function XIDefBot() {
  14.     window.XIDefBot = XIDefBot;
  15.     XIDefBot.url = {
  16.         script: 'https://extered.github.io/pixwars/PixelDefender.js'
  17.     };
  18.     XIDefBot.refreshTime = 300;
  19.     XIDefBot.pts = 30;
  20.     XIDefBot.tc = "rgb(0, 0, 0)";
  21.     XIDefBot.doCoordLog = true;
  22.     XIDefBot.currentUrl = '';
  23.     XIDefBot.urlGen = {
  24.         script: function() {
  25.             return XIDefBot.url.script + '?v=' + Math.random()
  26.         },
  27.         image: function() {
  28.             return new Promise(function(resolve, reject) {
  29.                 fetch('https://raw.githubusercontent.com/Extered/pixwars/gh-pages/defence.json').then(function(data) {
  30.                         data.json().then(function(answer){
  31.                             resolve(answer.currentTarget);
  32.                         }).catch(function(e) {
  33.                             XIDefBot.state.textContent = "E120: " + e;
  34.                             reject();
  35.                         });
  36.                 }).catch(function(e){
  37.                     XIDefBot.state.textContent = "E121: " + e;
  38.                     resolve(XIDefBot.currentUrl);
  39.                 });
  40.             });
  41.         }
  42.     };
  43.     XIDefBot.state = document.createElement("div");
  44.     XIDefBot.state.onclick = XIDefBot.reload;
  45.     XIDefBot.state.textContent = "Загрузка приложения...";
  46.     Object.assign(XIDefBot.state.style, {
  47.         background: "rgba(0,0,0,0.5)",
  48.         bottom: "0px",
  49.         right: "0px",
  50.         width: "100%",
  51.         height: "100%",
  52.         lineHeight: "500px",
  53.         textAlign: "center",
  54.         color: "#fff",
  55.         position: "fixed",
  56.         zIndex: 10000
  57.     });
  58.     document.body.appendChild(XIDefBot.state);
  59.     XIDefBot.loger = document.createElement("div");
  60.     XIDefBot.loger.onclick = XIDefBot.reload;
  61.     Object.assign(XIDefBot.loger.style, {
  62.         background: "rgba(0,0,0,0)",
  63.         top: "0px",
  64.         left: "0px",
  65.         width: "250px",
  66.         height: "100%",
  67.         color: "#fff",
  68.         position: "fixed",
  69.  
  70.         fontSize: "11px",
  71.         padding: "12px",
  72.         zIndex: 10001
  73.     });
  74.     document.body.appendChild(XIDefBot.loger);
  75.     XIDefBot.log = function(x) {
  76.         XIDefBot.loger.innerHTML += x + "<br>";
  77.         if (XIDefBot.loger.scrollTo) {
  78.             XIDefBot.loger.scrollTo(0, 10000);
  79.         } else {
  80.             XIDefBot.loger.scrollTop = XIDefBot.loger.scrollHeight;  
  81.         }
  82.     }
  83.     ;
  84.     XIDefBot.setState = function(s) {
  85.         XIDefBot.state.innerHTML = "XIDefBot " + s;
  86.         XIDefBot.log(s)
  87.     }
  88.     ;
  89.     XIDefBot.reloadImage = function() {
  90.         XIDefBot.urlGen.image().then(function(url) {
  91.             if (!XIDefBot.img) {
  92.                 XIDefBot.img = new Image();
  93.                 XIDefBot.img.crossOrigin = "Anonymous";
  94.                 XIDefBot.img.onload = function() {
  95.                     XIDefBot.setState("перезагрузил зону защиты.");
  96.                     if (XIDefBot.inited) XIDefBot.getFullData();
  97.                 };
  98.             }
  99.             if (url != XIDefBot.currentUrl) {
  100.                 XIDefBot.img.src = url;
  101.                 XIDefBot.currentUrl = url;
  102.             }
  103.         });
  104.     };
  105.     XIDefBot.canvasEvent = function(type, q) {
  106.         if (!XIDefBot.canvas)
  107.             return;
  108.         if (type == "mousewheel") {
  109.             XIDefBot.canvas.dispatchEvent(new WheelEvent("mousewheel",q));
  110.         } else {
  111.             XIDefBot.canvas.dispatchEvent(new MouseEvent(type,q));
  112.         }
  113.     }
  114.     ;
  115.  
  116.     XIDefBot.parseRgb = function(rgb) {
  117.         return rgb.replace('rgb(', '').replace(')', '').split(', ').map(function(e){return +e;});
  118.     };
  119.     XIDefBot.canvasClick = function(x, y, color) {
  120.         XIDefBot.resetZoom();
  121.         if (x > 1590) {
  122.             XIDefBot.canvasMoveTo(1590, 0);
  123.             x = x - 1590
  124.         } else {
  125.             XIDefBot.canvasMoveTo(0, 0)
  126.         }
  127.         var q = {
  128.             bubbles: true,
  129.             cancelable: true,
  130.             button: 1,
  131.             clientX: x,
  132.             clientY: y + 1,
  133.             layerX: x,
  134.             layerY: y + 1
  135.         };
  136.         var pxColor = XIDefBot.getColor(XIDefBot.ctx.getImageData(x, y + 1, 1, 1).data, 0);
  137.         var colors = document.getElementsByClassName('color');
  138.         var colorEl = null;
  139.         var mindelta = 999999;
  140.         if (colors.length == 0) {
  141.             return;
  142.         }
  143.         for (var i = 0; i < colors.length; i++) {
  144.             var colorElRgb = XIDefBot.parseRgb(colors[i].style.backgroundColor);
  145.             var neededRgb = XIDefBot.parseRgb(color);
  146.             var delta = Math.abs(colorElRgb[0] - neededRgb[0]) + Math.abs(colorElRgb[1] - neededRgb[1]) + Math.abs(colorElRgb[2] - neededRgb[2]);
  147.             if (delta < mindelta) {
  148.                 mindelta = delta;
  149.                 colorEl = colors[i];
  150.             };
  151.         }
  152.         if (!colorEl) {
  153.             console.log("color error %c " + color, 'background:' + color + ';');
  154.             XIDefBot.setState("Ошибка подбора цвета " + color);
  155.             return
  156.         } else if (pxColor == color) {
  157.             if (XIDefBot.doCoordLog) {
  158.                 console.log("== " + x + "x" + y + "%c " + pxColor, 'background:' + pxColor + ';');
  159.                 XIDefBot.setState("Пропускаю " + (x + 1) + "x" + (y + 1) + " совпал цвет")
  160.             } else {
  161.                 console.log("==");
  162.                 XIDefBot.setState("Пропускаю, совпал цвет")
  163.             }
  164.             return
  165.         } else {
  166.             if (XIDefBot.doCoordLog) {
  167.                 console.log(x + "x" + y + "%c " + pxColor + " -> %c " + color, 'background:' + pxColor + ';', 'background:' + color + ';');
  168.                 XIDefBot.setState("Поставил точку " + (x + 1) + "x" + (y + 1))
  169.             } else {
  170.                 console.log(" -> ");
  171.                 XIDefBot.setState("Поставил точку")
  172.             }
  173.         }
  174.         colorEl.click();
  175.         XIDefBot.canvasEvent("mousedown", q);
  176.         XIDefBot.canvasEvent("click", q);
  177.         q.button = 0;
  178.         XIDefBot.canvasEvent("mouseup", q);
  179.         document.getElementsByTagName('button')[0].click()
  180.     }
  181.     ;
  182.     XIDefBot.draw = function() {
  183.         var px = XIDefBot.pixs.shift();
  184.         if (!px) {
  185.             XIDefBot.setState("Точек нет")
  186.         } else {
  187.             XIDefBot.canvasClick(px[0], px[1], px[2])
  188.         }
  189.     }
  190.     ;
  191.     XIDefBot.canvasMove = function(x, y) {
  192.         var q = {
  193.             bubbles: true,
  194.             cancelable: true,
  195.             button: 1,
  196.             clientX: 0,
  197.             clientY: 0
  198.         };
  199.         XIDefBot.canvasEvent("mousedown", q);
  200.         q.clientY = y;
  201.         q.clientX = x;
  202.         XIDefBot.canvasEvent("mousemove", q);
  203.         XIDefBot.canvasEvent("mouseup", q)
  204.     }
  205.     ;
  206.     XIDefBot.canvasMoveTo = function(x, y) {
  207.         XIDefBot.canvasMove(10000, 10000);
  208.         XIDefBot.canvasMove(-40 - x, -149 - y)
  209.     }
  210.     ;
  211.     XIDefBot.getImageData = function() {
  212.         var data = XIDefBot.ctx.getImageData(0, 1, 1590, 400).data;
  213.         return data
  214.     }
  215.     ;
  216.     XIDefBot.getColor = function(data, i) {
  217.         return "rgb(" + data[i] + ", " + data[i + 1] + ", " + data[i + 2] + ")"
  218.     }
  219.     ;
  220.     XIDefBot.getFullData = function() {
  221.         XIDefBot.pixs = [];
  222.         XIDefBot.pixs = XIDefBot.randomShuffle(XIDefBot.getData(0));
  223.         XIDefBot.setState("осталось точек:" + XIDefBot.pixs.length);
  224.         return XIDefBot.pixs.length
  225.     }
  226.     ;
  227.     XIDefBot.getData = function(offsetX) {
  228.         XIDefBot.resetZoom();
  229.         XIDefBot.canvasMoveTo(offsetX, 0);
  230.         var id1 = XIDefBot.getImageData();
  231.         XIDefBot.ctx.drawImage(XIDefBot.img, -offsetX, 1);
  232.         var id2 = XIDefBot.getImageData();
  233.         var data = [];
  234.         for (var i = 0; i < id1.length; i += 4) {
  235.             var x = offsetX + (i / 4) % 1590
  236.               , y = ~~((i / 4) / 1590);
  237.             if (XIDefBot.getColor(id1, i) !== XIDefBot.getColor(id2, i) && XIDefBot.getColor(id2, i) !== XIDefBot.tc) {
  238.                 data.push([x, y, XIDefBot.getColor(id2, i), XIDefBot.getColor(id1, i)])
  239.             }
  240.         }
  241.         return data
  242.     };
  243.  
  244.     XIDefBot.randomShuffle = function(data) {
  245.         var currentIndex = data.length, temporaryValue, randomIndex;
  246.         while (0 !== currentIndex) {
  247.             randomIndex = Math.floor(Math.random() * currentIndex);
  248.             currentIndex -= 1;
  249.             temporaryValue = data[currentIndex];
  250.             data[currentIndex] = data[randomIndex];
  251.             data[randomIndex] = temporaryValue
  252.         }
  253.         return data
  254.     };
  255.  
  256.     XIDefBot.resetZoom = function() {
  257.         XIDefBot.canvasEvent("mousewheel", {
  258.             deltaY: 100000,
  259.             deltaX: 0,
  260.             clientX: 100,
  261.             clientY: 100,
  262.         });
  263.     };
  264.  
  265.     XIDefBot.init = function() {
  266.         XIDefBot.inited = 1;
  267.         XIDefBot.getFullData();
  268.         XIDefBot.setState("Запущен.")
  269.     };
  270.  
  271.     XIDefBot.wait = setInterval(function() {
  272.    
  273.     if (document.querySelectorAll(".Ttl__wait")[0] == null) {
  274.    
  275.         var all = document.querySelectorAll("canvas");
  276.         for(var i = 0; i < all.length; ++i) {
  277.             if(all[i].style.display != 'none') {
  278.                 XIDefBot.canvas = all[i];
  279.             }
  280.         }
  281.         XIDefBot.ctx = XIDefBot.canvas.getContext("2d");
  282.         XIDefBot.init();
  283.         XIDefBot.timer = 1;
  284.         XIDefBot.draw();
  285.         }
  286.     }, 1000);
  287.  
  288.  
  289.     XIDefBot.reloadImage();
  290.     console.log("XIDefBot loaded")
  291. }
  292.  
  293. XIDefBot();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement