class S{static detectPlatform(){const a=window.location.hostname;return a.includes("leetcode.com")?"leetcode":a.includes("geeksforgeeks.org")?"geeksforgeeks":a.includes("codeforces.com")?"codeforces":a.includes("hackerrank.com")?"hackerrank":"unknown"}static extract(){switch(this.detectPlatform()){case"leetcode":return this.extractLeetCode();case"geeksforgeeks":return this.extractGeeksforGeeks();case"codeforces":return this.extractCodeforces();case"hackerrank":return this.extractHackerRank();default:return null}}static extractLeetCode(){try{const a=document.querySelector('[data-cy="question-title"]')||document.querySelector('div[class*="title"]')||document.querySelector("h3"),o=a?a.textContent.trim():"Unknown Problem",d=o.match(/^(\d+)\./)?.[1]||"",m=document.querySelector('[data-cy="description"]')||document.querySelector(".question-content__JfgR")||document.querySelector('[class*="description"]'),c=m?m.innerText:"";let l="unknown";const n=document.querySelector('[data-cy="lang-select"]')||document.querySelector('button[class*="language"]')||document.querySelector('div[class*="lang-select"]')||document.querySelector('select[class*="language"]');n&&(l=n.textContent.trim()||n.value||n.getAttribute("data-lang")||"unknown");const s=document.querySelector(".monaco-editor");if(s&&l==="unknown"){const t=s.getAttribute("data-mode-id")||s.getAttribute("data-lang");t&&(l=t.split("/").pop()||t)}if(l==="unknown"){const t=document.querySelector('[class*="editor"]');if(t){const i=t.getAttribute("data-language")||t.getAttribute("data-lang");i&&(l=i)}}if(l==="unknown"){const t=["C++","Java","Python","Python3","C","C#","JavaScript","TypeScript","PHP","Swift","Kotlin","Dart","Go","Ruby","Scala","Rust","Racket","Erlang","Elixir","Pandas","React"],i=document.querySelectorAll('div[class*="flex"], div[class*="toolbar"]');for(const u of i){const p=u.querySelectorAll('button, div[role="button"], span');for(const y of p){const g=y.textContent.trim();if(t.includes(g)){l=g;break}}if(l!=="unknown")break}if(l==="unknown"){const u=document.querySelectorAll("button");for(const p of u)if(t.includes(p.textContent.trim())){l=p.textContent.trim();break}}}l=this.normalizeLanguage(l);let e="";const r=document.querySelector(".monaco-editor");if(r){if(!e&&window.monaco)try{const t=window.monaco.editor.getEditors();if(t&&t.length>0){const i=t[0].getModel();i&&(e=i.getValue())}}catch{}if(!e){const t=r.querySelectorAll(".view-line");t.length>0&&(e=Array.from(t).map(i=>(i.textContent||"").replace(/\s+$/,"")).join(`
`))}if(!e){const t=r.querySelector("textarea");t&&t.value&&(e=t.value)}}if(!e){const t=document.querySelector('textarea[class*="input"]')||document.querySelector('textarea[class*="code"]');t&&(e=t.value||t.textContent||"")}if(!e){const t=document.querySelector(".CodeMirror-code");t&&(e=t.innerText)}if(!e){const t=document.querySelector("pre code")||document.querySelector('pre[class*="code"]');t&&(e=t.textContent||t.innerText||"")}return{platform:"leetcode",title:o,problemNumber:d,description:c,code:e.trim(),language:l,url:window.location.href}}catch(a){return console.error("Error extracting LeetCode data:",a),null}}static extractGeeksforGeeks(){try{const a=document.querySelector(".gfg-article-title")||document.querySelector('[class*="problems_header_content_title"]')||document.querySelector('[class*="problem-title"]')||document.querySelector("h3"),o=a?a.textContent.trim():"Unknown Problem",d=document.querySelector(".problem-statement")||document.querySelector('[class*="problems_description__"]')||document.querySelector(".content")||document.querySelector("article"),m=d?d.innerText:"";let c="unknown";const l=document.querySelector('[class*="problems_language_dropdown__"]');if(l&&(c=(l.innerText||"").split(`
`)[0].trim()),c==="unknown"){const e=document.querySelector(".active-language")||document.querySelector(".language-active")||document.querySelector(".gfg-dropdown-active");e&&(c=e.textContent||e.innerText)}if(c==="unknown"){const e=document.querySelectorAll(".editor-toolbar, .divider, .pull-right");for(const r of e){const t=r.textContent,i=["C++","Java","Python","Python 3","C","C#","JavaScript"];for(const u of i)if(t.includes(u)&&(t.includes("Language")||t.includes("Lang"))){c=u;break}if(c!=="unknown")break}}if(c==="unknown"){const r=(document.querySelector(".problem-editor")||document.querySelector('[class*="problems_right_section__"]')||document.body).querySelectorAll(".dropdown-text, .ui.selection.dropdown, select, [role='listbox'], button"),t=["C++","Java","Python","Python3","C","C#","JavaScript","TypeScript","PHP","Swift","Kotlin","Dart","Go","Ruby","Scala","Rust"];for(const i of r){const u=i.textContent||i.value;if(u&&t.includes(u.trim())){c=u.trim();break}}}c=this.normalizeLanguage(c);let n="";const s=document.querySelector(".monaco-editor");if(s){if(!n&&window.monaco)try{const e=window.monaco.editor.getEditors();if(e&&e.length>0){const r=e[0].getModel();r&&(n=r.getValue())}}catch{}if(!n){const e=s.querySelectorAll(".view-line");e.length>0&&(n=Array.from(e).map(r=>(r.textContent||"").replace(/\s+$/,"")).join(`
`))}if(!n){const e=s.querySelector("textarea");e&&e.value&&(n=e.value)}}if(!n){const e=document.querySelectorAll(".ace_line");e.length>0&&(n=Array.from(e).map(r=>r.textContent.replace(/\s+$/,"")).join(`
`))}if(!n){const e=document.querySelectorAll(".CodeMirror-code");e.length>0&&(n=e[e.length-1].innerText)}if(!n){const e=document.querySelectorAll("textarea");for(const r of e)if(r.value.length>20&&!r.className.includes("comment")){n=r.value;break}}if(!n){const e=document.querySelector("pre code")||document.querySelector("pre");e&&(n=e.textContent||e.innerText||"")}return{platform:"geeksforgeeks",title:o,problemNumber:"",description:m,code:n.trim(),language:c,url:window.location.href}}catch(a){return console.error("Error extracting GeeksforGeeks data:",a),null}}static extractCodeforces(){try{const a=document.querySelector(".title")||document.querySelector("h2")||document.querySelector('[class*="problem-title"]'),o=a?a.textContent.trim():"Unknown Problem",d=document.querySelector(".problem-statement")||document.querySelector(".ttypography"),m=d?d.innerText:"";let c="unknown";const l=document.querySelector('select[name="programTypeId"]')||document.querySelector('[name="language"]')||document.querySelector('[class*="language"]')||document.querySelector("option[selected]");l&&(l.tagName==="SELECT"?c=l.options[l.selectedIndex]?.text||l.value||"unknown":c=l.textContent.trim()||l.value||l.getAttribute("data-lang")||"unknown"),c=this.normalizeLanguage(c);let n="";const s=document.querySelector("pre.program-source");if(s&&(n=s.textContent||s.innerText||""),!n){const e=document.querySelector("textarea#sourceCodeTextarea")||document.querySelector('textarea[name="source"]')||document.querySelector("textarea");e&&(n=e.value||e.textContent||"")}if(!n){const e=document.querySelector("pre code")||document.querySelector("pre")||document.querySelector("code");e&&(n=e.textContent||e.innerText||"")}return{platform:"codeforces",title:o,problemNumber:"",description:m,code:n.trim(),language:c,url:window.location.href}}catch(a){return console.error("Error extracting Codeforces data:",a),null}}static extractHackerRank(){try{const a=document.querySelector(".challenge-title")||document.querySelector("h1")||document.querySelector('[class*="title"]')||document.querySelector(".ui-icon-label"),o=a?a.textContent.trim():"Unknown Problem";let d="";const m=[".challenge-body",".challenge-text",".challenge-description",'[class*="description"]',".problem-statement",".challenge-problem-statement",".problem-description"];for(const e of m){const r=document.querySelector(e);if(r&&(d=r.innerText||r.textContent||"",d.trim().length>50))break}let c="unknown";const l=document.querySelector('select[data-attr1="language"]')||document.querySelector('select[class*="lang"]')||document.querySelector('select[name="language"]')||document.querySelector(".select-wrapper select");if(l&&l.tagName==="SELECT"){const e=l.options[l.selectedIndex];e&&(c=e.textContent.trim()||e.value||"unknown"),c==="unknown"&&l.value&&(c=l.value)}if(c==="unknown"){const e=document.querySelector('[class*="language"]')||document.querySelector('[class*="lang-select"]')||document.querySelector('button[class*="lang"]')||document.querySelector(".ui-selectmenu-text");e&&(c=e.textContent.trim()||e.getAttribute("data-lang")||e.value||"unknown")}if(c==="unknown"){const e=new URLSearchParams(window.location.search),r=e.get("language")||e.get("lang");r&&(c=r)}if(c==="unknown"){const r=(document.querySelector(".editor-wrapper")||document.body).querySelectorAll(".dropdown-text, .ui.selection.dropdown, select, [role='listbox'], button, span"),t=["C++","Java","Python","Python3","C","C#","JavaScript","TypeScript","PHP","Swift","Kotlin","Dart","Go","Ruby","Scala","Rust"];for(const i of r){const u=i.textContent||i.value;if(u&&t.includes(u.trim())){c=u.trim();break}}}c=this.normalizeLanguage(c);let n="";const s=document.querySelector(".monaco-editor");if(s){if(!n&&window.monaco)try{const e=window.monaco.editor.getEditors();for(const r of e){const t=r.getValue();if(t&&t.length>10){n=t;break}}!n&&e.length>0&&(n=e[0].getValue())}catch{}if(!n){const e=s.querySelectorAll(".view-line");e.length>0&&(n=Array.from(e).map(r=>(r.textContent||"").replace(/\s+$/,"")).join(`
`))}if(!n){const e=s.querySelector("textarea");e&&e.value&&(n=e.value)}}if(!n){const e=document.querySelector(".CodeMirror-code");if(e){const r=e.querySelectorAll(".CodeMirror-line");r.length>0?n=Array.from(r).map(t=>t.textContent||t.innerText||"").join(`
`):n=e.innerText||e.textContent||""}}if(!n){const e=document.querySelectorAll(".ace_line");e.length>0&&(n=Array.from(e).map(r=>r.textContent.replace(/\s+$/,"")).join(`
`))}if(!n){const e=document.querySelector(".CodeMirror textarea");e&&e.value&&(n=e.value)}if(!n){const e=document.querySelector('textarea[class*="code"]')||document.querySelector('textarea[name="code"]')||document.querySelector("textarea");e&&(n=e.value||e.textContent||"")}if(!n){const e=document.querySelector("pre code")||document.querySelector('pre[class*="code"]')||document.querySelector("pre");e&&(n=e.textContent||e.innerText||"")}return{platform:"hackerrank",title:o,problemNumber:"",description:d,code:n.trim(),language:c,url:window.location.href}}catch(a){return console.error("Error extracting HackerRank data:",a),null}}static normalizeLanguage(a){if(!a||a==="unknown")return"unknown";const o=a.toLowerCase().trim();if(o.includes("python"))return"python";if(o.includes("javascript")||o==="js")return"javascript";if(o.includes("typescript")||o==="ts")return"typescript";if(o.includes("java")&&!o.includes("javascript"))return"java";if(o.includes("c++")||o.includes("cpp")||o.includes("cplusplus"))return"cpp";if(o==="c"||o==="c "||o.includes(" c ")&&!o.includes("c++"))return"c";if(o.includes("c#")||o.includes("csharp"))return"csharp";if(o.includes("go")||o.includes("golang"))return"go";if(o.includes("rust"))return"rust";if(o.includes("ruby"))return"ruby";if(o.includes("swift"))return"swift";if(o.includes("kotlin"))return"kotlin";if(o.includes("scala"))return"scala";if(o.includes("php"))return"php";if(o.includes("sql"))return"sql";if(o.includes("bash")||o.includes("shell"))return"bash";const d=o.replace(/\s+/g,"").replace(/[^a-z0-9]/g,"");return{python:"python",python3:"python",py:"python",javascript:"javascript",js:"javascript",typescript:"typescript",ts:"typescript",java:"java",cpp:"cpp","c++":"cpp",cplusplus:"cpp",c:"c",csharp:"csharp","c#":"csharp",cs:"csharp",go:"go",golang:"go",rust:"rust",rs:"rust",ruby:"ruby",rb:"ruby",swift:"swift",kotlin:"kotlin",kt:"kotlin",scala:"scala",php:"php",sql:"sql",mysql:"sql",bash:"bash",shell:"bash",sh:"bash"}[d]||d||"unknown"}}window.addEventListener("message",f=>{if(f.source===window&&f.data.type&&f.data.type==="CODESYNC_EXTRACT_REQUEST"){const a=S.extract();window.postMessage({type:"CODESYNC_EXTRACT_RESPONSE",data:a},"*")}});
