From 58ccb9a26d9cac8eb0f54eef5d4638994bb7b4fb Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 24 Mar 2020 09:59:42 -0400 Subject: [PATCH] Improve disqus_embed.js scriptlet Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/773 The problematic site does not declare the expected `disqus_shortname` global variable, so the scriptlet has been extended to deal with such occurrence. --- src/web_accessible_resources/disqus_embed.js | 30 +++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/web_accessible_resources/disqus_embed.js b/src/web_accessible_resources/disqus_embed.js index acbb4ea68..571e2ef60 100644 --- a/src/web_accessible_resources/disqus_embed.js +++ b/src/web_accessible_resources/disqus_embed.js @@ -28,15 +28,31 @@ b.type = 'button'; p.appendChild(b); const loadDisqus = function(ev) { - b.removeEventListener('click', loadDisqus); - p.removeChild(b); - const script = document.createElement('script'); - script.async = true; + b.remove(); + let disqusScript = + document.querySelector('script[src$=".disqus.com/embed.js"]'); + let newScript; + if ( disqusScript !== null ) { + disqusScript.remove(); + newScript = document.createElement('script'); + if ( disqusScript.hasAttributes() ) { + const attrs = disqusScript.attributes; + for ( let i = 0; i < attrs.length; i++ ) { + const attr = attrs[i]; + newScript.setAttribute(attr.name, attr.value); + } + } + } else if ( typeof self.disqus_shortname === 'string' ) { + newScript = document.createElement('script'); + newScript.async = true; + newScript.src = `//${self.disqus_shortname}.disqus.com/embed.js`; + } + if ( newScript === undefined ) { return; } const t = Date.now().toString(); - script.src = '//' + window.disqus_shortname + '.disqus.com/embed.js?_=1457540' + t.slice(-6); - document.body.appendChild(script); + newScript.src += '?_=1457540' + t.slice(-6); + document.body.appendChild(newScript); ev.preventDefault(); ev.stopPropagation(); }; - b.addEventListener('click', loadDisqus); + b.addEventListener('click', loadDisqus, { once: true }); })();