// ==UserScript== // @name App Inventer 2 block helper // @version 0.4 // @namespace App Inventer 2 block helper // @description An easy way to operate blocks at MIT App Inventor 2. // @author wangsk789@163.com // @match *://ai2.appinventor.mit.edu/* // @match *://code.appinventor.mit.edu/* // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; setTimeout(() => { var lastIndex = -1; var blocks; let container = document.querySelectorAll("tr")[50]; let td = document.createElement("td"); td.style = "vertical-align: top;"; container.appendChild(td); let input = document.createElement("input"); input.type = "text"; input.size = "15"; input.id = "myInput"; input.placeholder = "key word here..."; td.appendChild(input); let td2 = document.createElement("td"); td2.style = "vertical-align: top;"; container.appendChild(td2); let button = document.createElement("button"); button.id = "mySearch"; button.className = "ode-TextButton ode-TextButton-up"; button.innerHTML = "Search"; td2.appendChild(button); button.addEventListener("click", () => { if (input.value) { findBlock(input.value); } }); let td3 = document.createElement("td"); td3.style = "vertical-align: top;"; container.appendChild(td3); let clear = document.createElement("button"); clear.id = "myClear"; clear.className = "ode-TextButton ode-TextButton-up"; clear.innerHTML = "Clear"; td3.appendChild(clear); clear.addEventListener("click", () => { input.value = ""; if (blocks && lastIndex > -1) { blocks[lastIndex].setHighlighted(false); lastIndex = -1; } }); let td4 = document.createElement("td"); td4.style = "vertical-align: top;"; container.appendChild(td4); let removeComment = document.createElement("button"); removeComment.id = "myRemoveComment"; removeComment.className = "ode-TextButton ode-TextButton-up"; removeComment.innerHTML = "Remove Comments"; td4.appendChild(removeComment); removeComment.addEventListener("click", () => { Blockly.getMainWorkspace().getAllBlocks().forEach(b=>{b.setCommentText(null)}); }); console.log("block finder ~~~"); function findBlock(keyword) { blocks = Blockly.getMainWorkspace().getAllBlocks(); for (var i = lastIndex + 1; i < blocks.length; i++) { blocks[i].setCollapsed(false); if (simpleString(blocks[i]).toLowerCase().includes(keyword.toLowerCase())) { Blockly.getMainWorkspace().centerOnBlock(blocks[i].id); blocks[i].select(); blocks[i].setHighlighted(true) if (lastIndex > -1) { blocks[lastIndex].setHighlighted(false); } lastIndex = i; return; } } if (lastIndex > -1) { blocks[lastIndex].setHighlighted(false); lastIndex = -1; } } function simpleString(block) { var text = ''; for (var i = 0, input; (input = block.inputList[i]); i++) { if (input.name == Blockly.BlockSvg.COLLAPSED_INPUT_NAME) { continue; } for (var j = 0, field; (field = input.fieldRow[j]); j++) { text += field.getText() + ' '; } } text = goog.string.trim(text) || '???'; return text; } }, 10000); })();