|
5 | 5 | * |
6 | 6 | * You need a personal YouTube API key. |
7 | 7 | */ |
8 | | -namespace text_from_youtube; |
| 8 | + |
| 9 | +namespace Lwt\Text_From_Youtube; |
9 | 10 |
|
10 | 11 | require_once 'inc/settings.php'; |
11 | 12 |
|
@@ -35,33 +36,36 @@ function do_js() |
35 | 36 | { |
36 | 37 | ?> |
37 | 38 | <script> |
38 | | -const API_KEY = document.getElementById('ytApiKey').value; |
39 | 39 | function setYtDataStatus(msg) { |
40 | | - const el = document.getElementById('ytDataStatus'); |
41 | | - el.textContent = msg; |
| 40 | + $('#ytDataStatus').text(msg); |
42 | 41 | } |
43 | 42 |
|
| 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 | + |
44 | 55 | function getYtTextData() { |
45 | 56 | setYtDataStatus('Fetching YouTube data...'); |
46 | 57 | const ytVideoId = document.getElementById('ytVideoId').value; |
| 58 | + const API_KEY = document.getElementById('ytApiKey').value; |
47 | 59 | 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(); |
50 | 61 | req.onload = function(e) { |
51 | 62 | 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); |
61 | 64 | }; |
62 | 65 | req.open('GET', url); |
63 | | - req.send(); |
| 66 | + req.send();*/ |
64 | 67 |
|
| 68 | + $.get(url, fetching_success); |
65 | 69 | } |
66 | 70 | </script> |
67 | 71 | <?php |
|
0 commit comments