"use strict";!function(){let e=null;function t(){fetch(chrome.runtime.getURL("script/interceptor/config.json")).then(function(e){return e.json()}).then(function(t){e=t,chrome.storage.local.set({interceptorConfig:t}),n(t)}).catch(function(e){console.error("[KDCM拦截器-Content] ❌ 配置加载失败:",e)})}function n(e){if(!e||!e.platforms||!Array.isArray(e.platforms))return void console.error("[KDCM拦截器-Content] ❌ 配置格式错误，缺少 platforms 数组:",e);const t=o(window.location.href,e);if(!t)return;const n={platformId:t.id,platformName:t.name,apiRules:t.apiRules,htmlCaptureRules:t.htmlCaptureRules||[],idExtractor:t.idExtractor,htmlCacheParam:t.htmlCacheParam||"cache_html",htmlCacheRuleKey:t.htmlCacheRuleKey||"page_html"};fetch(chrome.runtime.getURL("script/interceptor/injected.js")).then(function(e){return e.text()}).then(function(e){const t=`\n// 注入配置到 window 对象\nwindow.__INTERCEPTOR_CONFIG__ = ${JSON.stringify(n)};\n\n// 执行拦截器代码\n${e}\n`;chrome.runtime.sendMessage({type:"inject_script_to_main_world",code:t},function(e){chrome.runtime.lastError?console.error("[KDCM拦截器-Content] ❌ 注入失败:",chrome.runtime.lastError.message):e&&e.success})}).catch(function(e){console.error("[KDCM拦截器-Content] ❌ 读取 injected.js 失败:",e)}),window.addEventListener("KDCMInterceptorData",function(e){const t=e.detail;!1!==t.persist&&chrome.runtime.sendMessage({type:"interceptor_store_data",data:t},function(e){chrome.runtime.lastError?console.error("[KDCM拦截器-Content] ❌ 存储失败:",chrome.runtime.lastError.message):e&&e.success})}),window.addEventListener("message",function(e){if(e.source!==window)return;const t=e.data;t&&"KDCM_INTERCEPTOR_QUERY"===t.type&&chrome.runtime.sendMessage({type:t.action,data:t.data||{ruleKey:t.ruleKey,itemId:t.itemId,pageUrl:t.pageUrl,options:t.options}},function(e){window.postMessage({type:"KDCM_INTERCEPTOR_RESPONSE",requestId:t.requestId,response:e},"*")})})}chrome.storage.local.get(["interceptorConfig"],function(r){if(r.interceptorConfig){let o=r.interceptorConfig;if(o.state&&o.data)e=o.data;else{if(!o.platforms)return console.error("[KDCM拦截器-Content] ❌ 缓存配置格式错误:",o),void t();e=o}n(e)}else t()});const r=new Map;function o(e,t){if(!t||!t.platforms||!Array.isArray(t.platforms))return console.error("[KDCM拦截器-Content] ❌ matchPlatform: 配置格式错误"),null;for(const n of t.platforms)if(n.enabled)for(const t of n.urlPatterns){let o=r.get(t);if(!o){const e="^"+t.replace(/\*/g,".*").replace(/\?/g,"\\?")+"$";o=new RegExp(e),r.set(t,o)}if(o.test(e))return n}return null}chrome.runtime.onMessage.addListener(function(e,t,n){if("UPDATE_INTERCEPTOR_CONFIG"===e.action){r.clear();let t=e.config;t.state&&t.data&&(t=t.data),chrome.storage.local.set({interceptorConfig:t});const n=o(window.location.href,t);if(n){const e={platformId:n.id,platformName:n.name,apiRules:n.apiRules,htmlCaptureRules:n.htmlCaptureRules||[],idExtractor:n.idExtractor,htmlCacheParam:n.htmlCacheParam||"cache_html",htmlCacheRuleKey:n.htmlCacheRuleKey||"page_html"},t=`\nwindow.__INTERCEPTOR_CONFIG__ = ${JSON.stringify(e)};\nwindow.dispatchEvent(new MessageEvent('message', {\n  data: {\n    type: 'UPDATE_KDCM_INTERCEPTOR_CONFIG',\n    config: ${JSON.stringify(e)}\n  }\n}));\n`;chrome.runtime.sendMessage({type:"inject_script_to_main_world",code:t})}}})}();
