Greasy Fork

Greasy Fork is available in English.

App Inventer 2 block helper

An easy way to operate blocks at MIT App Inventor 2.

当前为 2023-04-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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       [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);
			}
		});
		
		

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


})();