function intersect(a, b) { if (b.length > a.length) { [a, b] = [b, a]; } // loop over shorter array return a.filter(item => (b.indexOf(item) !== -1)); } function insertAfter(newNode, referenceNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } function moveAll(fromNode, toNode) { for (let c; c = fromNode.firstChild; ) { toNode.appendChild(c); } } function removeAllFrom(fromNode) { for (let c; c = fromNode.lastChild; ) { fromNode.removeChild(c); } } function makeIframe(src, w, h, scrolling='no') { let iframe = document.createElement('iframe'); iframe.style.width = w; iframe.style.height = h; iframe.frameBorder = 0; iframe.scrolling = scrolling; iframe.setAttribute('allowFullScreen', ''); iframe.src = src; return iframe; } function wrapIntoTag(node, tagName, className) { let tag = document.createElement(tagName); if (className !== undefined) { tag.className = className; } tag.appendChild(node); return tag; } function makeResizableToRatio(element, ratio) { element.dataset['ratio'] = ratio; makeResizable(element, w => w * element.dataset['ratio']); } // calcHeight :: Number -> Number -- calculate element height for a given width function makeResizable(element, calcHeight) { const setHeight = el => { if (el.offsetWidth > 0) { el.style.height = (calcHeight(el.offsetWidth)).toFixed(2) + 'px'; } }; window.addEventListener('resize', () => setHeight(element)); setHeight(element); } function getEmbeddableLinkTypes() { return [ { name: 'Jpeg and png images', id: 'embed_jpeg_and_png_images', ctsDefault: false, re: /\.(jpeg|jpg|png|svg)(:[a-zA-Z]+)?(?:\?[\w&;\?=]*)?$/i, makeNode: function(aNode) { let aNode2 = document.createElement('a'); let imgNode = document.createElement('img'); imgNode.src = aNode.href; aNode2.href = aNode.href; aNode2.appendChild(imgNode); return wrapIntoTag(aNode2, 'div', 'picture compact'); } }, { name: 'Gif images', id: 'embed_gif_images', ctsDefault: true, re: /\.gif(:[a-zA-Z]+)?(?:\?[\w&;\?=]*)?$/i, makeNode: function(aNode) { let aNode2 = document.createElement('a'); let imgNode = document.createElement('img'); imgNode.src = aNode.href; aNode2.href = aNode.href; aNode2.appendChild(imgNode); return wrapIntoTag(aNode2, 'div', 'picture compact'); } }, { name: 'Video (webm, mp4, ogv)', id: 'embed_webm_and_mp4_videos', ctsDefault: false, re: /\.(webm|mp4|m4v|ogv)(?:\?[\w&;\?=]*)?$/i, makeNode: function(aNode) { let video = document.createElement('video'); video.src = aNode.href; video.title = aNode.href; video.setAttribute('controls', ''); return wrapIntoTag(video, 'div', 'video compact'); } }, { name: 'Audio (mp3, ogg, weba, opus, m4a, oga, wav)', id: 'embed_sound_files', ctsDefault: false, re: /\.(mp3|ogg|weba|opus|m4a|oga|wav)(?:\?[\w&;\?=]*)?$/i, makeNode: function(aNode) { let audio = document.createElement('audio'); audio.src = aNode.href; audio.title = aNode.href; audio.setAttribute('controls', ''); return wrapIntoTag(audio, 'div', 'audio'); } }, { name: 'YouTube videos (and playlists)', id: 'embed_youtube_videos', onByDefault: false, ctsDefault: false, re: /^(?:https?:)?\/\/(?:www\.|m\.)?(?:youtu(?:(?:\.be\/|be\.com\/(?:v|embed)\/)([-\w]+)|be\.com\/watch)((?:(?:\?|&(?:amp;)?)(?:\w+=[-\.\w]*[-\w]))*)|youtube\.com\/playlist\?list=([-\w]*)(&(amp;)?[-\w\?=]*)?)/i, makeNode: function(aNode, reResult) { let [url, v, args, plist] = reResult; let iframeUrl; if (plist !== undefined) { iframeUrl = '//www.youtube-nocookie.com/embed/videoseries?list=' + plist; } else { args = args.replace(/^\?/, ''); let arr = args.split('&').map(s => s.split('=')); let pp = {}; arr.forEach(z => pp[z[0]] = z[1]); let embedArgs = { rel: '0' }; if (pp.t != undefined) { const tre = /^(?:(\d+)|(?:(\d+)h)?(?:(\d+)m)?(\d+)s|(?:(\d+)h)?(\d+)m|(\d+)h)$/i; let [, t, h, m, s, h1, m1, h2] = tre.exec(pp.t); embedArgs['start'] = (+t) || ((+(h || h1 || h2 || 0))*60*60 + (+(m || m1 || 0))*60 + (+(s || 0))); } if (pp.list !== undefined) { embedArgs['list'] = pp.list; } v = v || pp.v; iframeUrl = '//www.youtube-nocookie.com/embed/' + v + '?' + Object.keys(embedArgs).map(k => `${k}=${embedArgs[k]}`).join('&'); } let iframe = makeIframe(iframeUrl, '100%', '360px'); setTimeout(() => makeResizableToRatio(iframe, 9.0/16.0), 10); return wrapIntoTag(iframe, 'div', 'youtube resizableV'); } }, { name: 'Vimeo videos', id: 'embed_vimeo_videos', onByDefault: false, ctsDefault: false, //re: /^(?:https?:)?\/\/(?:www\.)?(?:player\.)?vimeo\.com\/(?:(?:video\/|album\/[\d]+\/video\/)?([\d]+)|([\w-]+)\/(?!videos)([\w-]+))/i, re: /^(?:https?:)?\/\/(?:www\.)?(?:player\.)?vimeo\.com\/(?:video\/|album\/[\d]+\/video\/)?([\d]+)/i, makeNode: function(aNode, reResult) { let iframe = makeIframe('//player.vimeo.com/video/' + reResult[1], '100%', '360px'); setTimeout(() => makeResizableToRatio(iframe, 9.0/16.0), 10); return wrapIntoTag(iframe, 'div', 'vimeo resizableV'); } } ]; } function embedLink(aNode, linkTypes, container, alwaysCts, afterNode) { let anyEmbed = false; let linkId = (aNode.href.replace(/^https?:/i, '').replace(/\'/i,'')); let sameEmbed = container.querySelector(`*[data-linkid='${linkId}']`); // do not embed the same thing twice if (sameEmbed === null) { anyEmbed = [].some.call(linkTypes, function(linkType) { let reResult = linkType.re.exec(aNode.href); if (reResult !== null) { if ((linkType.match !== undefined) && (linkType.match(aNode, reResult) === false)) { return false; } let newNode = linkType.makeNode(aNode, reResult); if (!newNode) { return false; } newNode.setAttribute('data-linkid', linkId); if (afterNode !== undefined) { insertAfter(newNode, afterNode); } else { container.appendChild(newNode); } aNode.classList.add('embedLink'); return true; } }); } return anyEmbed; } function embedLinks(aNodes, container, alwaysCts, afterNode) { let anyEmbed = false; let embeddableLinkTypes = getEmbeddableLinkTypes(); Array.from(aNodes).forEach(aNode => { let isEmbedded = embedLink(aNode, embeddableLinkTypes, container, alwaysCts, afterNode); anyEmbed = anyEmbed || isEmbedded; }); return anyEmbed; } function articleInfo(article) { let tagNodes = article.querySelectorAll('.msg-tags > *'); let tags = Array.from(tagNodes).map(d => d.textContent.toLowerCase()); return { tags: tags }; } function embedLinksToX(x, beforeNodeSelector, allLinksSelector, ctsUsers, ctsTags) { let isCtsPost = false; let allLinks = x.querySelectorAll(allLinksSelector); let existingContainer = x.querySelector('div.embedContainer'); if (existingContainer !== null) { embedLinks(allLinks, existingContainer, isCtsPost); } else { let embedContainer = document.createElement('div'); embedContainer.className = 'embedContainer'; let anyEmbed = embedLinks(allLinks, embedContainer, isCtsPost); if (anyEmbed) { let beforeNode = x.querySelector(beforeNodeSelector); x.insertBefore(embedContainer, beforeNode); } } } function embedLinksToArticles() { let ctsUsers = [], ctsTags = []; // TODO click-to-show users and tags let beforeNodeSelector = 'nav.l'; let allLinksSelector = 'p:not(.ir) a, pre a'; Array.from(document.querySelectorAll('#content > article')).forEach(article => { embedLinksToX(article, beforeNodeSelector, allLinksSelector, ctsUsers, ctsTags); }); } function embedLinksToPost() { let ctsUsers = [], ctsTags = []; // TODO click-to-show users and tags let beforeNodeSelector = '.msg-txt + *'; let allLinksSelector = '.msg-txt a'; Array.from(document.querySelectorAll('#content .msg-cont')).forEach(msg => { embedLinksToX(msg, beforeNodeSelector, allLinksSelector, ctsUsers, ctsTags); }); } exports.embed = function() { if (document.querySelectorAll('#content > article').length) { embedLinksToArticles(); } else { embedLinksToPost(); } };