App Inventer2 block finder

An easy way to find and highlight blocks at App Inventor 2.

Від 02.04.2023. Дивіться остання версія.

// ==UserScript==
// @name         App Inventer2 block finder
// @version      0.3
// @namespace    App Inventer2 block finder
// @description  An easy way to find and highlight blocks at App Inventor 2.
// @author       [email protected]
// @match        *://ai2.appinventor.mit.edu/*
// @match        *://code.appinventor.mit.edu/*
// @license      MIT

// ==/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);
			}
		});
		
		console.log("block finder ~~~");

		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;
			}
		});

		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);


})();