NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name reddit.com/r/place template + auto placer // @namespace http://tampermonkey.net/ // @version 1.5 // @description try to take over the world! // @author You // @match https://www.reddit.com/place* // @match https://www.reddit.com/r/place* // @grant unsafeWindow // @updateURL https://openuserjs.org/meta/WebFreak001/reddit.comrplace_template_+_auto_placer.meta.js // ==/UserScript== function getPixels(img) { var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); return ctx.getImageData(0, 0, img.width, img.height).data; } function rgbToPlaceColor(r, g, b, a) { if (a < 128) return null; var colors = [ [255, 255, 255], [228, 228, 228], [136, 136, 136], [34, 34, 34], [255, 167, 209], [229, 0, 0], [229, 149, 0], [160, 106, 66], [229, 217, 0], [148, 224, 68], [2, 190, 1], [0, 211, 221], [0, 131, 199], [0, 0, 234], [207, 110, 228], [130, 0, 128] ]; var distances = []; var i; for (i = 0; i < colors.length; i++) { var c = colors[i]; var dR = c[0] - r; var dG = c[1] - g; var dB = c[2] - b; distances.push(dR * dR + dG * dG + dB * dB); } var minIdx = 0; var minDist = 255 * 255 * 255; for (i = 0; i < distances.length; i++) if (distances[i] < minDist) { minDist = distances[i]; minIdx = i; } return minIdx; } (function () { 'use strict'; function toHtml(str) { var htmlObject = document.createElement('div'); htmlObject.innerHTML = str; return htmlObject.firstChild; } const params = (location.search || "?").substr(1).split("&").map(x => x.split("=").map(a => unescape(a))).reduce((o, [k, v]) => Object.assign(o, { [k]: v }), {}); var img = document.createElement("img"); img.crossOrigin = "Anonymous"; img.src = params.template; img.className = "place-canvas"; var offsetX = parseInt(params.ox); var offsetY = parseInt(params.oy); Object.assign(img.style, { transform: `translate(${params.ox - 0.5}px,${params.oy - 0.5}px)`, position: "absolute", top: 0, left: 0, width: params.tw ? `${params.tw}px` : undefined, pointerEvents: "none", zIndex: 5, opacity: 0.5, }); const v = document.querySelector(".place-camera"); if (!v) return; v.appendChild(img); const cb = document.querySelector(".place-camera-button"); const c2 = toHtml(`<button id="place-template-button" class="place-camera-button" style="display:inline-block; top:110px; background-image:inherit">T</button>`); cb.parentNode.insertBefore(c2, cb); let active = 1; c2.addEventListener("click", () => { if (active === 1) { active = 2; img.style.opacity = 0.9; } else if (active === 2) { active = 0; img.style.visibility = "hidden"; } else if (active === 0) { active = 1; img.style.visibility = "inherit"; img.style.opacity = 0.5; } }); img.onload = function () { var pixels = getPixels(img); function randomInt(min, max) { return Math.floor(Math.random() * (max - min) + min); } var r = unsafeWindow.r; console.log(r); var draw; function check() { if (r.place.getCooldownTimeRemaining() === 0) draw(); else setTimeout(check, 2500); } draw = function () { var data = document.getElementById("place-canvasse").getContext("2d").getImageData(0, 0, 1000, 1000).data; var intv = setInterval(function () { var y = randomInt(0, img.width); var x = randomInt(0, img.height); var pixel = y * img.width * 4 + x * 4; var place = rgbToPlaceColor(pixels[pixel], pixels[pixel + 1], pixels[pixel + 2], pixels[pixel + 3]); if (place !== null) { var placeRGB = [ data[offsetY * 4000 + y * 4000 + offsetX * 4 + x * 4], data[offsetY * 4000 + y * 4000 + offsetX * 4 + x * 4 + 1], data[offsetY * 4000 + y * 4000 + offsetX * 4 + x * 4 + 2] ]; if (rgbToPlaceColor(placeRGB) != place) { console.log("have to draw! ", place); console.log(offsetX + x, offsetY + y); clearInterval(intv); setTimeout(check, 2500); r.place.setColor(place); r.place.drawTile(offsetX + x, offsetY + y); } } }, 100); }; setTimeout(check, 2500); }; console.log("/r/place template added"); })();