X-Git-Url: https://git.ao2.it/GM_direct_download_links.git/blobdiff_plain/4b6c78612d879d049dc2233db870018fb133acaf..9fc53115a6ded84ac5e87773e4bfc38f9a801115:/direct_download_links.user.js diff --git a/direct_download_links.user.js b/direct_download_links.user.js index eb4175a..6e4bbfd 100644 --- a/direct_download_links.user.js +++ b/direct_download_links.user.js @@ -140,6 +140,7 @@ function direct_download_link_add(pageURL, site) { } _get_URL(site, element); }, false); + return; } _get_URL(site, element); @@ -179,22 +180,30 @@ function _add_link(e) { var site = e.site; var URL = e.URL;; - var links = document.getElementById(site.linkDest); - if (!links) { + var destination = document.getElementById(site.linkDest); + if (!destination) { DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.'); return; } - var download_link = document.createElement('a'); - download_link.textContent = 'Direct Link'; - download_link.setAttribute('href', URL); - var style = 'background-color: white; color: blue;'; - style += ' border: 2px solid red;' - style += ' float: right; font-size: large;'; - style += ' padding: .5em; margin: 1em;' - download_link.setAttribute('style', style); - - links.insertBefore(download_link, links.firstChild); + // Check if we added the link already, if so just update the href attribute. + // This is useful when _get_URL() is called on async events. + var download_link = document.getElementById('GM_direct_downaload_link'); + if (download_link) { + download_link.setAttribute('href', URL); + } else { + download_link = document.createElement('a'); + download_link.textContent = 'Direct Link'; + download_link.setAttribute('id', 'GM_direct_downaload_link'); + download_link.setAttribute('href', URL); + var style = 'background-color: white; color: blue;'; + style += ' border: 2px solid red;' + style += ' float: right; font-size: large;'; + style += ' padding: .5em; margin: 1em;' + download_link.setAttribute('style', style); + + destination.insertBefore(download_link, destination.firstChild); + } } function DDL_log(message) {