diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-20 10:14:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-20 10:14:28 +0200 |
commit | 9b3c1270bcbe4f8e30e0160da8badd43dd94871a (patch) | |
tree | f1d87ed084970cf0c3ef99fef8e4ad6dcea423ce /assets/common | |
parent | 552c3b942afe565b780785eab5b2e29c1e800c2e (diff) | |
parent | 5334090be04e66da5cb5c3ad487604b3733c5cac (diff) | |
download | Shaarli-9b3c1270bcbe4f8e30e0160da8badd43dd94871a.tar.gz Shaarli-9b3c1270bcbe4f8e30e0160da8badd43dd94871a.tar.zst Shaarli-9b3c1270bcbe4f8e30e0160da8badd43dd94871a.zip |
Merge pull request #1567 from ArthurHoaro/feature/async-title-retrieval
Diffstat (limited to 'assets/common')
-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 | })(); | ||