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
1 changed files with 23 additions and 7 deletions

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