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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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.

(I already have a user style manager, let me install it!)

// ==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();
    }
})();