2019-09-21 23:11:09 +02:00
|
|
|
// Here we are making iframes responsive. Particularly useful for inline Youtube videos.
|
2019-09-21 11:34:11 +02:00
|
|
|
function wrapFrames() {
|
|
|
|
document.querySelectorAll("iframe").forEach(element => {
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
wrapper.classList.add("iframeWrap");
|
|
|
|
element.parentNode.insertBefore(wrapper, element);
|
|
|
|
wrapper.appendChild(element);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-31 01:30:10 +01:00
|
|
|
// Strip out color and font styling
|
|
|
|
|
|
|
|
function stripStylesFromElement(element, propertiesToStrip) {
|
|
|
|
for (name of propertiesToStrip) {
|
|
|
|
element.style.removeProperty(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:34:11 +02:00
|
|
|
function stripStyles() {
|
|
|
|
document.getElementsByTagName("body")[0].querySelectorAll("style, link[rel=stylesheet]").forEach(element => element.remove());
|
2019-12-31 01:30:10 +01:00
|
|
|
// Removing "background" and "font" will also remove properties that would be reflected in them, e.g., "background-color" and "font-family"
|
|
|
|
document.getElementsByTagName("body")[0].querySelectorAll("[style]").forEach(element => stripStylesFromElement(element, ["color", "background", "font"]));
|
2019-09-21 11:34:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:39:02 +01:00
|
|
|
// Convert all Feedbin proxy images to be used as src, otherwise change image locations to be absolute if not already
|
2019-11-10 00:37:25 +01:00
|
|
|
function convertImgSrc() {
|
|
|
|
document.querySelectorAll("img").forEach(element => {
|
2020-01-17 19:41:04 +01:00
|
|
|
if (element.hasAttribute("data-canonical-src")) {
|
|
|
|
element.src = element.getAttribute("data-canonical-src")
|
2020-01-28 23:39:02 +01:00
|
|
|
} else if (!element.src.match(/^[a-z]+\:\/\//i)) {
|
2020-01-17 01:14:38 +01:00
|
|
|
element.src = new URL(element.src, document.baseURI).href;
|
|
|
|
}
|
2019-11-10 00:37:25 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-28 07:05:16 +01:00
|
|
|
// Wrap tables in an overflow-x: auto; div
|
|
|
|
function wrapTables() {
|
2019-12-31 00:24:02 +01:00
|
|
|
var tables = document.querySelector("div.articleBody").getElementsByTagName("table");
|
2019-12-28 07:05:16 +01:00
|
|
|
|
|
|
|
for (table of tables) {
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
wrapper.className = "nnw-overflow";
|
2019-12-31 00:24:02 +01:00
|
|
|
table.parentNode.insertBefore(wrapper, table);
|
|
|
|
wrapper.appendChild(table);
|
2019-12-28 07:05:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 08:27:31 +01:00
|
|
|
// 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);
|
|
|
|
},
|
2019-12-31 00:35:47 +01:00
|
|
|
// `elements` can be a selector string, an element, or a list of elements
|
2019-12-28 08:27:31 +01:00
|
|
|
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() {
|
2019-12-31 00:19:05 +01:00
|
|
|
ElementUnwrapper.unwrapAppropriateChildren("div.articleBody td > pre");
|
2019-12-28 08:27:31 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 22:08:11 +01:00
|
|
|
function reloadArticleImage(imageSrc) {
|
2019-11-19 02:33:31 +01:00
|
|
|
var image = document.getElementById("nnwImageIcon");
|
2020-02-09 22:08:11 +01:00
|
|
|
image.src = imageSrc;
|
2019-11-19 02:33:31 +01:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:34:11 +02:00
|
|
|
function error() {
|
|
|
|
document.body.innerHTML = "error";
|
|
|
|
}
|
|
|
|
|
2019-11-21 01:16:54 +01:00
|
|
|
function render(data, scrollY) {
|
2019-09-21 11:34:11 +02:00
|
|
|
document.getElementsByTagName("style")[0].innerHTML = data.style;
|
2020-01-30 13:10:45 +01:00
|
|
|
|
|
|
|
let title = document.getElementsByTagName("title")[0];
|
|
|
|
title.textContent = data.title
|
|
|
|
|
|
|
|
let base = document.getElementsByTagName("base")[0];
|
|
|
|
base.href = data.baseURL
|
|
|
|
|
2019-09-21 11:34:11 +02:00
|
|
|
document.body.innerHTML = data.body;
|
2019-09-21 23:11:09 +02:00
|
|
|
|
2019-11-21 01:16:54 +01:00
|
|
|
window.scrollTo(0, scrollY);
|
2019-09-21 11:34:11 +02:00
|
|
|
|
|
|
|
wrapFrames()
|
2019-12-28 08:26:02 +01:00
|
|
|
wrapTables()
|
2019-09-21 11:34:11 +02:00
|
|
|
stripStyles()
|
2019-11-10 00:37:25 +01:00
|
|
|
convertImgSrc()
|
2019-12-28 08:27:31 +01:00
|
|
|
flattenPreElements()
|
|
|
|
|
2019-10-13 22:47:11 +02:00
|
|
|
postRenderProcessing()
|
2019-09-21 11:34:11 +02:00
|
|
|
}
|