Skip to content

Commit 9d1e4a1

Browse files
committed
Second layer of refactoring for text from YouTube.
1 parent 6071980 commit 9d1e4a1

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

‎edit_texts.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ function edit_texts_new($lid)
619619
</td>
620620
</tr>
621621
<?php if (YT_API_KEY != null) {
622-
text_from_youtube\do_form_fragment();
622+
Lwt\Text_From_Youtube\do_form_fragment();
623623
} ?>
624624
<tr>
625625
<td class="td1 right" colspan="2">
@@ -639,7 +639,7 @@ function edit_texts_new($lid)
639639
<input type="button" value="Long Text Import" onclick="location.href='long_text_import.php';" />
640640
</p>
641641
<?php
642-
text_from_youtube\do_js();
642+
Lwt\Text_From_Youtube\do_js();
643643
}
644644

645645
/**

‎text_from_yt.php‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*
66
* You need a personal YouTube API key.
77
*/
8-
namespace text_from_youtube;
8+
9+
namespace Lwt\Text_From_Youtube;
910

1011
require_once 'inc/settings.php';
1112

@@ -35,33 +36,36 @@ function do_js()
3536
{
3637
?>
3738
<script>
38-
const API_KEY = document.getElementById('ytApiKey').value;
3939
function setYtDataStatus(msg) {
40-
const el = document.getElementById('ytDataStatus');
41-
el.textContent = msg;
40+
$('#ytDataStatus').text(msg);
4241
}
4342

43+
const fetching_success = function (data) {
44+
if (data.items.length == 0) {
45+
setYtDataStatus('No videos found.');
46+
} else {
47+
setYtDataStatus('Success!');
48+
const snippet = data.items[0].snippet;
49+
$('[name=TxTitle]').attr('value', snippet.title);
50+
$('[name=TxText]').attr('value', snippet.description);
51+
$('[name=TxSourceURI]').attr('value', `https://youtube.com/watch?v=${ytVideoId}`);
52+
}
53+
}
54+
4455
function getYtTextData() {
4556
setYtDataStatus('Fetching YouTube data...');
4657
const ytVideoId = document.getElementById('ytVideoId').value;
58+
const API_KEY = document.getElementById('ytApiKey').value;
4759
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${ytVideoId}&key=${API_KEY}`;
48-
console.log('fetching', url);
49-
const req = new XMLHttpRequest();
60+
/*const req = new XMLHttpRequest();
5061
req.onload = function(e) {
5162
const res = JSON.parse(req.responseText);
52-
if (res.items.length == 0) {
53-
setYtDataStatus('No videos found.');
54-
} else {
55-
setYtDataStatus('Success!');
56-
const snippet = res.items[0].snippet;
57-
$('[name=TxTitle]')[0].value = snippet.title;
58-
$('[name=TxText]')[0].value = snippet.description;
59-
$('[name=TxSourceURI]')[0].value = `https://youtube.com/watch?v=${ytVideoId}`;
60-
}
63+
fetching_success(res);
6164
};
6265
req.open('GET', url);
63-
req.send();
66+
req.send();*/
6467

68+
$.get(url, fetching_success);
6569
}
6670
</script>
6771
<?php

0 commit comments

Comments
 (0)