Improve disqus_embed.js scriptlet

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/959

The problematic site does not declare the expected
`disqus_shortname` global variable, so the scriptlet
has been extended to deal with such occurrence.
This commit is contained in:
Raymond Hill 2020-03-24 09:59:42 -04:00
parent 11d24abea0
commit 2fcc41f641
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -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 });
})();