DiepGrid

Do you want to replace those lame boring grid tiles with something much more interesting? Well, you've come to the right place!

  1. // ==UserScript==
  2. // @name DiepGrid
  3. // @namespace https://diep.io
  4. // @version 1.0
  5. // @description Do you want to replace those lame boring grid tiles with something much more interesting? Well, you've come to the right place!
  6. // @author Binary
  7. // @match https://diep.io/*
  8. // @run-at document-end
  9. // ==/UserScript==
  10.  
  11. var hotkey_activate_sequence = function(event) { // CTRL + B
  12. if (event.ctrlKey && !event.altKey && !event.shiftKey && event.code === 'KeyB' && !event.repeat) {
  13. event.preventDefault();
  14. return true;
  15. }
  16. };
  17.  
  18. var localStorage_key = 'diepgrid_preferences';
  19. var version = window.GM_info ? window.GM_info.script.version : 'error, update tampermonkey';
  20.  
  21. var presets = [{
  22. name: 'Default',
  23. imageUrl: ''
  24. },{
  25. name: 'Awesome Face',
  26. imageUrl: 'https://i.imgur.com/Q801K1i.jpg'
  27. },{
  28. name: 'Minecraft grass texture',
  29. imageUrl: 'https://i.imgur.com/SohhFGz.jpg'
  30. },{ // the following is credited to https://imgur.com/gallery/rJaJaAq
  31. name: 'Pixel water texture',
  32. imageUrl: 'https://i.imgur.com/5e8VUmY.png'
  33. },{
  34. name: 'Pixel wood texture',
  35. imageUrl: 'https://i.imgur.com/619IWye.png'
  36. },{
  37. name: 'Pixel brick texture',
  38. imageUrl: 'https://i.imgur.com/7o2h985.png'
  39. },{
  40. name: 'Pixel lava texture',
  41. imageUrl: 'https://i.imgur.com/M5VKiE7.png'
  42. },{
  43. name: 'Pixel glass texture',
  44. imageUrl: 'https://i.imgur.com/vDcA87H.png'
  45. },{
  46. name: 'Pixel bedrock texture',
  47. imageUrl: 'https://i.imgur.com/a359ele.png'
  48. },{
  49. name: 'Find out yourself',
  50. imageUrl: 'https://i.imgur.com/dGhAuvh.png'
  51. }];
  52.  
  53. var storageSettings;
  54. try{
  55. storageSettings = JSON.parse(window.localStorage.getItem(localStorage_key));
  56. }catch(e){storageSettings = {}}
  57. if(!(storageSettings instanceof Object)) storageSettings = {};
  58. var booleanOrDefault = function(key, defaultValue){
  59. if(typeof storageSettings[key] === 'boolean') return storageSettings[key];
  60. return defaultValue;
  61. };
  62. var stringOrDefault = function(key, defaultValue){
  63. if(typeof storageSettings[key] === 'string') return storageSettings[key];
  64. return defaultValue;
  65. };
  66. var settings = {
  67. enableOnLoad: booleanOrDefault('enableOnLoad', true),
  68. imageUrl: stringOrDefault('imageUrl', '')
  69. };
  70. function saveSettings() {
  71. window.localStorage.setItem(localStorage_key, JSON.stringify(settings));
  72. }
  73.  
  74. var wrapper = document.createElement('div');
  75. wrapper.style.position = 'fixed';
  76. wrapper.style.backgroundColor = '#a3ceb6';
  77. wrapper.style.padding = '10px';
  78. wrapper.style.left = '0px';
  79. wrapper.style.right = '0px';
  80. wrapper.style.bottom = '0px';
  81. wrapper.style.overflowY = 'auto';
  82. wrapper.style.overflowX = 'hidden';
  83. wrapper.style.fontFamily = 'Ubuntu';
  84. wrapper.style.display = 'none';
  85.  
  86. var versionHeader = document.createElement('p');
  87. versionHeader.style.margin = '0px';
  88. versionHeader.style.fontSize = '12px';
  89. versionHeader.style.position = 'absolute';
  90. versionHeader.style.right = '10px';
  91. versionHeader.textContent = 'Version: ' + version;
  92. wrapper.appendChild(versionHeader);
  93.  
  94. var hotkeyTip = document.createElement('p');
  95. hotkeyTip.style.margin = '0px';
  96. hotkeyTip.style.fontSize = '12px';
  97. hotkeyTip.style.position = 'absolute';
  98. hotkeyTip.textContent = 'Press CTRL + B to activate';
  99. wrapper.appendChild(hotkeyTip);
  100.  
  101. var heading = document.createElement('h1');
  102. heading.textContent = 'DiepGrid';
  103. heading.style.color = '#096f2d';
  104. wrapper.appendChild(heading);
  105.  
  106. var addParagraph = (text, fontSize = '14px') => {
  107. var paragraph = document.createElement('p');
  108. paragraph.textContent = text;
  109. paragraph.style.fontSize = fontSize;
  110. wrapper.appendChild(paragraph);
  111. };
  112.  
  113. var addSeparator = function(height, parentElement = wrapper) {
  114. var separator = document.createElement('div');
  115. separator.style.height = height + 'px';
  116. parentElement.appendChild(separator);
  117. };
  118.  
  119. var checkbox_inputs = {};
  120. var addCheckboxInput = function(displayText, name) {
  121. checkbox_inputs[name] = document.createElement('input');
  122. var enableShadowLabel = document.createElement('label');
  123. var enableShadowText = document.createTextNode(displayText);
  124. checkbox_inputs[name].type = 'checkbox';
  125. checkbox_inputs[name].checked = settings[name];
  126. enableShadowLabel.style.display = 'block';
  127. enableShadowLabel.style.width = 'fit-content';
  128. enableShadowLabel.style.fontSize = '14px';
  129. enableShadowLabel.appendChild(checkbox_inputs[name]);
  130. enableShadowLabel.appendChild(enableShadowText);
  131. wrapper.appendChild(enableShadowLabel);
  132.  
  133. checkbox_inputs[name].addEventListener('change', function() {
  134. settings[name] = checkbox_inputs[name].checked;
  135. saveSettings();
  136. });
  137. };
  138.  
  139. addCheckboxInput('Enable DiepGrid on load', 'enableOnLoad');
  140.  
  141. addParagraph('Do you want to replace those lame boring grid tiles with something much more interesting? Well, you\'ve come to the right place!', '18px');
  142. addParagraph('Step 1: paste in an image url. It is best that the image is a square (excess gets cut off) and be in increments of 50px (to avoid pixelation). If you don\'t have ideas for the image, I have provided a few presets.');
  143.  
  144. var input = document.createElement('input');
  145. input.placeholder = 'enter your image url here';
  146. input.style.width = '400px';
  147. input.onclick = () => {
  148. var promptInput = prompt('for some reason, this input bar does not like receiving input. please enter it here instead', input.value);
  149. if (promptInput !== null) input.value = promptInput;
  150. };
  151. wrapper.appendChild(input);
  152.  
  153. addParagraph('Presets:');
  154. var addPreset = (name, imageUrl) => {
  155. var btn = document.createElement('button');
  156. btn.textContent = name;
  157. btn.onclick = () => { input.value = imageUrl };
  158. wrapper.appendChild(btn);
  159. };
  160.  
  161. presets.forEach(preset => addPreset(preset.name, preset.imageUrl));
  162.  
  163. addSeparator(15);
  164. addParagraph('Step 2: press this button to activate the grid-replacer-machine. (note that it will get angry if you provide it with something that is not an image');
  165.  
  166. var activationBtn = document.createElement('button');
  167. activationBtn.onclick = () => activate();
  168. activationBtn.textContent = 'Activate the grid replacer machine';
  169. wrapper.appendChild(activationBtn);
  170.  
  171. addSeparator(15);
  172. addParagraph('Step 3: enjoy your quite different playing experience!');
  173.  
  174. document.body.appendChild(wrapper);
  175.  
  176. var image = null;
  177. var pattern = null;
  178. CanvasRenderingContext2D.prototype.createPattern = new Proxy(CanvasRenderingContext2D.prototype.createPattern, {
  179. apply(target, thisArg, args) {
  180. if (pattern) return pattern;
  181. else if (image) {
  182. var patternCanvas = document.createElement('canvas');
  183. patternCanvas.width = 50;
  184. patternCanvas.height = 50;
  185. var patternCtx = patternCanvas.getContext('2d');
  186. var squareSideLength = image.width > image.height ? image.height : image.width;
  187. var scaling = 50 / squareSideLength;
  188. patternCtx.setTransform(scaling, 0, 0, scaling, 0, 0);
  189. patternCtx.drawImage(image, 0, 0, squareSideLength, squareSideLength);
  190. args[0] = patternCanvas;
  191. pattern = Reflect.apply(target, thisArg, args);
  192. return pattern;
  193. }
  194. return Reflect.apply(target, thisArg, args);
  195. }
  196. });
  197.  
  198. function activate() {
  199. if (!input.value) {
  200. image = null;
  201. pattern = null;
  202. settings.imageUrl = '';
  203. saveSettings();
  204. return;
  205. }
  206. var img = new Image();
  207. img.onload = () => {
  208. if (img.width && img.height) {
  209. image = img;
  210. pattern = null;
  211. settings.imageUrl = input.value;
  212. saveSettings();
  213. } else {
  214. alert('DiepGrid: image has to have a width and a height');
  215. }
  216. };
  217. img.onerror = () => alert('DiepGrid: an error occurred while loading the image');
  218. img.src = input.value;
  219. }
  220.  
  221. input.value = settings.imageUrl;
  222. if (settings.enableOnLoad) {
  223. activate();
  224. }
  225.  
  226. var isDisplaying = false;
  227. document.addEventListener('keydown', function(event) {
  228. if (!hotkey_activate_sequence(event)) return;
  229. if (isDisplaying) {
  230. isDisplaying = false;
  231. wrapper.style.display = 'none';
  232. }
  233. else {
  234. isDisplaying = true;
  235. wrapper.style.display = 'block';
  236. }
  237. });