// 引入获取1688上传图片iframe相册数据的脚本
// import { getAlbumData as getAlbumData1688 } from "@/web/panle_1688/embed_iframe/upload_image_iframe";

if (window.location.href.includes("item.upload.taobao.com/sell") || window.location.href.includes("sell.publish.tmall.com/tmall/publish.htm") || window.location.href.includes('fxg.jinritemai.com/ffa/g/create?')) {
    var s = document.createElement("script");
    s.text = `
    (function(){
        (function(w){
            w.originalAdd = w.addEventListener;
            var myAdd = function() {
                if(arguments[0] == 'beforeunload'){
                    return
                }
                return w.originalAdd.apply(this, arguments);
            }
            myAdd.toString = function(){
                return w.originalAdd.toString();
            }
            Object.defineProperty(w, 'addEventListener', {
                value: myAdd
            });
        })(window);
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
    s = document.createElement("script");
    s.text = `
    (function(){
        let custom_value = localStorage.getItem("custom_value");
        let custom_Json = localStorage.getItem("Json");
        if (custom_value && !custom_Json) {
            custom_value = JSON.parse(custom_value)
            let _Json = {}, _Json2 = {};
            Object.defineProperty(window, 'Json', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    val.models.formValues.sku = JSON.parse(custom_value.sku);
                    val.components.sku.props.value = JSON.parse(custom_value.sku);
                    val.models.formValues.customSaleProp = JSON.parse(custom_value.customSaleProp);
                    val.components.customSaleProp.props.value = JSON.parse(custom_value.customSaleProp);
                    _Json = val;
                }
            })
            Object.defineProperty(window, 'Json2', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json2;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    val.components.sku.props.value = JSON.parse(custom_value.sku);
                    val.components.customSaleProp.props.value = JSON.parse(custom_value.customSaleProp);
                    _Json2 = val;
                }
            })
            localStorage.removeItem('custom_value');
        }
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
    s = document.createElement("script");
    s.text = `
    (function(){
        let custom_Json = localStorage.getItem("Json");
        if (custom_Json) {
            custom_Json = JSON.parse(custom_Json)
            let _Json = {}
            Object.defineProperty(window, 'Json', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    _Json = custom_Json;
                }
            })
            Object.defineProperty(window, 'Json2', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    for(let key of Object.keys(val.components)){
                        if(custom_Json.components[key]){
                            val.components[key] = custom_Json.components[key]
                        }
                    }
                    val.models.formValues = custom_Json.models.formValues;
                    _Json = val;
                }
            })
        }
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
    s = document.createElement("script");
    s.text = `
    (function(){
        let custom_img = localStorage.getItem("custom_img");
        let custom_Json = localStorage.getItem("Json");
        if (custom_img && !custom_Json) {
            custom_img = JSON.parse(custom_img)
            let _Json = {}
            Object.defineProperty(window, 'Json', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    for(let i = 0; i < val.models.formValues.sku.length; i++){
                        let item = val.models.formValues.sku[i];
                        let item2 = val.components.sku.props.value[i];
                        let key;
                        if(item.skuUniqueName){
                            key = item.skuUniqueName.value
                        }else{
                            key = item.props.map(item => item.id + '-' + item.value).join('_')
                        }
                        let img = custom_img[key]
                        if(img){
                            item.skuPicture = [img]
                            item2.skuPicture = [img]
                        }
                    }
                    _Json = val;
                }
            })
            Object.defineProperty(window, 'Json2', {
                configurable: true,
                enumerable: true,
                get: function () {
                    return _Json;
                },
                set: function (val) {
                    debugger
                    console.log(val)
                    val.components.sku.props.value.forEach((item)=>{
                        let key;
                        if(item.skuUniqueName){
                            key = item.skuUniqueName.value
                        }else{
                            key = item.props.map(item => item.id + '-' + item.value).join('_')
                        }
                        if (custom_img[key]){
                            item.skuPicture = [custom_img[key]]
                        }
                    })
                    _Json = val;
                }
            })
            localStorage.removeItem('custom_img');
        }
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
}
if(location.href.includes('passport.taobao.com/iv') ){
    let s = document.createElement("script");
    s.src = 'https://a.alipayobjects.com/jquery/jquery/1.9.1/jquery.js'
    document.documentElement.appendChild(s);
}
if (window.location.href.includes("haohuo.jinritemai.com/ecommerce/trade/detail") || window.location.href.includes('douyin.com/')) {
    var s = document.createElement("script");
    s.text = `
    (function(){
        function XMLADD() {
            window.hook_data = [];
            window.hook_pc = [];
            console.log('注入代码>1:函数1函数1函数1函数1');
            const on_send = XMLHttpRequest.prototype.send;
            XMLHttpRequest.prototype.send = function (send_data) {  //this
            this.addEventListener('load', function () {
                let zs = 'mcj_xml_send'
                var class_name = ''
                if (this.responseURL.includes('haohuo.jinritemai.com/aweme/v2/shop/promotion/pack/h5/') || this.responseURL.includes('v2/shop/promotion/pack/detail/')  || this.responseURL.includes('www.douyin.com/aweme/v1/web/general/search/single/') || this.responseURL.includes('ecom/product/detail/saas/pc')){
                    class_name = 'hook_data'
                }
                if (this.responseURL.includes('www.douyin.com/ecom/product/detail/saas/pc/')){
                    class_name = 'hook_pc'
                }
                if (this.responseURL.includes('www.douyin.com/aweme/v2/shop/promotion/pack/detail/')){
                    class_name = 'hook_detail'
                }
                if (this.responseURL.includes('www.douyin.com/aweme/v1/web/ecom/order/confirm/')){
                    class_name = 'hook_confirm'
                }
                if (this.responseURL.includes('product/sku/list/')){
                    class_name = 'hook_skulist'
                }
                if (class_name.length > 0){
                    let response = JSON.parse(this.responseText);
                    if (class_name === 'hook_data'){
                        window.hook_data.push(response);
                    } else if (class_name === 'hook_pc'){
                        window.hook_pc.push(response);
                    }
                    let div = document.createElement('div');
                    div.innerHTML = encodeURIComponent(this.responseText);
                    div.className = class_name + ' hook'
                    div.style.display = 'none';
                    div.setAttribute('data-url', this.responseURL)
                    document.body.appendChild(div);
                }
            });
            return on_send.apply(this, arguments);
            };
        }
        XMLADD()
        setInterval(() => {
            if(!XMLHttpRequest.prototype.send.toString().includes('mcj_xml_send')){
                XMLADD()
            }    
        }, 1000)
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
}

// 滑块页数据监控
if (window.location.href.includes('action=captcha&pureCaptcha=')) {
    var s = document.createElement("script");
    s.text = `
    (function(){
        const on_send = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function (send_data) {
          this.addEventListener('load', function () {
            if (this.responseURL.includes('/_____tmd_____/slide')){
                window.parent.postMessage('close_the_pop_wait', '*')
            }
          });
          return on_send.apply(this, arguments);
        };
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
}

// 小红书保存草稿监控
if (window.location.href.includes('ark.xiaohongshu.com/app-item/good/create')) {
    var s = document.createElement("script");
    s.text = `
    (function(){
        window.hook_data = {}
        const on_send = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function (send_data) {
          this.addEventListener('load', function () {
            if (this.responseURL.includes('ark.xiaohongshu.com/api/edith/product/create_product_drafts')){
                window.hook_data.create_product_drafts = JSON.parse(this.responseText)
                let div = document.createElement('div');
                div.innerHTML = encodeURIComponent(this.responseText);
                div.classList.value = 'hook_data create_product_drafts'
                div.style.display = 'none';
                document.body.appendChild(div);
            }
            if (this.responseURL.includes('ark.xiaohongshu.com/api/edith/product/get_category_property')){
                if(JSON.parse(this.responseText).data.productCategoryProperties.length > 0){
                    window.hook_data.productCategoryProperties = JSON.parse(this.responseText)
                    let div = document.createElement('div');
                    div.innerHTML = encodeURIComponent(this.responseText);
                    div.classList.value = 'hook_data productCategoryProperties'
                    div.style.display = 'none';
                    document.body.appendChild(div);
                }
            }
          });
          return on_send.apply(this, arguments);
        };
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
}

// 获取1688上传图片iframe相册数据
if (window.location.href.includes("picman.1688.com/album/picman/picker")) {
    getAlbumData1688().catch(error => {
        parent?.window.postMessage(
            { message: "error", error: error },
            "https://offer-new.1688.com/",
        );
    });
}


if (window.location.href.includes("wares-jdm.jd.com/newPublish")|| window.location.href.includes("wares-jdm.jd.com/popPublish") || window.location.href.includes("wares-jdm.jd.com/vcProductPublish")) {
    var s = document.createElement("script");
    s.text = `
    (function(){
        (function(w){
            w.originalAdd = w.addEventListener;
            var myAdd = function() {
                if(arguments[0] == 'beforeunload'){
                    return
                }
                return w.originalAdd.apply(this, arguments);
            }
            myAdd.toString = function(){
                return w.originalAdd.toString();
            }
            Object.defineProperty(w, 'addEventListener', {
                value: myAdd
            });
        })(window);
    })()
    `
    s.async = false;
    document.documentElement.appendChild(s);
}