profoundjs
Version:
Profound.js Framework and Server
11 lines (10 loc) • 5.19 kB
JavaScript
(function(){if("undefined"!==typeof pui&&pui.coder&&pui.coder.baseUrl){var g=pui.coder.baseUrl,k=function(a){return a&&"string"===typeof a&&a.startsWith("/")&&!a.startsWith(g)&&!a.startsWith("//")?"/tasks"===a||a.startsWith("/tasks/")?a:g+a:a},m=function(a){for(var b="/profoundui/ /ext- /xterm/ /cloud.css /diff2html/ /marked/ /pluralize/ /ace/ /vs/ /community/".split(" "),d=0;d<b.length;d++)if(0===a.indexOf(b[d]))return!0;return!1},u=HTMLImageElement.prototype.setAttribute;HTMLImageElement.prototype.setAttribute=
function(a,b){"src"===a&&b&&"string"===typeof b&&(b=k(b));return u.call(this,a,b)};var n=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");n&&n.set&&Object.defineProperty(HTMLImageElement.prototype,"src",{get:function(){return n.get.call(this)},set:function(a){return n.set.call(this,k(a))}});var p=Object.getOwnPropertyDescriptor(Element.prototype,"innerHTML");p&&p.set&&Object.defineProperty(Element.prototype,"innerHTML",{get:function(){return p.get.call(this)},set:function(a){a&&"string"===
typeof a&&(a=a.replace(/<img([^>]*?)src\s*=\s*(['"])(\/[^'"]+)\2([^>]*?)>/gi,function(b,d,f,e,c){return m(e)&&0!==e.indexOf(g)?"<img"+d+"src="+f+g+e+f+c+">":b}),a=a.replace(/style\s*=\s*(['"])([^'"]*background-image:\s*url\([^)]+\)[^'"]*?)\1/gi,function(b,d,f){b=f.replace(/background-image:\s*url\(\s*(['"]?)(\/[^'"\)\s]+)\1\s*\)/gi,function(e,c,h){return m(h)&&0!==h.indexOf(g)?"background-image: url("+c+g+h+c+")":e});return"style="+d+b+d}));return p.set.call(this,a)}});var v=XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open=function(a,b,d,f,e){b=k(b);return v.call(this,a,b,d,f,e)};if("undefined"!==typeof fetch){var w=window.fetch;window.fetch=function(a,b){"string"===typeof a&&(a=k(a));return w.call(this,a,b)}}var l=function(a){if(!a||"string"!==typeof a)return a;a=a.replace(/<img([^>]*?)src\s*=\s*(['"])(\/[^'"]+)\2([^>]*?)>/gi,function(b,d,f,e,c){return m(e)&&0!==e.indexOf(g)?"<img"+d+"src="+f+g+e+f+c+">":b});return a=a.replace(/background-image:\s*url\(\s*(['"]?)(\/[^'"\)\s]+)\1\s*\)/gi,
function(b,d,f){return m(f)&&0!==f.indexOf(g)?"background-image: url("+d+g+f+d+")":b})},x=window.Image;window.Image=function(){var a=new x,b=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");b&&Object.defineProperty(a,"src",{get:function(){return b.get.call(this)},set:function(d){b.set.call(this,k(d))},configurable:!0});return a};var y=document.createElement.bind(document);document.createElement=function(a){var b=y(a);if(a&&"img"===a.toLowerCase()){var d=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,
"src");d&&Object.defineProperty(b,"src",{get:function(){return d.get.call(this)},set:function(e){d.set.call(this,k(e))},configurable:!0});var f=b.setAttribute;b.setAttribute=function(e,c){"src"===e&&c&&"string"===typeof c&&(c=k(c));return f.call(this,e,c)}}return b};(new MutationObserver(function(a){a.forEach(function(b){b.addedNodes.forEach(function(d){if(1===d.nodeType&&"IMG"===d.tagName){var f=d.getAttribute("src");if(f){var e=k(f);e!==f&&d.setAttribute("src",e)}}})})})).observe(document.documentElement,
{childList:!0,subtree:!0});var q=function(a){var b=a.getAttribute("style");if(b&&-1!==b.indexOf("background-image:")){var d=b.replace(/background-image:\s*url\(\s*(['"]?)(\/[^'"\)\s]+)\1\s*\)/gi,function(f,e,c){return m(c)&&0!==c.indexOf(g)?"background-image: url("+e+g+c+e+")":f});d!==b&&a.setAttribute("style",d)}};(new MutationObserver(function(a){a.forEach(function(b){"attributes"===b.type&&"style"===b.attributeName?q(b.target):"childList"===b.type&&b.addedNodes.forEach(function(d){if(1===d.nodeType){q(d);
d=d.querySelectorAll?d.querySelectorAll('[style*="background-image"]'):[];for(var f=0;f<d.length;f++)q(d[f])}})})})).observe(document.documentElement,{attributes:!0,attributeFilter:["style"],childList:!0,subtree:!0});setTimeout(function(){document.querySelectorAll('img[src^="/"]').forEach(function(a){var b=a.getAttribute("src");b&&b.startsWith("/")&&!b.startsWith(g)&&!b.startsWith("//")&&m(b)&&a.setAttribute("src",g+b)});document.querySelectorAll('[style*="background-image"]').forEach(function(a){q(a)})},
500);var t=0,r=function(){if("undefined"!==typeof Ext&&Ext.DomHelper){if(Ext.DomHelper.insertHtml){var a=Ext.DomHelper.insertHtml;Ext.DomHelper.insertHtml=function(e,c,h){h=l(h);return a.call(this,e,c,h)}}if(Ext.DomHelper.overwrite){var b=Ext.DomHelper.overwrite;Ext.DomHelper.overwrite=function(e,c,h){"string"===typeof c?c=l(c):c&&"object"===typeof c&&(c.html&&(c.html=l(c.html)),c.src&&"string"===typeof c.src&&(c.src=k(c.src)));return b.call(this,e,c,h)}}if(Ext.DomHelper.append){var d=Ext.DomHelper.append;
Ext.DomHelper.append=function(e,c,h){"string"===typeof c?c=l(c):c&&c.html&&(c.html=l(c.html));return d.call(this,e,c,h)}}if(Ext.DomHelper.insertBefore){var f=Ext.DomHelper.insertBefore;Ext.DomHelper.insertBefore=function(e,c,h){"string"===typeof c?c=l(c):c&&c.html&&(c.html=l(c.html));return f.call(this,e,c,h)}}}else 50>t&&(t++,setTimeout(r,100))};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",r):setTimeout(r,100)}else console.warn('Coder URL Rewriter: pui["coder"]["baseUrl"] is not configured. Skipping URL rewriting.')})();