mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 08:17:16 +00:00
250 lines
8.8 KiB
JavaScript
250 lines
8.8 KiB
JavaScript
// Here we are making iframes responsive. Particularly useful for inline Youtube videos.
|
||
function wrapFrames() {
|
||
document.querySelectorAll("iframe").forEach(element => {
|
||
if (getComputedStyle(element).display === "none") {
|
||
return;
|
||
}
|
||
if (parseInt(element.height) > 0) {
|
||
preserveAspectRatioOfFixedSizeFrame(element);
|
||
return;
|
||
}
|
||
var wrapper = document.createElement("div");
|
||
wrapper.classList.add("iframeWrap");
|
||
element.parentNode.insertBefore(wrapper, element);
|
||
wrapper.appendChild(element);
|
||
});
|
||
}
|
||
|
||
// The stylesheet clamps iframes to the article width with max-width: 100%,
|
||
// which leaves an attribute-sized iframe with its full height — distorted,
|
||
// and a huge blank box when the embed fails to load. Let the height follow
|
||
// the width instead.
|
||
function preserveAspectRatioOfFixedSizeFrame(element) {
|
||
const numeric = /^[1-9]\d*$/;
|
||
const width = element.getAttribute("width");
|
||
const height = element.getAttribute("height");
|
||
if (!numeric.test(width) || !numeric.test(height) || element.style.height) {
|
||
return;
|
||
}
|
||
element.style.aspectRatio = width + " / " + height;
|
||
element.style.height = "auto";
|
||
}
|
||
|
||
// Strip out color and font styling
|
||
|
||
function stripStylesFromElement(element, propertiesToStrip) {
|
||
for (name of propertiesToStrip) {
|
||
element.style.removeProperty(name);
|
||
}
|
||
}
|
||
|
||
// Strip inline styles that could harm readability.
|
||
function stripStyles() {
|
||
document.getElementsByTagName("body")[0].querySelectorAll("style, link[rel=stylesheet]").forEach(element => element.remove());
|
||
// Removing "background" and "font" will also remove properties that would be reflected in them, e.g., "background-color" and "font-family"
|
||
// The stylesheet forces images to height: auto, so an author aspect-ratio box can't be filled by its image — content after the box overlaps the overflowing image.
|
||
// Iframes keep their aspect-ratio: without it an iframe with no pixel height collapses to the default 150px.
|
||
document.getElementsByTagName("body")[0].querySelectorAll("[style]").forEach(element => {
|
||
const isIframe = element.tagName === "IFRAME";
|
||
stripStylesFromElement(element, isIframe ? ["color", "background", "font", "max-width", "max-height", "position"] : ["color", "background", "font", "max-width", "max-height", "position", "aspect-ratio"]);
|
||
});
|
||
}
|
||
|
||
// Constrain the height of iframes whose heights are defined relative to the document body to be at most
|
||
// 50% of the viewport width.
|
||
function constrainBodyRelativeIframes() {
|
||
let iframes = document.getElementsByTagName("iframe");
|
||
|
||
for (iframe of iframes) {
|
||
// The article body container is positioned, so it — not document.body —
|
||
// is the offsetParent for iframes with no positioned ancestor in the content.
|
||
const offsetParent = iframe.offsetParent;
|
||
if (offsetParent === document.body || (offsetParent && (offsetParent.classList.contains("articleBody") || offsetParent.classList.contains("article-body")))) {
|
||
let heightAttribute = iframe.style.height;
|
||
|
||
if (/%|vw|vh$/i.test(heightAttribute)) {
|
||
iframe.classList.add("nnw-constrained");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// An image served from the timeline’s copy falls back to its original URL when the copy can’t be loaded.
|
||
// Registered at document start, and capturing, since error events don’t bubble.
|
||
document.addEventListener("error", event => {
|
||
const element = event.target;
|
||
if (element instanceof HTMLImageElement && element.hasAttribute("data-nnw-src")) {
|
||
const originalSrc = element.getAttribute("data-nnw-src");
|
||
element.removeAttribute("data-nnw-src");
|
||
element.src = originalSrc;
|
||
}
|
||
}, true);
|
||
|
||
// Convert all Feedbin proxy images to be used as src, otherwise change image locations to be absolute if not already
|
||
function convertImgSrc() {
|
||
document.querySelectorAll("img").forEach(element => {
|
||
if (element.hasAttribute("data-canonical-src")) {
|
||
element.src = element.getAttribute("data-canonical-src")
|
||
} else if (!element.hasAttribute("data-nnw-src") && !/^[a-z]+\:\/\//i.test(element.src)) {
|
||
element.src = new URL(element.src, document.baseURI).href;
|
||
}
|
||
});
|
||
}
|
||
|
||
// Wrap tables in an overflow-x: auto; div
|
||
function wrapTables() {
|
||
var tables = document.querySelectorAll("div.articleBody table");
|
||
|
||
for (table of tables) {
|
||
var wrapper = document.createElement("div");
|
||
wrapper.className = "nnw-overflow";
|
||
table.parentNode.insertBefore(wrapper, table);
|
||
wrapper.appendChild(table);
|
||
|
||
// A negative side margin — usually from the site's own page layout —
|
||
// would put part of the table where the wrapper can't scroll to it.
|
||
if (parseFloat(getComputedStyle(table).marginLeft) < 0) {
|
||
table.style.marginLeft = "0";
|
||
}
|
||
if (parseFloat(getComputedStyle(table).marginRight) < 0) {
|
||
table.style.marginRight = "0";
|
||
}
|
||
}
|
||
}
|
||
|
||
// Add the playsinline attribute to any HTML5 videos that don"t have it.
|
||
// Without this attribute videos may autoplay and take over the whole screen
|
||
// on an iphone when viewing an article.
|
||
function inlineVideos() {
|
||
document.querySelectorAll("video").forEach(element => {
|
||
element.setAttribute("playsinline", true);
|
||
if (!element.classList.contains("nnwAnimatedGIF")) {
|
||
element.setAttribute("controls", true);
|
||
element.removeAttribute("autoplay");
|
||
}
|
||
});
|
||
}
|
||
|
||
// Remove some children (currently just spans) from pre elements to work around a strange clipping issue
|
||
var ElementUnwrapper = {
|
||
unwrapSelector: "span",
|
||
unwrapElement: function (element) {
|
||
var parent = element.parentNode;
|
||
var children = Array.from(element.childNodes);
|
||
|
||
for (child of children) {
|
||
parent.insertBefore(child, element);
|
||
}
|
||
|
||
parent.removeChild(element);
|
||
},
|
||
// `elements` can be a selector string, an element, or a list of elements
|
||
unwrapAppropriateChildren: function (elements) {
|
||
if (typeof elements[Symbol.iterator] !== 'function')
|
||
elements = [elements];
|
||
else if (typeof elements === "string")
|
||
elements = document.querySelectorAll(elements);
|
||
|
||
for (element of elements) {
|
||
for (unwrap of element.querySelectorAll(this.unwrapSelector)) {
|
||
this.unwrapElement(unwrap);
|
||
}
|
||
|
||
element.normalize()
|
||
}
|
||
}
|
||
};
|
||
|
||
function flattenPreElements() {
|
||
ElementUnwrapper.unwrapAppropriateChildren("div.articleBody td > pre");
|
||
}
|
||
|
||
function reloadArticleImage(imageSrc) {
|
||
var image = document.getElementById("nnwImageIcon");
|
||
image.src = imageSrc + "?" + new Date().getTime();
|
||
}
|
||
|
||
function stopMediaPlayback() {
|
||
document.querySelectorAll("iframe").forEach(element => {
|
||
var iframeSrc = element.src;
|
||
element.src = iframeSrc;
|
||
});
|
||
|
||
// We pause all videos that have controls. Video without controls shouldn't
|
||
// have sound and are actually converted gifs. Basically if the user can't
|
||
// start the video again, don't stop it.
|
||
document.querySelectorAll("video, audio").forEach(element => {
|
||
if (element.hasAttribute("controls")) {
|
||
element.pause();
|
||
}
|
||
});
|
||
}
|
||
|
||
function error() {
|
||
document.body.innerHTML = "error";
|
||
}
|
||
|
||
// Takes into account absoluting of URLs.
|
||
function isLocalFootnote(target) {
|
||
return (target.hash.startsWith("#fn") || target.hash.startsWith("#footnote")) && target.href.indexOf(document.baseURI) === 0;
|
||
}
|
||
|
||
function styleLocalFootnotes() {
|
||
// Multimarkdown-style: <sup><a href="#fn…">. Substack-style: a bare
|
||
// <a id="footnote-anchor-N" href="#footnote-N"> with no <sup> wrapper.
|
||
for (elem of document.querySelectorAll("sup > a[href*='#fn'], sup > div > a[href*='#fn'], a[id^='footnote-anchor-']")) {
|
||
if (isLocalFootnote(elem)) {
|
||
elem.classList.add("footnote");
|
||
}
|
||
}
|
||
}
|
||
|
||
// convert <img alt="📰" src="[...]" class="wp-smiley"> to a text node containing 📰
|
||
// Tailwind size classes like w-[22px] and h-[22px] encode an SVG icon's intended size.
|
||
// The site stylesheet that implements them is stripped, so apply the sizes directly —
|
||
// otherwise an SVG with no width and height renders at full column width.
|
||
function sizeTailwindSVGs() {
|
||
document.querySelectorAll("div.articleBody svg").forEach(svg => {
|
||
const classAttribute = svg.getAttribute("class") || "";
|
||
const width = classAttribute.match(/(?:^|\s)w-\[([\d.]+(?:px|em|rem))\]/);
|
||
if (width) {
|
||
svg.style.width = width[1];
|
||
}
|
||
const height = classAttribute.match(/(?:^|\s)h-\[([\d.]+(?:px|em|rem))\]/);
|
||
if (height) {
|
||
svg.style.height = height[1];
|
||
}
|
||
});
|
||
}
|
||
|
||
function removeWpSmiley() {
|
||
for (const img of document.querySelectorAll("img.wp-smiley[alt]")) {
|
||
img.parentNode.replaceChild(document.createTextNode(img.alt), img);
|
||
}
|
||
}
|
||
|
||
// <https://github.com/Ranchero-Software/NetNewsWire/issues/3501>
|
||
function removeInstapaperIgnoreElements() {
|
||
document.querySelectorAll(".instapaper_ignore").forEach(element => element.remove());
|
||
}
|
||
|
||
function processPage() {
|
||
// stripStyles must run first — wrapFrames sets aspect-ratio on fixed-size iframes, which stripStyles would remove.
|
||
stripStyles();
|
||
wrapFrames();
|
||
wrapTables();
|
||
inlineVideos();
|
||
sizeTailwindSVGs();
|
||
constrainBodyRelativeIframes();
|
||
convertImgSrc();
|
||
flattenPreElements();
|
||
styleLocalFootnotes();
|
||
removeWpSmiley()
|
||
removeInstapaperIgnoreElements();
|
||
postRenderProcessing();
|
||
}
|
||
|
||
document.addEventListener("DOMContentLoaded", function(event) {
|
||
processPage();
|
||
})
|