Skip to content

Commit cc1b460

Browse files
author
Ke, Mingze
committed
Fixed youtube
1 parent d5b4ae6 commit cc1b460

8 files changed

Lines changed: 43 additions & 45 deletions

File tree

generators/webduino.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,30 @@ Blockly.JavaScript['demo_range_input_value'] = function (block) {
206206
Blockly.JavaScript['demo_youtube'] = function (block) {
207207
var value_name_ = Blockly.JavaScript.valueToCode(block, 'name_', Blockly.JavaScript.ORDER_ATOMIC);
208208
var text_id_ = block.getFieldValue('id_');
209-
var code = 'function onYouTubeIframeAPIReady() {\n' +
210-
' ' + value_name_ + ' = new YT.Player("player", {\n' +
211-
' height: "240",\n' +
212-
' width: "96%",\n' +
213-
' videoId: "' + text_id_ + '",\n' +
214-
' playerVars: {\n' +
215-
' "autoplay": 1,\n' +
216-
' "controls": 1},\n' +
217-
' events: {\n' +
218-
' "onReady": onPlayerReady,\n' +
219-
' },\n' +
220-
' playerReady: false\n' +
221-
' });\n' +
222-
' function onPlayerReady(event) {\n' +
223-
' event.target.playVideo();\n' +
224-
' ' + value_name_ + '.playerReady=true;\n' +
209+
var code =
210+
'(function () {\n' +
211+
' var tag = document.createElement("script");\n' +
212+
' tag.src = "https://www.youtube.com/iframe_api";\n' +
213+
' var scptTag = document.getElementsByTagName("script")[0];\n' +
214+
' scptTag.parentNode.insertBefore(tag, scptTag);\n' +
215+
' window.onYouTubeIframeAPIReady = playVideo;\n\n' +
216+
' function playVideo() {\n' +
217+
' ' + value_name_ + ' = new YT.Player("player", {\n' +
218+
' height: "240",\n' +
219+
' width: "96%",\n' +
220+
' videoId: "' + text_id_ + '",\n' +
221+
' playerVars: {\n' +
222+
' autoplay: 1,\n' +
223+
' controls: 1\n' +
224+
' },\n' +
225+
' events: {\n' +
226+
' onReady: function (evt) {\n' +
227+
' ' + value_name_ + '.playerReady = true;\n' +
228+
' }\n' +
229+
' }\n' +
230+
' });\n' +
225231
' }\n' +
226-
'}\n\n' +
227-
'onYouTubeIframeAPIReady();\n\n';
232+
'}());\n\n';
228233
return code;
229234
};
230235

@@ -235,22 +240,22 @@ Blockly.JavaScript['demo_youtube_volume'] = function (block) {
235240
var varA = Blockly.JavaScript.variableDB_.getDistinctName(
236241
'varA', Blockly.Variables.NAME_TYPE);
237242
var code = 'var ' + varA + ' = ' + value_volume_ + ';\n' +
238-
' if(' + varA + '>=100){\n' +
239-
' ' + varA + '=100;\n' +
240-
' }\n' +
241-
' if(' + variable_name_ + '.playerReady){\n' +
242-
' ' + variable_name_ + '.setVolume(' + varA + ');\n' +
243-
' }\n';
243+
'if (' + varA + ' >= 100) {\n' +
244+
' ' + varA + ' = 100;\n' +
245+
'}\n' +
246+
'if (' + variable_name_ + '.playerReady) {\n' +
247+
' ' + variable_name_ + '.setVolume(' + varA + ');\n' +
248+
'}\n';
244249
return code;
245250
};
246251

247252

248253
Blockly.JavaScript['demo_youtube_speed'] = function (block) {
249254
var variable_name_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('name_'), Blockly.Variables.NAME_TYPE);
250255
var dropdown_speed_ = block.getFieldValue('speed_');
251-
var code = ' if(' + variable_name_ + '.playerReady){\n' +
252-
' ' + variable_name_ + '.setPlaybackRate(' + dropdown_speed_ + ');\n' +
253-
' }\n';
256+
var code = 'if (' + variable_name_ + '.playerReady) {\n' +
257+
' ' + variable_name_ + '.setPlaybackRate(' + dropdown_speed_ + ');\n' +
258+
'}\n';
254259
return code;
255260
};
256261

@@ -260,18 +265,18 @@ Blockly.JavaScript['demo_youtube_control'] = function (block) {
260265
var dropdown_status_ = block.getFieldValue('status_');
261266
var code;
262267
if (dropdown_status_ == '1') {
263-
code = ' if(' + variable_name_ + '.playerReady){\n' +
264-
' ' + variable_name_ + '.playVideo();\n' +
265-
' }\n';
268+
code = 'if (' + variable_name_ + '.playerReady) {\n' +
269+
' ' + variable_name_ + '.playVideo();\n' +
270+
'}\n';
266271
} else if (dropdown_status_ == '2') {
267-
code = ' if(' + variable_name_ + '.playerReady){\n' +
268-
' ' + variable_name_ + '.pauseVideo();\n' +
269-
' }\n';
272+
code = 'if (' + variable_name_ + '.playerReady) {\n' +
273+
' ' + variable_name_ + '.pauseVideo();\n' +
274+
'}\n';
270275
} else if (dropdown_status_ == '0') {
271-
code = ' if(' + variable_name_ + '.playerReady){\n' +
272-
' ' + variable_name_ + '.seekTo(0);\n' +
273-
' ' + variable_name_ + '.stopVideo();\n' +
274-
' }\n';
276+
code = 'if (' + variable_name_ + '.playerReady) {\n' +
277+
' ' + variable_name_ + '.seekTo(0);\n' +
278+
' ' + variable_name_ + '.stopVideo();\n' +
279+
'}\n';
275280
}
276281
return code;
277282
};
@@ -280,7 +285,7 @@ Blockly.JavaScript['demo_youtube_control'] = function (block) {
280285
Blockly.JavaScript['demo_youtube_status'] = function (block) {
281286
var variable_name_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('name_'), Blockly.Variables.NAME_TYPE);
282287
var dropdown_status_ = block.getFieldValue('status_');
283-
var code = variable_name_ + '.getPlayerState()==' + dropdown_status_;
288+
var code = variable_name_ + '.getPlayerState() == ' + dropdown_status_;
284289
return [code, Blockly.JavaScript.ORDER_ATOMIC];
285290
};
286291

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
<script src="lib/babel.min.js"></script>
3535
<script src="lib/jquery-2.2.1.min.js"></script>
3636
<script src="lib/firebase.js"></script>
37-
<script src="lib/iframe_api.js"></script>
3837
<script src="lib/clipboard.js"></script>
3938
<script src="lib/tracking-min.js"></script>
4039
<script src="lib/face-min.js"></script>

templates/demo-area-07.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
77
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>
88
<script src="https://blockly.webduino.io/lib/runtime.min.js"></script>
9-
<script src="https://www.youtube.com/iframe_api"></script>
109
</template>
1110
<template id="css">
1211
#player {

templates/tutorials/button-5.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<meta charset="utf-8">
33
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
44
<title>Webduino Blockly 課程 4-5:點擊按鈕開關控制 Youtube</title>
5-
<script src="https://www.youtube.com/iframe_api"></script>
65
<!-- webduino -->
76
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
87
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>

templates/tutorials/irrecv-3.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<meta charset="utf-8">
33
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
44
<title>Webduino Blockly - 超音波傳感器控制 youtube 的音量</title>
5-
<script src="https://www.youtube.com/iframe_api"></script>
65
<!-- webduino -->
76
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
87
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>

templates/tutorials/rfid-3.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<meta charset="utf-8">
33
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
44
<title>Webduino Blockly - RFID 控制 youtube</title>
5-
<script src="https://www.youtube.com/iframe_api"></script>
65
<!-- webduino -->
76
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
87
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>

templates/tutorials/ultrasonic-5.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<meta charset="utf-8">
33
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
44
<title>Webduino Blockly - 超音波傳感器控制 youtube 的音量</title>
5-
<script src="https://www.youtube.com/iframe_api"></script>
65
<!-- webduino -->
76
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
87
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>

templates/tutorials/ultrasonic-6.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<meta charset="utf-8">
33
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
44
<title>Webduino Blockly - 超音波傳感器控制 Youtube 播放速度</title>
5-
<script src="https://www.youtube.com/iframe_api"></script>
65
<!-- webduino -->
76
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
87
<script src="https://blockly.webduino.io/webduino-blockly.js"></script>

0 commit comments

Comments
 (0)