App Inventer2 block finder

An easy to use way to find blocks at App Inventor2. It will find the block and highlight it. Search again will find next.

La data de 02-04-2023. Vezi ultima versiune.

// ==UserScript==
// @name         App Inventer2 block finder
// @version      0.1
// @namespace    App Inventer2 block finder
// @description  An easy to use way to find blocks at App Inventor2. It will find the block and highlight it. Search again will find next.
// @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(".ode-TextButton")[12].parentElement;

        let input = document.createElement("input");
        input.type = "text";
        input.size = "10";
        input.id = "myInput";
        input.placeholder = "key word here...";
        container.appendChild(input);

        let button = document.createElement("button");
        button.id = "mySearch";
        button.className = "ode-TextButton ode-TextButton-up";
        button.innerHTML = "Search";
        container.appendChild(button);
        button.addEventListener("click",()=>{
            if(input.value){
                findBlock(input.value);
            }

        });

        let clear = document.createElement("button");
        clear.id = "myClear";
        clear.className = "ode-TextButton ode-TextButton-up";
        clear.innerHTML = "Clear";
        container.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);


})();