/* -*- mode: javascript -*- Oembed, outer script, run three. */ var oembedhost = new URL('/', 'https://oembed.rz.uni-kiel.de/oembed.js'); // document.currentScript.src); var shimfile = new URL("shim.html", document.currentScript.src); var libfile = new URL("iframeResizer.min.js", document.currentScript.src); function collectCSS(a, host){ if (!host.endsWith("uni-kiel.de")){ // we cannot trust this page to look ok // if we mess with fonts, colors, etc. return []; } const IFRAME_CLONEABLE_CSS_PROPERTIES = [ "font-family", "font-size", "font-weight", "font-shape", "font-style", "font-variant", "letter-spacing", "line-height", "text-decoration", "color", "cursor" ]; const CSS_TEMPLATES = [ ['body', a.parentNode||document.body], ['a:link', a] ]; var extraStyleList = []; // 1. get all the font rules. for (const stylesh of document.styleSheets){ try { for (const rule of stylesh.cssRules){ if (rule instanceof CSSFontFaceRule){ // I was convinced we didn't need to handle relative URI // problems here, but apparently I was wrong? extraStyleList.push(rule.cssText.replace(/url\(["']([^)]*)["']\)/g, (match, p1)=>{return `url("${new URL(p1, document.baseURI)}")`})); } } } catch(e){ console.error(e); } } // 2. copy things according to other elements. // (we probably can't copy link attributes, though) for ([selector, elem] of CSS_TEMPLATES){ var styles = window.getComputedStyle(elem); extraStyleList.push( ((rs) => `${selector} {${rs}}`)(IFRAME_CLONEABLE_CSS_PROPERTIES.map( (p) => `${p}:${styles.getPropertyValue(p)}` ).join(';')) ); } return extraStyleList; } function oembedify(a){ // step 1: useless validation if (a.href.indexOf(a.dataset.domainMustMatch||'') == -1){ console.error("bad oembed link: target "+a.href+" does not match required domain "+a.dataset.domainMustMatch); return; } var ifr = document.createElement('iframe'); Object.assign(ifr, { referrerPolicy: 'unsafe-url', src: shimfile, classList: ["oembedded"], }); Object.assign(ifr.style, { borderWidth: 0, outlineWidth: 0, margin: 0, padding: 0, width: a?.style?.width || "100%", }); // setup setup message, including extra CSS styles, if needed. var msg = { oembedsetup: 1, //vvv client window shouldn't trust us. //oembedhost: oembedprovider, //clientlib: new URL("iframeResizer.contentWindow.min.js", oembedhost).href, language: (document.documentElement.lang || document.documentElement.body.lang || navigator.language || 'de').split('-',1)[0], url: a.href, css: collectCSS(a, new URL(a.href).host), }; if (a.style.width) msg.width = a.style.width; if (a.style.height) msg.height= a.style.height; ifr.addEventListener('load', (ev) => { ifr.contentWindow.postMessage(msg, oembedhost.origin); }); a.parentNode.replaceChild(ifr, a); } // load the outer code that will actually resize. window.addEventListener('load', (ev) => { import (libfile).then(() => { document.querySelectorAll('a.oembed').forEach(oembedify); iFrameResize( {checkOrigin: [shimfile.origin, 'https://oembed.rz.uni-kiel.de']}, 'iframe.oembedded' ); }); });