Skip to content

Instantly share code, notes, and snippets.

@gwarser
Created August 9, 2020 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gwarser/2d4b5dcd32b0d219e0ae65b43eb1f53d to your computer and use it in GitHub Desktop.
Save gwarser/2d4b5dcd32b0d219e0ae65b43eb1f53d to your computer and use it in GitHub Desktop.
Disqus click-to-load
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2019-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
// Source code as in https://github.com/gorhill/uBlock/commit/7c22a312945a2bff41a2b5696a7e54f1c4c01cf2
/// disqus_embed.js
(function() {
'use strict';
const p = document.getElementById(window.disqus_container_id || 'disqus_thread');
if ( p === null ) { return; }
const b = document.createElement('button');
b.textContent = 'Disqus blocked by uBlock Origin: click to unblock';
b.type = 'button';
p.appendChild(b);
const loadDisqus = function(ev) {
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();
newScript.src += '?_=1457540' + t.slice(-6);
document.body.appendChild(newScript);
ev.preventDefault();
ev.stopPropagation();
};
b.addEventListener('click', loadDisqus, { once: true });
})();
/// disqus_forums_embed.js
(function() {
'use strict';
const ee = document.getElementsByTagName('script');
let i = ee.length, src;
while ( i-- ) {
src = ee[i].src || '';
if ( src === '' ) {
continue;
}
if ( src.lastIndexOf('disqus.com/embed.js') === (src.length - 19) ) {
return;
}
}
const e = document.createElement('script');
e.async = true;
e.src = '//' + window.disqus_shortname + '.disqus.com/embed.js';
document.body.appendChild(e);
})();
! Title: Disqus click-to-load
! Expires: 30 days
! Description: load Disqus comments on demand only,
! so that no connection to `disqus.com` occurs by default when you
! land on a site which uses Disqus comments widget.
! These filters will cause a button labeled:
! "Disqus blocked by uBlock Origin: click to unblock" to be embedded
! where Disqus comments would normally be loaded. Click the button
! to force the comments to be loaded.
||disqus.com/forums/*/embed.js$script,redirect=disqus_forums_embed.js
||disqus.com/embed.js$script,redirect=disqus_embed.js
||disqus.com/count.js$script
@@||disqus.com/embed.js?_=1457540*$script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment