Skip to content

Instantly share code, notes, and snippets.

@deadflowers
Last active December 20, 2024 08:33
Show Gist options
  • Select an option

  • Save deadflowers/276f53b4a018d10879e6d6d340cc8ef8 to your computer and use it in GitHub Desktop.

Select an option

Save deadflowers/276f53b4a018d10879e6d6d340cc8ef8 to your computer and use it in GitHub Desktop.
reader mode test

BeQuiet Reader Mode Bookmarklet

Script to launch by URL that scrubs a page of most color and graphics including many ads. Useful for trying to read a page that;s throwing so many ads it's locking up your device or otherwise providing a difficult experience.

new in this version:

  • DIV background should be white unless it's already a shade of grey.
  • <a href> elements should be treated as buttons if they indicate so via class or id names, otherwise just bold the text.
  • Icons and logos identified by name, id, class, or size should get the grayscale treatment.
  • Remove the blue border-just didnt work aesthetically.
  • Handle ads from doubleclick.netand googlesyndication.comwith cross-out SVGs and ensure styles use !important.

note this is just a test experiement one of many along these lines but does work

javascript: (function() {
function removeElements() {
document.querySelectorAll('iframe, img, video, object, embed').forEach(function(el) {
var box = document.createElement('div');
box.style.width = el.offsetWidth + 'px';
box.style.height = el.offsetHeight + 'px';
box.style.backgroundColor = '#fff';
box.style.color = '#1b1b1b';
box.style.display = 'flex';
box.style.alignItems = 'center';
box.style.justifyContent = 'center';
box.style.fontSize = '11px';
box.style.textAlign = 'center';
box.style.fontWeight = 'bold';
box.innerHTML = 'shhh be quiet';
el.replaceWith(box);
});
}
function changeColors() {
document.body.style.backgroundColor = '#fff !important';
document.body.style.color = '#1b1b1b !important';
bgsvg='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 2400"><path fill="#fff" d="M0 0h4000v2400H0z"/></svg>'; var bgencoded = window.btoa(bgsvg); document.body.style.background = "url(data:image/svg+xml;base64,"+bgencoded+")";
document.querySelectorAll('*').forEach(function(el) {
var bg = window.getComputedStyle(el).backgroundColor;
if (bg !== 'rgb(255, 255, 255)' && bg !== 'rgb(0, 0, 0)' && bg !== 'rgb(169, 169, 169)' && bg !== 'rgb(245, 245, 245)' && bg !== 'rgb(211, 211, 211)') {
el.style.background = "url(data:image/svg+xml;base64,"+bgencoded+")";
el.style.backgroundColor = '#fff';
}
if (window.getComputedStyle(el).color !== 'rgb(27, 27, 27)') {
el.style.color = '#1b1b1b';
}
});
}
function createPlaceholderSVG(width, height) {
const svg = `
<svg class="svgbob" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
<style>
.svgbob line { stroke: #1DA1F2; stroke-width: 1; }
.svgbob rect.backdrop { fill: white; }
</style>
<rect class="backdrop" width="${width}" height="${height}"/>
<line class="solid" x1="0" y1="0" x2="${width}" y2="${height}"/>
</svg>`;
return `data:image/svg+xml;base64,${btoa(svg)}`;
}
removeElements();
changeColors();
document.querySelectorAll('img[src*="carbonads.net"], iframe[src*="carbonads.net"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"],img[src*="googlesyndication.com"],iframe[src*="googlesyndication.com"],img[src*="adsafeprotected.com"], iframe[src*="adsafeprotected.com"],img[src*="teads.tv"], iframe[src*="teads.tv"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach(el => { const computedStyle = window.getComputedStyle(el); const width = el.clientWidth || 100; const height = el.clientHeight || 100; el.style.backgroundColor = 'white !important'; el.src = createPlaceholderSVG(width, height); });
window.addEventListener('DOMNodeInserted', function(e) {
if (e.target.tagName === 'IFRAME' || e.target.tagName === 'IMG' || e.target.tagName === 'VIDEO') {
e.target.style.display = 'none';
}
});
})();
!function removeElements(){document.querySelectorAll("iframe, img, video, object, embed").forEach((function(el){var box=document.createElement("div");box.style.width=el.offsetWidth+"px",box.style.height=el.offsetHeight+"px",box.style.backgroundColor="#fff",box.style.color="#1b1b1b",box.style.display="flex",box.style.alignItems="center",box.style.justifyContent="center",box.style.fontSize="11px",box.style.textAlign="center",box.style.fontWeight="bold",box.innerHTML="shhh be quiet",el.replaceWith(box)}))}(),function changeColors(){document.body.style.backgroundColor="#fff !important",document.body.style.color="#1b1b1b !important",bgsvg='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 2400"><path fill="#fff" d="M0 0h4000v2400H0z"/></svg>';var bgencoded=window.btoa(bgsvg);document.body.style.background="url(data:image/svg+xml;base64,"+bgencoded+")",document.querySelectorAll("*").forEach((function(el){var bg=window.getComputedStyle(el).backgroundColor;"rgb(255, 255, 255)"!==bg&&"rgb(0, 0, 0)"!==bg&&"rgb(169, 169, 169)"!==bg&&"rgb(245, 245, 245)"!==bg&&"rgb(211, 211, 211)"!==bg&&(el.style.background="url(data:image/svg+xml;base64,"+bgencoded+")",el.style.backgroundColor="#fff"),"rgb(27, 27, 27)"!==window.getComputedStyle(el).color&&(el.style.color="#1b1b1b")}))}(),document.querySelectorAll('img[src*="carbonads.net"], iframe[src*="carbonads.net"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"],img[src*="googlesyndication.com"],iframe[src*="googlesyndication.com"],img[src*="adsafeprotected.com"], iframe[src*="adsafeprotected.com"],img[src*="teads.tv"], iframe[src*="teads.tv"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach((el=>{window.getComputedStyle(el);const width=el.clientWidth||100,height=el.clientHeight||100;el.style.backgroundColor="white !important",el.src=function createPlaceholderSVG(width,height){return`data:image/svg+xml;base64,${btoa(`\n <svg class="svgbob" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">\n <style>\n .svgbob line { stroke: #1DA1F2; stroke-width: 1; }\n .svgbob rect.backdrop { fill: white; }\n </style>\n <rect class="backdrop" width="${width}" height="${height}"/>\n <line class="solid" x1="0" y1="0" x2="${width}" y2="${height}"/>\n </svg>`)}`}(width,height)})),window.addEventListener("DOMNodeInserted",(function(e){"IFRAME"!==e.target.tagName&&"IMG"!==e.target.tagName&&"VIDEO"!==e.target.tagName||(e.target.style.display="none")}));
(function() {
function removeElements() {
document.querySelectorAll('iframe, img, video, object, embed').forEach(function(el) {
var box = document.createElement('div');
box.style.width = el.offsetWidth + 'px';
box.style.height = el.offsetHeight + 'px';
box.style.backgroundColor = '#fff';
box.style.color = '#222';
box.style.display = 'flex';
box.style.alignItems = 'center';
box.style.justifyContent = 'center';
box.style.fontSize = '11px';
box.style.textAlign = 'center';
box.style.fontWeight = 'bold';
box.innerHTML = 'shhh be quiet';
el.replaceWith(box);
});
}
function changeColors() {
document.body.style.backgroundColor = '#fff !important';
document.body.style.color = '#1b1b1b !important';
document.body.backgroundImage = 'none !important';
document.querySelectorAll('*').forEach(function(el) {
var bg = window.getComputedStyle(el).backgroundColor;
if (bg !== 'rgb(255, 255, 255)' && bg !== 'rgb(0, 0, 0)' && bg !== 'rgb(169, 169, 169)' && bg !== 'rgb(245, 245, 245)' && bg !== 'rgb(211, 211, 211)') {
el.style.backgroundColor = '#fff';
}
if (window.getComputedStyle(el).color !== 'rgb(27, 27, 27)') {
el.style.color = '#1b1b1b';
}
});
}
function createPlaceholderSVG(width, height) {
const svg = `
<svg class="svgbob" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
<style>
.svgbob line { stroke: #1DA1F2; stroke-width: 1; }
.svgbob rect.backdrop { fill: white; }
</style>
<rect class="backdrop" width="${width}" height="${height}"/>
<line class="solid" x1="0" y1="0" x2="${width}" y2="${height}"/>
</svg>`;
return `data:image/svg+xml;base64,${btoa(svg)}`;
}
removeElements();
changeColors();
document.querySelectorAll('img[src*="carbonads.net"], iframe[src*="carbonads.net"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"],img[src*="googlesyndication.com"],iframe[src*="googlesyndication.com"],img[src*="adsafeprotected.com"], iframe[src*="adsafeprotected.com"],img[src*="teads.tv"], iframe[src*="teads.tv"],img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach(el => { const computedStyle = window.getComputedStyle(el); const width = el.clientWidth || 100; const height = el.clientHeight || 100; el.style.backgroundColor = 'white !important'; el.src = createPlaceholderSVG(width, height); });
window.addEventListener('DOMNodeInserted', function(e) {
if (e.target.tagName === 'IFRAME' || e.target.tagName === 'IMG' || e.target.tagName === 'VIDEO') {
e.target.style.display = 'none';
}
});
})();
javascript:!function(){function t(t){const e=window.getComputedStyle(t),o=e.color.match(/\d+/g).map(Number),[n,l,r]=o;n+l+r>300&&(t.style.color="#222 !important"),("bold"===e.fontWeight||parseInt(e.fontWeight)>=700)&&(t.style.color="#000 !important")}function e(t,e){return`data:image/svg+xml;base64,${btoa(`\n <svg class="svgbob" viewBox="0 0 ${t} ${e}" xmlns="http://www.w3.org/2000/svg">\n <style>\n .svgbob line { stroke: #1DA1F2; stroke-width: 1; }\n .svgbob rect.backdrop { fill: white; }\n </style>\n <rect class="backdrop" width="${t}" height="${e}"/>\n <line class="solid" x1="0" y1="0" x2="${t}" y2="${e}"/>\n </svg>`)}`}!function(){!function(){const t=document.createElement("style");t.innerHTML='\n img { filter: grayscale(100%) !important; transition: filter 0.5s !important; }\n a { color: #000 !important; }\n svg, img[src*=".svg"], img[src*=".gif"], img[src*="logo"] { filter: grayscale(100%) !important; }\n ',document.head.appendChild(t)}(),document.querySelectorAll("body *").forEach(t),document.querySelectorAll("a").forEach((t=>{t.className.includes("btn")||t.id.includes("btn")||t.className.includes("button")||t.id.includes("button")?(t.style.backgroundColor="#FFFFFF",t.style.color="#000",t.style.border="2px solid #000",t.style.fontWeight="bold",t.style.padding="5px 10px",t.style.textDecoration="none",t.style.display="inline-block",t.addEventListener("mousedown",(()=>t.style.transform="scale(0.98)")),t.addEventListener("mouseup",(()=>t.style.transform="scale(1)"))):(t.style.fontWeight="bold",t.style.color="#000 !important");const e=window.getComputedStyle(t).backgroundColor.match(/\d+/g).map(Number),[o,n,l]=e;o+n+l>300&&(t.style.backgroundColor="#FFFFFF !important")})),document.querySelectorAll('img, iframe, video, [class*="ad"], [id*="ad"]').forEach((t=>{window.getComputedStyle(t);const o=t.clientWidth||100,n=t.clientHeight||100;t.style.backgroundColor="white !important",t.src=e(o,n)})),document.querySelectorAll('img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach((t=>{window.getComputedStyle(t);const o=t.clientWidth||100,n=t.clientHeight||100;t.style.backgroundColor="white !important",t.src=e(o,n)}));const o=document.createElement("style");o.innerHTML="::selection { background: #FFD700; }",document.head.appendChild(o),document.querySelectorAll("button").forEach((t=>{t.style.backgroundColor="#FFFFFF",t.style.color="#000",t.style.border="2px solid #000",t.style.fontWeight="bold",t.addEventListener("mousedown",(()=>t.style.transform="scale(0.98)")),t.addEventListener("mouseup",(()=>t.style.transform="scale(1)"))})),document.querySelectorAll("div").forEach((t=>{const e=window.getComputedStyle(t).backgroundColor.match(/\d+/g).map(Number),[o,n,l]=e;o+n+l>540&&(t.style.backgroundColor="#FFFFFF !important")}))}()}();
(function() {
// Function to apply grayscale to images
function applyGrayscaleCSS() {
const style = document.createElement('style');
style.innerHTML = `
img { filter: grayscale(100%) !important; transition: filter 0.5s !important; }
a { color: #000 !important; }
svg, img[src*=".svg"], img[src*=".gif"], img[src*="logo"] { filter: grayscale(100%) !important; }
`;
document.head.appendChild(style);
}
// Function to classify and change text color
function changeTextColor(element) {
const style = window.getComputedStyle(element);
const color = style.color.match(/\d+/g).map(Number);
const [r, g, b] = color;
if (r + g + b > 300) { // Not dark enough
element.style.color = '#222 !important';
}
if (style.fontWeight === 'bold' || parseInt(style.fontWeight) >= 700) {
element.style.color = '#000 !important';
}
}
// Function to handle button-like links
function handleLinks() {
document.querySelectorAll('a').forEach(a => {
if (a.className.includes('btn') || a.id.includes('btn') || a.className.includes('button') || a.id.includes('button')) {
a.style.backgroundColor = '#FFFFFF';
a.style.color = '#000';
a.style.border = '2px solid #000';
a.style.fontWeight = 'bold';
a.style.padding = '5px 10px';
a.style.textDecoration = 'none';
a.style.display = 'inline-block';
a.addEventListener('mousedown', () => a.style.transform = 'scale(0.98)');
a.addEventListener('mouseup', () => a.style.transform = 'scale(1)');
} else {
a.style.fontWeight = 'bold';
a.style.color = '#000 !important';
}
// Change background color for styled anchor elements
const bgColor = window.getComputedStyle(a).backgroundColor;
const rgb = bgColor.match(/\d+/g).map(Number);
const [r, g, b] = rgb;
if (r + g + b > 300) { // Light colors
a.style.backgroundColor = '#FFFFFF !important';
}
});
}
// Function to create SVG placeholder
function createPlaceholderSVG(width, height) {
const svg = `
<svg class="svgbob" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
<style>
.svgbob line { stroke: #1DA1F2; stroke-width: 1; }
.svgbob rect.backdrop { fill: white; }
</style>
<rect class="backdrop" width="${width}" height="${height}"/>
<line class="solid" x1="0" y1="0" x2="${width}" y2="${height}"/>
</svg>`;
return `data:image/svg+xml;base64,${btoa(svg)}`;
}
// Apply changes
function applyChanges() {
// Apply grayscale to images
applyGrayscaleCSS();
// Change text color
document.querySelectorAll('body *').forEach(changeTextColor);
// Handle button-like links
handleLinks();
// Replace images and iframes with placeholders
document.querySelectorAll('img, iframe, video, [class*="ad"], [id*="ad"]').forEach(el => {
const computedStyle = window.getComputedStyle(el);
const width = el.clientWidth || 100;
const height = el.clientHeight || 100;
el.style.backgroundColor = 'white !important';
el.src = createPlaceholderSVG(width, height);
});
// Handle specific ad domains
document.querySelectorAll('img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach(el => {
const computedStyle = window.getComputedStyle(el);
const width = el.clientWidth || 100;
const height = el.clientHeight || 100;
el.style.backgroundColor = 'white !important';
el.src = createPlaceholderSVG(width, height);
});
// Handle text selection highlighting
const selectionStyle = document.createElement('style');
selectionStyle.innerHTML = '::selection { background: #FFD700; }';
document.head.appendChild(selectionStyle);
// Make buttons stand out
document.querySelectorAll('button').forEach(button => {
button.style.backgroundColor = '#FFFFFF';
button.style.color = '#000';
button.style.border = '2px solid #000';
button.style.fontWeight = 'bold';
button.addEventListener('mousedown', () => button.style.transform = 'scale(0.98)');
button.addEventListener('mouseup', () => button.style.transform = 'scale(1)');
});
// Change background color of DIVs if not light grey
document.querySelectorAll('div').forEach(div => {
const bgColor = window.getComputedStyle(div).backgroundColor;
const rgb = bgColor.match(/\d+/g).map(Number);
const [r, g, b] = rgb;
if (r + g + b > 540) { // Light grey threshold
div.style.backgroundColor = '#FFFFFF !important';
}
});
}
applyChanges();
})();
!function(){function t(t){const e=window.getComputedStyle(t),o=e.color.match(/\d+/g).map(Number),[n,l,r]=o;n+l+r>300&&(t.style.color="#222 !important"),("bold"===e.fontWeight||parseInt(e.fontWeight)>=700)&&(t.style.color="#000 !important")}function e(t,e){return`data:image/svg+xml;base64,${btoa(`\n <svg class="svgbob" viewBox="0 0 ${t} ${e}" xmlns="http://www.w3.org/2000/svg">\n <style>\n .svgbob line { stroke: #1DA1F2; stroke-width: 1; }\n .svgbob rect.backdrop { fill: white; }\n </style>\n <rect class="backdrop" width="${t}" height="${e}"/>\n <line class="solid" x1="0" y1="0" x2="${t}" y2="${e}"/>\n </svg>`)}`}!function(){!function(){const t=document.createElement("style");t.innerHTML='\n img { filter: grayscale(100%) !important; transition: filter 0.5s !important; }\n a { color: #000 !important; }\n svg, img[src*=".svg"], img[src*=".gif"], img[src*="logo"] { filter: grayscale(100%) !important; }\n ',document.head.appendChild(t)}(),document.querySelectorAll("body *").forEach(t),document.querySelectorAll("a").forEach((t=>{t.className.includes("btn")||t.id.includes("btn")||t.className.includes("button")||t.id.includes("button")?(t.style.backgroundColor="#FFFFFF",t.style.color="#000",t.style.border="2px solid #000",t.style.fontWeight="bold",t.style.padding="5px 10px",t.style.textDecoration="none",t.style.display="inline-block",t.addEventListener("mousedown",(()=>t.style.transform="scale(0.98)")),t.addEventListener("mouseup",(()=>t.style.transform="scale(1)"))):(t.style.fontWeight="bold",t.style.color="#000 !important");const e=window.getComputedStyle(t).backgroundColor.match(/\d+/g).map(Number),[o,n,l]=e;o+n+l>300&&(t.style.backgroundColor="#FFFFFF !important")})),document.querySelectorAll('img, iframe, video, [class*="ad"], [id*="ad"]').forEach((t=>{window.getComputedStyle(t);const o=t.clientWidth||100,n=t.clientHeight||100;t.style.backgroundColor="white !important",t.src=e(o,n)})),document.querySelectorAll('img[src*="doubleclick.net"], iframe[src*="doubleclick.net"], img[src*="googlesyndication.com"], iframe[src*="googlesyndication.com"]').forEach((t=>{window.getComputedStyle(t);const o=t.clientWidth||100,n=t.clientHeight||100;t.style.backgroundColor="white !important",t.src=e(o,n)}));const o=document.createElement("style");o.innerHTML="::selection { background: #FFD700; }",document.head.appendChild(o),document.querySelectorAll("button").forEach((t=>{t.style.backgroundColor="#FFFFFF",t.style.color="#000",t.style.border="2px solid #000",t.style.fontWeight="bold",t.addEventListener("mousedown",(()=>t.style.transform="scale(0.98)")),t.addEventListener("mouseup",(()=>t.style.transform="scale(1)"))})),document.querySelectorAll("div").forEach((t=>{const e=window.getComputedStyle(t).backgroundColor.match(/\d+/g).map(Number),[o,n,l]=e;o+n+l>540&&(t.style.backgroundColor="#FFFFFF !important")}))}()}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment