// load external urls in new screen without violating invalid html markup (function () { function openExternalLinksSafely() { const anchors = document.getElementsByTagName("a"); for (anchors, index = 0; index < anchors.length; index++) { const anchor = anchors[index]; const href = anchor.getAttribute("href"); if ( href && anchor.hostname !== location.hostname && !anchor.hasAttribute("@click") && !anchor.hasAttribute("@click.prevent") && !anchor.hasAttribute("@click.prevent.stop") && !anchor.hasAttribute("x-on:click") ) { anchor.setAttribute("rel", "noreferrer noopener"); anchor.addEventListener("click", (event) => { event.preventDefault(); window.open(href, "_blank"); }); } } } // add safe open:_blank to all external urls after Alpine initialised document.addEventListener("alpine:initialized", openExternalLinksSafely); /* if the Alpine component 'initColumns' is present on the page, / run openExternalLinksSafely each time 'onChangeMedia' is triggered, / as it rerenders html and eventListeners are lost */ if (!window.initColumns) { return; } const origInitColumns = window.initColumns; window.initColumns = new Proxy(origInitColumns, { apply(target, thisArg, argArray) { const instance = target.apply(thisArg, argArray); if (instance.onChangeMedia) { const origOnChange = instance.onChangeMedia; instance.onChangeMedia = function (...args) { const result = origOnChange.apply(this, args); this.$nextTick(() => { openExternalLinksSafely(); }); return result; }; } return instance; }, }); })();