1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| (function(Scratch) { 'use strict';
class AdvancedExtension { getInfo() { return { id: 'advancedExtension', name: '高级拓展', color1: '#4CAF50', color2: '#45a049', color3: '#3d8b40', blocks: [ { opcode: 'sayHello', blockType: Scratch.BlockType.COMMAND, text: '向 [NAME] 打招呼', arguments: { NAME: { type: Scratch.ArgumentType.STRING, defaultValue: '世界' } } }, { opcode: 'calculate', blockType: Scratch.BlockType.REPORTER, text: '[A] 加 [B] 等于', arguments: { A: { type: Scratch.ArgumentType.NUMBER, defaultValue: 10 }, B: { type: Scratch.ArgumentType.NUMBER, defaultValue: 5 } } }, { opcode: 'isEven', blockType: Scratch.BlockType.BOOLEAN, text: '[NUMBER] 是偶数?', arguments: { NUMBER: { type: Scratch.ArgumentType.NUMBER, defaultValue: 4 } } }, { opcode: 'getDay', blockType: Scratch.BlockType.REPORTER, text: '获取 [DAY] 的信息', arguments: { DAY: { type: Scratch.ArgumentType.STRING, menu: 'days', defaultValue: 'monday' } } } ], menus: { days: { items: [ { text: '星期一', value: 'monday' }, { text: '星期二', value: 'tuesday' }, { text: '星期三', value: 'wednesday' } ] } } }; }
sayHello(args, util) { const name = args.NAME; if (util.runtime.renderer) { console.log(`Hello, ${name}!`); } }
calculate(args, util) { const a = Number(args.A); const b = Number(args.B); return a + b; }
isEven(args, util) { const number = Number(args.NUMBER); return number % 2 === 0; }
getDay(args, util) { const day = args.DAY; const dayMap = { 'monday': '星期一', 'tuesday': '星期二', 'wednesday': '星期三' }; return dayMap[day] || '未知'; } }
Scratch.extensions.register(new AdvancedExtension()); })(Scratch);
|