Universal Domain Redirect

did someone change their domain but a imbed is still using the old one \n not anymore automaticaly change domains to fix this \n originaly made because aniwatch stoped working

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Universal Domain Redirect
// @namespace   Violentmonkey Scripts
// @version     1.2
// @description  did someone change their domain but a imbed is still using the old one \n not anymore automaticaly change domains to fix this \n originaly made because aniwatch stoped working
// @author       dindiner
// @match        *://*/*
// @exclude      *://*youtube*/*
// @run-at       document-start
// @grant        unsafeWindow
// @license MIT
// ==/UserScript==
(function(){
    'use strict';
  	const win=(unsafeWindow||window);
    const chnglst={
        // TLD-style rule
        //"megacloud": { ".blog": ".tv" },
        // Full domain swap (root + subdomains)
        "megacloud.blog": "megacloud.tv"
    };
    const transformUrl=(url)=>{
        try{
            const u=new URL(url);
            let host=u.hostname;
            for(const key in chnglst){
                const val=chnglst[key];
                if(typeof val==="string"){
                    if(host===key||host.endsWith("."+key)){
                        const prefix=host===key?"":host.replace("."+key,"");
                        host=prefix?`${prefix}.${val}`:val;
                    }
                }else if(typeof val==="object"){
                    for(const from in val){
                        const to=val[from];
                        if(host===key+from||host.endsWith("."+key+from)){
                            const prefix=host===key+from?"":host.replace("."+key+from,"");
                            host=prefix?`${prefix}.${key}${to}`:`${key}${to}`;
                        }
                    }
                }
            }
            u.hostname=host;
            return u.toString();
        }catch(e){
            return url;
        }
    };
    const redirected=transformUrl(win.location.href);
    if(redirected!==win.location.href){
        win.location.replace(redirected);
        return;
    }
    const fixIframes=()=>{
        document.querySelectorAll('iframe').forEach(iframe=>{
            if(iframe.src){
                const updated=transformUrl(iframe.src);
                if(updated!==iframe.src)iframe.src=updated;
            }
        });
    };
    const originalFetch=win.fetch;
    win.fetch=function(input,init){
        try{
            let url=(typeof input==="string")?input:input.url;
            const newUrl=transformUrl(url);
            if(typeof input==="string"){
                input=newUrl;
            }else{
                input=new Request(newUrl,input);
            }
        } catch(e){}
        return originalFetch.call(this,input,init);
    };
    const originalOpen=XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open=function(method,url,...rest){
        try{
            url=transformUrl(url);
        }catch(e){}
        return originalOpen.call(this,method,url,...rest);
    };
    const observer=new MutationObserver(fixIframes);
    const init=()=>{
        fixIframes();
        observer.observe(document.documentElement,{childList:true,subtree:true});
    };
    if (document.readyState==='loading'){
        document.addEventListener('DOMContentLoaded',init);
    }else{
        init();
    }
})();