wirelyre select save

save selection

  1. // ==UserScript==
  2. // @name wirelyre select save
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description save selection
  6. // @author 13pake
  7. // @match https://wirelyre.github.io/tetra-tools/pc-solver.html*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.io
  9. // @grant GM_addStyle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. GM_addStyle("#solutions > a { border-radius: 4px; }");
  17. GM_addStyle("#solutions { row-gap: 20px; }");
  18. GM_addStyle("#select-save { background-color: rgba(0,0,0,0.2); color: #fff; border: 1px solid rgba(0,0,0,0.3); margin-left: 5px; }");
  19. GM_addStyle("#label-save { margin-top: 10px; }");
  20.  
  21. // Constants
  22. var pieces = ['T', 'I', 'L', 'J', 'S', 'Z', 'O'];
  23. var colors = [
  24. 'rgb(180, 81, 172)', // purple
  25. 'rgb(65, 175, 222)', // cyan
  26. 'rgb(239, 149, 54)', // orange
  27. 'rgb(24, 131, 191)', // blue
  28. 'rgb(102, 198, 92)', // green
  29. 'rgb(239, 98, 77)', // red
  30. 'rgb(247, 211, 62)', // yellow
  31. ];
  32.  
  33. window.onload = function() {
  34.  
  35. // Add save selection
  36. var label = document.createElement('label');
  37. label.id = 'label-save';
  38. label.innerHTML = 'Save';
  39. document.querySelectorAll('#query > div:nth-child(6)')[0].appendChild(label);
  40.  
  41. var select = document.createElement('select');
  42. label.appendChild(select);
  43. select.id = 'select-save';
  44.  
  45. var selectOptions = ['All', ...pieces];
  46.  
  47. for (var i = 0; i < selectOptions.length; i++) {
  48. var option = document.createElement('option');
  49. option.value = selectOptions[i];
  50. option.text = selectOptions[i];
  51. select.appendChild(option);
  52. }
  53.  
  54. // On select change
  55. select.onchange = function(event) {
  56. selectSave(event.target.value);
  57. }
  58.  
  59. function selectSave(value) {
  60. if (value !== 'All') {
  61. document.querySelectorAll('#solutions > a').forEach(function(a) {
  62. a.style.display = 'none';
  63. a.style.borderTopWidth = '0';
  64. });
  65. document.querySelectorAll('#solutions > a.' + value).forEach(function(a) {
  66. a.style.display = 'block';
  67. });
  68. } else if (value == 'All') {
  69. document.querySelectorAll('#solutions > a').forEach(function(a) {
  70. a.style.display = 'block';
  71. a.style.borderTopWidth = '10px';
  72. });
  73. }
  74. }
  75.  
  76. // Add listener for solutions
  77. var targetNode = document.getElementById('solutions');
  78. var config = {
  79. // attributes: true,
  80. childList: true,
  81. subtree: true
  82. };
  83.  
  84. var observer = new MutationObserver(function(mutationsList) {
  85.  
  86. var queue = document.getElementById('queue').value;
  87.  
  88. // check if queue is just pieces
  89. var piecesOnlyMatch = queue.match(/^[TILJSZO]*$/);
  90. if (piecesOnlyMatch) {
  91. //console.log('good queue');
  92. } else {
  93. return;
  94. }
  95.  
  96. var queuePieces = pieces.map(function(piece) {
  97. return (queue.split(piece).length - 1);
  98. });
  99.  
  100. // We're just gonna assume the queue length is correct !!!
  101.  
  102. //console.log('queue pieces', queuePieces);
  103.  
  104. for (var mutation of mutationsList) {
  105. if (mutation.type == 'childList') {
  106. // console.log('A child node has been added or removed.', mutation.addedNodes[0]);
  107. try {
  108. var aNode = mutation.addedNodes[0];
  109. var dataField = aNode.firstChild.getAttribute('data-field');
  110. var solutionPieces = pieces.map(function(piece) {
  111. return (dataField.split(piece).length - 1) / 4;
  112. });
  113. //console.log('pieces used', solutionPieces);
  114.  
  115. // get difference between arrays
  116. var differentIndex = -1;
  117. for (let i = 0; i < queuePieces.length; i++) {
  118. if (queuePieces[i] !== solutionPieces[i]) {
  119. differentIndex = i;
  120. }
  121. }
  122. //console.log('saved piece', pieces[differentIndex]);
  123. aNode.style.borderTop = "10px solid " + colors[differentIndex];
  124. aNode.classList.add(pieces[differentIndex]);
  125.  
  126.  
  127. } catch (e) {
  128. // do nothing lol
  129. }
  130.  
  131. }
  132. }
  133. });
  134.  
  135. observer.observe(targetNode, config);
  136. }
  137. })();