diff options
Diffstat (limited to 'assets/common/js/metadata.js')
-rw-r--r-- | assets/common/js/metadata.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/assets/common/js/metadata.js b/assets/common/js/metadata.js new file mode 100644 index 00000000..5200b481 --- /dev/null +++ b/assets/common/js/metadata.js | |||
@@ -0,0 +1,39 @@ | |||
1 | import he from 'he'; | ||
2 | |||
3 | function clearLoaders(loaders) { | ||
4 | if (loaders != null && loaders.length > 0) { | ||
5 | [...loaders].forEach((loader) => { | ||
6 | loader.classList.remove('loading-input'); | ||
7 | }); | ||
8 | } | ||
9 | } | ||
10 | |||
11 | (() => { | ||
12 | const loaders = document.querySelectorAll('.loading-input'); | ||
13 | const inputTitle = document.querySelector('input[name="lf_title"]'); | ||
14 | if (inputTitle != null && inputTitle.value.length > 0) { | ||
15 | clearLoaders(loaders); | ||
16 | return; | ||
17 | } | ||
18 | |||
19 | const url = document.querySelector('input[name="lf_url"]').value; | ||
20 | const basePath = document.querySelector('input[name="js_base_path"]').value; | ||
21 | |||
22 | const xhr = new XMLHttpRequest(); | ||
23 | xhr.open('GET', `${basePath}/admin/metadata?url=${encodeURI(url)}`, true); | ||
24 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
25 | xhr.onload = () => { | ||
26 | const result = JSON.parse(xhr.response); | ||
27 | Object.keys(result).forEach((key) => { | ||
28 | if (result[key] !== null && result[key].length) { | ||
29 | const element = document.querySelector(`input[name="lf_${key}"], textarea[name="lf_${key}"]`); | ||
30 | if (element != null && element.value.length === 0) { | ||
31 | element.value = he.decode(result[key]); | ||
32 | } | ||
33 | } | ||
34 | }); | ||
35 | clearLoaders(loaders); | ||
36 | }; | ||
37 | |||
38 | xhr.send(); | ||
39 | })(); | ||