]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - assets/common/js/metadata.js
4 * This script is used to retrieve bookmarks metadata asynchronously:
5 * - title, description and keywords while creating a new bookmark
6 * - thumbnails while visiting the bookmark list
8 * Note: it should only be included if the user is logged in
9 * and the setting general.enable_async_metadata is enabled.
13 * Removes given input loaders - used in edit link template.
15 * @param {object} loaders List of input DOM element that need to be cleared
17 function clearLoaders(loaders
) {
18 if (loaders
!= null && loaders
.length
> 0) {
19 [...loaders
].forEach((loader
) => {
20 loader
.classList
.remove('loading-input');
26 * AJAX request to update the thumbnail of a bookmark with the provided ID.
27 * If a thumbnail is retrieved, it updates the divElement with the image src, and displays it.
29 * @param {string} basePath Shaarli subfolder for XHR requests
30 * @param {object} divElement Main <div> DOM element containing the thumbnail placeholder
31 * @param {int} id Bookmark ID to update
33 function updateThumb(basePath
, divElement
, id
) {
34 const xhr
= new XMLHttpRequest();
35 xhr
.open('PATCH', `${basePath}/admin/shaare/${id}/update-thumbnail`);
36 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
37 xhr
.responseType
= 'json';
39 if (xhr
.status
!== 200) {
40 alert(`An error occurred. Return code: ${xhr.status}`);
42 const { response
} = xhr
;
44 if (response
.thumbnail
!== false) {
45 const imgElement
= divElement
.querySelector('img');
47 imgElement
.src
= response
.thumbnail
;
48 imgElement
.dataset
.src
= response
.thumbnail
;
49 imgElement
.style
.opacity
= '1';
50 divElement
.classList
.remove('hidden');
58 const basePath
= document
.querySelector('input[name="js_base_path"]').value
;
61 * METADATA FOR EDIT BOOKMARK PAGE
63 const inputTitles
= document
.querySelectorAll('input[name="lf_title"]');
64 if (inputTitles
!= null) {
65 [...inputTitles
].forEach((inputTitle
) => {
66 const form
= inputTitle
.closest('form[name="linkform"]');
67 const loaders
= form
.querySelectorAll('.loading-input');
69 if (inputTitle
.value
.length
> 0) {
70 clearLoaders(loaders
);
74 const url
= form
.querySelector('input[name="lf_url"]').value
;
76 const xhr
= new XMLHttpRequest();
77 xhr
.open('GET', `${basePath}/admin/metadata?url=${encodeURI(url)}`, true);
78 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
80 const result
= JSON
.parse(xhr
.response
);
81 Object
.keys(result
).forEach((key
) => {
82 if (result
[key
] !== null && result
[key
].length
) {
83 const element
= form
.querySelector(`input[name="lf_${key}"], textarea[name="lf_${key}"]`);
84 if (element
!= null && element
.value
.length
=== 0) {
85 element
.value
= he
.decode(result
[key
]);
89 clearLoaders(loaders
);
97 * METADATA FOR THUMBNAIL RETRIEVAL
99 const thumbsToLoad
= document
.querySelectorAll('div[data-async-thumbnail]');
100 if (thumbsToLoad
!= null) {
101 [...thumbsToLoad
].forEach((divElement
) => {
102 const { id
} = divElement
.closest('[data-id]').dataset
;
104 updateThumb(basePath
, divElement
, id
);