Changeset 3482586
- Timestamp:
- 03/14/2026 03:00:49 PM (2 weeks ago)
- File:
-
- 1 edited
-
gptranslate/trunk/assets/js/admin.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
gptranslate/trunk/assets/js/admin.js
r3466525 r3482586 1 function e(e,t){const n=document.getElementById(e+"-container"),a=e.replace("-","_");n.innerHTML="",Object.entries(t).forEach((([e,t])=>{const o=document.createElement("div");o.classList.add("translation-row"),o.innerHTML=`<label class="badge bg-primary">${PLG_GPTRANSLATE_ORIGINAL_TEXT}</label><textarea type="text" class="original">${e}</textarea> ➜ \n <label class="badge bg-primary">${PLG_GPTRANSLATE_TRANSLATED_TEXT}</label><textarea type="text" class="translated">${t}</textarea>\n <button type="button" class="btn btn-danger" onclick="this.parentElement.remove()">${PLG_GPTRANSLATE_DELETE}</button>\n <button type="button" class="group-sync btn btn-warning btn-invisible hasPopover" data-type="${a}" title="${PLG_GPTRANSLATE_SYNC_TITLE} - ${PLG_GPTRANSLATE_SYNC_DESC}" aria-label="${PLG_GPTRANSLATE_SYNC}">\n\t\t\t\t\t\t <span class="icon-refresh">↻</span> ${PLG_GPTRANSLATE_SYNC}</button>`,n.appendChild(o)}))}function t(e){const t=document.getElementById(e+"-container"),n={};return t.querySelectorAll("div").forEach((e=>{const t=e.querySelector(".original").value,a=e.querySelector(".translated").value;t&&a&&(n[t]=a)})),JSON.stringify(n)}document.addEventListener("DOMContentLoaded",(()=>{const n=localStorage.getItem("gptranslate_selected_tab");n&&document.getElementById("tab-"+n)&&(document.querySelectorAll(".nav-tab").forEach((e=>e.classList.remove("nav-tab-active"))),document.querySelectorAll(".tab-content").forEach((e=>e.style.display="none")),document.querySelector('a[href="#'+n+'"]').classList.add("nav-tab-active"),document.getElementById("tab-"+n).style.display="block"),"undefined"!=typeof initialTranslations&&(e("translations",initialTranslations),e("alt-translations",initialAltTranslations));const a=document.querySelector("form#edit-translations");a&&a.addEventListener("submit",(e=>{document. getElementById("translations_json").value=t("translations"),document.getElementById("alt_translations_json").value=t("alt-translations")})),document.querySelectorAll('input[type="submit"][data-action]').forEach((e=>{e.addEventListener("click",(function(){const e=this.getAttribute("data-action");document.getElementById("form_action").value=e}))})),document.querySelectorAll("button.btn-adder").forEach((e=>{e.addEventListener("click",(function(){!function(e){const t=document.getElementById(e+"-container"),n=document.createElement("div");n.classList.add("translation-row"),n.innerHTML=`<label class="badge bg-primary">${PLG_GPTRANSLATE_ORIGINAL_TEXT}</label><textarea type="text" class="original"></textarea> ➜ \n <label class="badge bg-primary">${PLG_GPTRANSLATE_TRANSLATED_TEXT}</label><textarea type="text" class="translated"></textarea>\n <button type="button" class="btn btn-danger" onclick="this.parentElement.remove()">${PLG_GPTRANSLATE_DELETE}</button>`,t.insertBefore(n,t.firstChild)}(this.getAttribute("data-addtype"))}))})),document.querySelectorAll("textarea.translated").forEach((function(e,t){e.addEventListener("keyup",(function(e){const t=e.target.closest("div.translation-row").querySelector("button.group-sync");t&&t.classList.remove("btn-invisible")}))}));const o=document.getElementById("checkall");o&&o.addEventListener("change",(function(){document.querySelectorAll('input[name="gptid[]"]').forEach((e=>e.checked=o.checked))})),document.addEventListener("click",(function(e){const t=e.target.closest('button[data-role="search-translations"]');if(t){const e=t.closest("div.gptcard"),n=e.querySelector('input[name="search"]').value,a=e.querySelectorAll("div.translation-row"),o=new RegExp(n,"i");a.forEach((function(e){const t=e.querySelector('textarea[class="original"]').value,n=e.querySelector('textarea[class="translated"]').value;o.test(t)||o.test(n)?e.classList.remove("translation-row-hidden"):e.classList.add("translation-row-hidden")}))}const n=e.target.closest('button[data-role="reset-search"]');if(n){const e=n.closest("div.gptcard");e.querySelector('input[name="search"]').value="";e.querySelectorAll("div.translation-row").forEach((function(e){e.classList.remove("translation-row-hidden")}))}})),document.querySelectorAll("button.group-sync").forEach((function(e,t){e.addEventListener("click",(function(e){const t=e.currentTarget,n=t.querySelector("span.icon-refresh"),a=t.closest("div.translation-row"),o=a.querySelector("textarea.original"),r=a.querySelector("textarea.translated"),l=t.dataset.type,s=o.value,c=r.value,i=document.getElementById("languagetranslated").value;n.classList.add("icon-working");const d={task:"syncTranslation",original:s,translated:c,language_translated:i,translation_type:l},u=`${gptServerSideLink}`,p={method:"POST",body:JSON.stringify(d),headers:{"Content-Type":"application/json; charset=utf-8",Accept:"application/json","x-gptranslate-key":gptranslate_vars.gptApiKey}};fetch(u,p).then((e=>e.json())).then((e=>{if(e.result){const e=document.createElement("label");e.className="gpt-label badge bg-success",e.innerText=PLG_GPTRANSLATE_SYNC_COMPLETED,t.insertAdjacentElement("afterend",e)}else{const e=document.createElement("label");e.className="gpt-label badge bg-danger",e.innerText=PLG_GPTRANSLATE_SYNC_ERROR,t.insertAdjacentElement("afterend",e)}})).catch((e=>{const n=document.createElement("label");n.className="gpt-label badge bg-danger",n.innerText=PLG_GPTRANSLATE_SYNC_ERROR,t.insertAdjacentElement("afterend",n)})).finally((()=>{n.classList.remove("icon-working"),setTimeout((()=>{t.classList.add("btn-invisible"),document.querySelectorAll("label.gpt-label").forEach((e=>e.remove()))}),2e3)}))}))}));const r=document.createElement("div");r.className="custom-tooltip",document.body.appendChild(r),document.querySelectorAll("[title]").forEach((function(e){const t=e.getAttribute("title");e.addEventListener("mouseenter",(function(n){e.setAttribute("data-original-title",t),e.removeAttribute("title"),r.textContent=t,r.style.opacity="1"})),e.addEventListener("mousemove",(function(e){r.style.top=e.pageY+10+"px",r.style.left=e.pageX+10+"px"})),e.addEventListener("mouseleave",(function(){r.style.opacity="0",e.setAttribute("title",e.getAttribute("data-original-title"))}))})),jQuery(".select2-dropdown").length&&jQuery(".select2-dropdown").select2({tags:!1,tokenSeparators:[","]});const l=document.getElementById("config-gptranslate"),s=document.querySelector("div.action-buttons-toolbar");if(l){document.querySelectorAll("h2.nav-tab-wrapper a.nav-tab").forEach((function(e,t){e.addEventListener("click",(function(t){const n=e.dataset.click;var a;a=n,document.querySelectorAll(".tab-content").forEach((e=>e.style.display="none")),document.querySelectorAll(".nav-tab").forEach((e=>e.classList.remove("nav-tab-active"))),document.getElementById("tab-"+a).style.display="block",event.currentTarget.classList.add("nav-tab-active"),localStorage.setItem("gptranslate_selected_tab",a)}))}));const p=document.querySelector('select[name="gptranslate_options[google_translate_engine]"]'),g=document.querySelectorAll(".chatgpt_ctrl"),m=document.getElementById("chatgpt_apikey");function y(e){g.forEach((function(t){const n=t.closest("tr")||t.closest(".control-group")||t.closest(".form-group");n&&("0"==e?(n.style.display="",m&&m.setAttribute("data-validation","required")):(n.style.display="none",m&&m.removeAttribute("data-validation")))}))}p&&(y(p.value),p.addEventListener("change",(function(){y(this.value)})));const f=document.getElementById("chatgpt_model"),v=document.querySelector('label[for="chatgpt_apikey"]');if(f&&v){const W=()=>{const e=v.getAttribute("data-original-text")||v.textContent;v.getAttribute("data-original-text")||v.setAttribute("data-original-text",e),v.textContent=e.replace("DeepSeek","{aiengine}").replace("Gemini","{aiengine}").replace("Claude","{aiengine}").replace("Google Cloud Translation","{aiengine}").replace("ChatGPT","{aiengine}");const t=f.value;"deepseek-chat"===t?v.textContent=v.textContent.replace("{aiengine}","DeepSeek"):0===t.indexOf("gemini-")?v.textContent=v.textContent.replace("{aiengine}","Gemini"):0===t.indexOf("claude-")?v.textContent=v.textContent.replace("{aiengine}","Claude"):0===t.indexOf("google-")?v.textContent=v.textContent.replace("{aiengine}","Google Cloud Translation"):v.textContent="deepl-api"===t?v.textContent.replace("{aiengine}","DeepL"):v.textContent.replace("{aiengine}","ChatGPT")};W(),f.addEventListener("change",W)}if(m){var c=document.createElement("button");c.type="button",c.id="btn_test_apikey",c.className="button button-secondary chatgpt_ctrl",c.innerHTML='<span class="dashicons dashicons-yes-alt" style="vertical-align:text-top;margin-right:3px;"></span>'+("undefined"!=typeof gptranslate_vars?gptranslate_vars.i18n_test_apikey:"Test API Key");var i=document.createElement("span");i.id="apikey_test_result",i.style.display="none",i.style.marginLeft="8px",i.style.marginTop="4px",m.parentNode.insertBefore(c,m.nextSibling),c.parentNode.insertBefore(i,c.nextSibling);var d=y;y=function(e){d(e),"0"==e?c.style.display="":(c.style.display="none",i.style.display="none")},p&&y(p.value),c.addEventListener("click",(function(){var e=m.value.trim();if(!e)return i.style.display="inline-block",i.className="gpt-test-error",void(i.textContent=gptranslate_vars.i18n_test_apikey_empty);var t=document.getElementById("chatgpt_model"),n=t?t.value:"";c.disabled=!0,c.innerHTML='<span class="dashicons dashicons-update icon-spin" style="vertical-align:text-top;margin-right:3px;"></span>'+gptranslate_vars.i18n_test_apikey_testing,i.style.display="none";var a=new FormData;a.append("action","gptranslate_test_apikey"),a.append("nonce",gptranslate_vars.testApikeyNonce),a.append("apikey",e),a.append("model",n),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:a}).then((function(e){return e.json()})).then((function(e){if(i.style.display="inline-block",e.success)i.className="gpt-test-success",i.textContent=gptranslate_vars.i18n_test_apikey_success;else{i.className="gpt-test-error";var t=gptranslate_vars.i18n_test_apikey_error;e.data&&e.data.error_code&&(t+=" ("+e.data.error_code+")"),e.data&&e.data.message&&(t+=": "+e.data.message),i.textContent=t}})).catch((function(e){i.style.display="inline-block",i.className="gpt-test-error",i.textContent=gptranslate_vars.i18n_test_apikey_error+": "+e.message})).finally((function(){c.disabled=!1,c.innerHTML='<span class="dashicons dashicons-yes-alt" style="vertical-align:text-top;margin-right:3px;"></span>'+gptranslate_vars.i18n_test_apikey}))}))}const _=document.querySelectorAll('input[name="gptranslate_options[enable_dropdown]"]'),E=document.querySelectorAll(".enable_dropdown_ctrl");function b(e){E.forEach((t=>{const n=t.closest("tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")}))}const h=Array.from(_).find((e=>e.checked))?.value;"1"!=h&&b(0),_.forEach((e=>{e.addEventListener("change",(function(){b(this.value)}))}));const L=document.querySelectorAll('input[name="gptranslate_options[rewrite_language_url]"]'),S=document.querySelectorAll(".urlrewriting");function T(e){S.forEach((t=>{const n=t.closest("tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")})),"1"==e&&(A("gptranslate_options[detect_current_language]","1"),A("gptranslate_options[autotranslate_detected_language]","1"))}function A(e,t){document.querySelectorAll(`input[name="${e}"]`).forEach((e=>{const n=e.closest("label");e.value===t?(e.checked=!0,n?.classList.add("active")):n?.classList.remove("active")}))}parseInt(Array.from(L).find((e=>e.checked))?.value)||T(0),L.forEach((e=>{e.addEventListener("change",(function(){T(this.value)}))}));const q=document.querySelectorAll('input[name="gptranslate_options[serverside_translations]"]'),k=document.querySelectorAll(".serverside_translations");function w(e){k.forEach((t=>{const n=t.closest(".form-table tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")}))}const N=Array.from(q).find((e=>e.checked))?.value;w(N),q.forEach((e=>{e.addEventListener("change",(function(){if(w(this.value),"1"==this.value){const e=document.querySelector('input[name="gptranslate_options[rewrite_language_url]"][value="0"]'),t=document.querySelector('input[name="gptranslate_options[rewrite_language_url]"][value="1"]');e&&t&&(t.checked=!0,e.checked=!1,t.dispatchEvent(new Event("change",{bubbles:!0})))}}))}));const x=document.querySelectorAll('input[name="gptranslate_options[translate_altimages]"]'),P=document.getElementById("css_selector_classes_translate_altimages_excluded")?.closest(".form-table tr"),G=document.getElementById("translate_srcimages")?.closest(".form-table tr");function I(e){P&&(P.style.display="1"==e?"":"none"),G&&(G.style.display="1"==e?"":"none")}const R=Array.from(x).find((e=>e.checked))?.value;I(R),x.forEach((e=>{e.addEventListener("change",(function(){I(this.value)}))}));const C=document.getElementById("serverside_translations_method"),B=document.getElementById("css_selector_serverside_leafnodes_excluded")?.closest(".form-table tr"),O=document.getElementById("serverside_translations_matchquotes1")?.closest("label");function D(e){if(B&&(B.style.display="strireplace"===e?"none":""),"simplehtmldom"===e&&O){const e=O.querySelector("input");e&&(e.checked=!0)}}if(C){1==document.querySelector('input[name="gptranslate_options[serverside_translations]"]:checked').value&&D(C.value),C.addEventListener("change",(function(){D(this.value)}))}const M=document.querySelectorAll('input[name="gptranslate_options[detect_current_language]"]'),$=document.querySelectorAll('input[name="gptranslate_options[detect_default_language]"]');M.forEach((e=>{e.addEventListener("click",(()=>{if(1===parseInt(e.value)){const e=document.querySelector('input[name="gptranslate_options[detect_default_language]"][value="0"]');e&&!e.checked&&(e.checked=!0)}}))})),$.forEach((e=>{e.addEventListener("click",(()=>{if(1===parseInt(e.value)){const e=document.querySelector('input[name="gptranslate_options[detect_current_language]"][value="0"]');e&&!e.checked&&(e.checked=!0)}}))}));const j=document.getElementById("words_leafnodes_excluded_bylanguage_repeatable-body"),H=document.getElementById("add-repeatable-row"),K=document.getElementById("words_leafnodes_excluded_bylanguage_repeatable");function U(e={}){const t=document.createElement("tr");t.classList.add("repeatable-row");const n=document.querySelector('select[name="gptranslate_options[language]"]'),a=`\n <option value="*">All languages</option>\n ${n?n.innerHTML:""}\n `;t.innerHTML=`\n <td><input type="text" class="widefat word" value="${e.word||""}" /></td>\n <td>\n <select class="widefat lang-original">\n ${a}\n </select>\n </td>\n <td>\n <select class="widefat lang-translated">\n ${a}\n </select>\n </td>\n <td><input type="text" class="widefat optional-translation" value="${e.optionalTranslation||""}" /></td>\n <td>\n <button type="button" class="button button-secondary move-row" draggable="true" title="${PLG_GPTRANSLATE_MOVE}">⇅</button>\n <button type="button" class="button button-secondary remove-row" title="${PLG_GPTRANSLATE_REMOVE}">✖</button>\n </td>\n `;const o=t.querySelector(".lang-original"),r=t.querySelector(".lang-translated");return o.value=e.langOriginal||"*",r.value=e.langTranslated||"*",t}function Y(){const e=j.querySelectorAll("tr.repeatable-row"),t=[];e.forEach((e=>{t.push({word:e.querySelector(".word").value,langOriginal:e.querySelector(".lang-original").value,langTranslated:e.querySelector(".lang-translated").value,optionalTranslation:e.querySelector(".optional-translation").value})})),K.value=JSON.stringify(t)}H.addEventListener("click",(function(){const e=U();j.appendChild(e),Y()})),j.addEventListener("click",(function(e){e.target.classList.contains("remove-row")&&(e.target.closest("tr").remove(),Y())})),j.addEventListener("input",(function(){Y()}));let X=null,F=null,J=null;j.addEventListener("dragstart",(function(e){if(e.target.classList.contains("move-row")){X=e.target.closest("tr");const t=X.getBoundingClientRect(),n=e.clientX-t.left,a=e.clientY-t.top;e.dataTransfer.setDragImage(X,n,a),e.dataTransfer.effectAllowed="move",setTimeout((function(){X.style.opacity="0.5"}),0)}})),j.addEventListener("dragend",(function(){X&&(X.style.opacity="",X=null),j.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),F=null,J=null})),j.addEventListener("dragover",(function(e){if(!X)return;e.preventDefault(),e.dataTransfer.dropEffect="move";const t=e.target.closest("tr");if(t&&t!==X){const n=t.getBoundingClientRect(),a=e.clientY-n.top>n.height/2?"after":"before";t===F&&a===J||(j.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),t.classList.add("gpt-drag-over-"+a),F=t,J=a,"after"===a?t.after(X):t.before(X))}})),j.addEventListener("drop",(function(){j.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),X&&Y()})),document.querySelector(".button-import").addEventListener("click",(function(){document.querySelectorAll(".toggle-import").forEach((e=>{e.classList.toggle("hidden")}))}));try{JSON.parse(K.value||"[]").forEach((e=>{const t=U(e);j.appendChild(t)}))}catch(Q){}}else s&&(document.getElementById("notranslations-notice")&&document.querySelectorAll(".form-filter-container, .action-buttons-toolbar, table.widefat.fixed.striped,div.tablenav").forEach((function(e){e.style.display="none"})),document.querySelector(".button-import").addEventListener("click",(function(){document.querySelectorAll(".toggle-import").forEach((e=>{e.classList.toggle("hidden")}))})),document.getElementById("toggle-migration").addEventListener("click",(function(){const e=document.getElementById("migraterow");e.style.display="none"===e.style.display||""===e.style.display?"flex":"none",e.style.alignItems="center"})),document.getElementById("migrationcancel").addEventListener("click",(function(){document.getElementById("migratetranslations_currentdomain").value="",document.getElementById("migratetranslations_newdomain").value=""})),document.getElementById("migrationconfirm").addEventListener("click",(function(){const e=document.getElementById("migratetranslations_currentdomain").value.trim(),t=document.getElementById("migratetranslations_newdomain").value.trim();if(!e||!t)return void alert("Please enter both domains.");const n=new FormData;n.append("action","gptranslate_migrate_translations"),n.append("old_domain",e),n.append("new_domain",t),n.append("_wpnonce",gptranslate_vars.nonce),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:n}).then((e=>e.json())).then((e=>{const t=document.createElement("div");if(t.className="notice is-dismissible",e.success)t.classList.add("notice-success"),t.innerHTML="<p>"+PLG_GPTRANSLATE_MIGRATION_SUCCESS+"</p>",document.querySelector("#wpcontent").prepend(t),setTimeout((()=>{window.location.reload()}),1500);else{t.classList.add("notice-error");const n=e.data||PLG_GPTRANSLATE_UNKNOWN_ERROR;t.innerHTML="<p>"+PLG_GPTRANSLATE_MIGRATION_FAILED+": "+n+"</p>",document.querySelector("#wpcontent").prepend(t)}})).catch((()=>{const e=document.createElement("div");e.className="notice notice-error is-dismissible",e.innerHTML="<p>"+PLG_GPTRANSLATE_NETWORK_ERROR+"</p>",document.querySelector("#wpcontent").prepend(e)}))})),document.getElementById("bulk-delete-btn").addEventListener("click",(function(){const e=Array.from(document.querySelectorAll('input[name="gptid[]"]:checked')).map((e=>e.value));if(0===e.length)return void alert(PLG_GPTRANSLATE_BULK_DELETE_SELECT_ONE);if(!confirm(PLG_GPTRANSLATE_BULK_DELETE_CONFIRM))return;const t=new FormData;t.append("action","gptranslate_bulk_delete"),t.append("_wpnonce",gptranslate_vars.deletenonce),e.forEach((e=>t.append("gptid[]",e))),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:t}).then((e=>e.json())).then((e=>{const t=document.createElement("div");t.className="notice is-dismissible",e.success?(t.classList.add("notice-success"),t.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_SUCCESS+"</p>",document.querySelector("#wpcontent").prepend(t),setTimeout((()=>{window.location.href=window.location.origin+window.location.pathname+"?page=gptranslate"}),250)):(t.classList.add("notice-error"),t.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_ERROR+": "+(e.data||PLG_GPTRANSLATE_UNKNOWN_ERROR)+"</p>",document.querySelector("#wpcontent").prepend(t))})).catch((()=>{const e=document.createElement("div");e.className="notice notice-error is-dismissible",e.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_NETWORK+"</p>",document.querySelector("#wpcontent").prepend(e)}))})));document.querySelectorAll(".wrapper").forEach((function(e){e.addEventListener("click",(function(t){if(t.isSynthetic)return;if("INPUT"===t.target.tagName)return;t.preventDefault();const n=e.querySelectorAll('input[type="radio"]'),a=e.querySelector('input[type="radio"]:checked');n.forEach((function(e){if(e!==a){e.checked=!0;const t=new MouseEvent("click",{bubbles:!0,cancelable:!0,view:window});t.isSynthetic=!0,e.dispatchEvent(t);const n=new Event("change",{bubbles:!0});n.isSynthetic=!0,e.dispatchEvent(n)}}))}))}));const u=document.getElementById("gptranslate-start-crawler");u&&u.addEventListener("click",(function(e){e.preventDefault(),u.disabled=!0,u.classList.add("disabled");const t=document.querySelector("input.button-crawler");t&&t.click(),setTimeout((function(){document.getElementById("crawler_dialog")&&setTimeout((function(){const e=document.getElementById("start_analysis");e&&e.click()}),100)}),100)}))}));1 function e(e,t){const n=document.getElementById(e+"-container"),a=e.replace("-","_");n.innerHTML="",Object.entries(t).forEach((([e,t])=>{const o=document.createElement("div");o.classList.add("translation-row"),o.innerHTML=`<label class="badge bg-primary">${PLG_GPTRANSLATE_ORIGINAL_TEXT}</label><textarea type="text" class="original">${e}</textarea> ➜ \n <label class="badge bg-primary">${PLG_GPTRANSLATE_TRANSLATED_TEXT}</label><textarea type="text" class="translated">${t}</textarea>\n <button type="button" class="btn btn-danger" onclick="this.parentElement.remove()">${PLG_GPTRANSLATE_DELETE}</button>\n <button type="button" class="group-sync btn btn-warning btn-invisible hasPopover" data-type="${a}" title="${PLG_GPTRANSLATE_SYNC_TITLE} - ${PLG_GPTRANSLATE_SYNC_DESC}" aria-label="${PLG_GPTRANSLATE_SYNC}">\n\t\t\t\t\t\t <span class="icon-refresh">↻</span> ${PLG_GPTRANSLATE_SYNC}</button>`,n.appendChild(o)}))}function t(e){const t=document.getElementById(e+"-container"),n={};return t.querySelectorAll("div").forEach((e=>{const t=e.querySelector(".original").value,a=e.querySelector(".translated").value;t&&a&&(n[t]=a)})),JSON.stringify(n)}document.addEventListener("DOMContentLoaded",(()=>{const n=localStorage.getItem("gptranslate_selected_tab");n&&document.getElementById("tab-"+n)&&(document.querySelectorAll(".nav-tab").forEach((e=>e.classList.remove("nav-tab-active"))),document.querySelectorAll(".tab-content").forEach((e=>e.style.display="none")),document.querySelector('a[href="#'+n+'"]').classList.add("nav-tab-active"),document.getElementById("tab-"+n).style.display="block"),"undefined"!=typeof initialTranslations&&(e("translations",initialTranslations),e("alt-translations",initialAltTranslations));const a=document.querySelector("form#edit-translations");a&&a.addEventListener("submit",(e=>{document.querySelectorAll('div.gptcard [id$="-container"]').forEach((function(e){r(e,"length-desc")})),document.getElementById("translations_json").value=t("translations"),document.getElementById("alt_translations_json").value=t("alt-translations")})),document.querySelectorAll('input[type="submit"][data-action]').forEach((e=>{e.addEventListener("click",(function(){const e=this.getAttribute("data-action");document.getElementById("form_action").value=e}))})),document.querySelectorAll("button.btn-adder").forEach((e=>{e.addEventListener("click",(function(){!function(e){const t=document.getElementById(e+"-container"),n=document.createElement("div");n.classList.add("translation-row"),n.innerHTML=`<label class="badge bg-primary">${PLG_GPTRANSLATE_ORIGINAL_TEXT}</label><textarea type="text" class="original"></textarea> ➜ \n <label class="badge bg-primary">${PLG_GPTRANSLATE_TRANSLATED_TEXT}</label><textarea type="text" class="translated"></textarea>\n <button type="button" class="btn btn-danger" onclick="this.parentElement.remove()">${PLG_GPTRANSLATE_DELETE}</button>`,t.insertBefore(n,t.firstChild)}(this.getAttribute("data-addtype"))}))})),document.querySelectorAll("textarea.translated").forEach((function(e,t){e.addEventListener("keyup",(function(e){const t=e.target.closest("div.translation-row").querySelector("button.group-sync");t&&t.classList.remove("btn-invisible")}))})),document.querySelectorAll(".gpt-apikey-toggle").forEach((function(e){e.addEventListener("click",(function(){var t=document.getElementById(e.dataset.target),n=e.querySelector(".dashicons");"password"===t.type?(t.type="text",n.classList.replace("dashicons-visibility","dashicons-hidden"),e.title=e.dataset.hide):(t.type="password",n.classList.replace("dashicons-hidden","dashicons-visibility"),e.title=e.dataset.show)}))}));const o=document.getElementById("checkall");function r(e,t){const n=Array.from(e.querySelectorAll("div.translation-row"));n.sort((function(e,n){const a=e.querySelector("textarea.original").value.toLowerCase(),o=n.querySelector("textarea.original").value.toLowerCase();switch(t){case"alpha-asc":return a.localeCompare(o);case"alpha-desc":return o.localeCompare(a);case"length-asc":return a.length-o.length;default:return o.length-a.length}})),n.forEach((function(t){e.appendChild(t)}))}o&&o.addEventListener("change",(function(){document.querySelectorAll('input[name="gptid[]"]').forEach((e=>e.checked=o.checked))})),document.addEventListener("change",(function(e){const t=e.target.closest('select[data-role="sort-translations"]');if(!t)return;r(t.closest("div.gptcard").querySelector('[id$="-container"]'),t.value)})),document.addEventListener("keydown",(function(e){if("Enter"===e.key){const t=e.target.closest('input[name="search"]');if(t){e.preventDefault();const n=t.closest("div.gptcard").querySelector('button[data-role="search-translations"]');n&&n.click()}}})),document.addEventListener("click",(function(e){const t=e.target.closest('button[data-role="search-translations"]');if(t){const e=t.closest("div.gptcard"),n=e.querySelector('input[name="search"]').value,a=e.querySelectorAll("div.translation-row"),o=new RegExp(n,"i");a.forEach((function(e){const t=e.querySelector('textarea[class="original"]').value,n=e.querySelector('textarea[class="translated"]').value;o.test(t)||o.test(n)?e.classList.remove("translation-row-hidden"):e.classList.add("translation-row-hidden")}))}const n=e.target.closest('button[data-role="reset-search"]');if(n){const e=n.closest("div.gptcard");e.querySelector('input[name="search"]').value="";e.querySelectorAll("div.translation-row").forEach((function(e){e.classList.remove("translation-row-hidden")}))}})),document.querySelectorAll("button.group-sync").forEach((function(e,t){e.addEventListener("click",(function(e){const t=e.currentTarget,n=t.querySelector("span.icon-refresh"),a=t.closest("div.translation-row"),o=a.querySelector("textarea.original"),r=a.querySelector("textarea.translated"),l=t.dataset.type,s=o.value,c=r.value,i=document.getElementById("languagetranslated").value;n.classList.add("icon-working");const d={task:"syncTranslation",original:s,translated:c,language_translated:i,translation_type:l},u=`${gptServerSideLink}`,p={method:"POST",body:JSON.stringify(d),headers:{"Content-Type":"application/json; charset=utf-8",Accept:"application/json","x-gptranslate-key":gptranslate_vars.gptApiKey}};fetch(u,p).then((e=>e.json())).then((e=>{if(e.result){const e=document.createElement("label");e.className="gpt-label badge bg-success",e.innerText=PLG_GPTRANSLATE_SYNC_COMPLETED,t.insertAdjacentElement("afterend",e)}else{const e=document.createElement("label");e.className="gpt-label badge bg-danger",e.innerText=PLG_GPTRANSLATE_SYNC_ERROR,t.insertAdjacentElement("afterend",e)}})).catch((e=>{const n=document.createElement("label");n.className="gpt-label badge bg-danger",n.innerText=PLG_GPTRANSLATE_SYNC_ERROR,t.insertAdjacentElement("afterend",n)})).finally((()=>{n.classList.remove("icon-working"),setTimeout((()=>{t.classList.add("btn-invisible"),document.querySelectorAll("label.gpt-label").forEach((e=>e.remove()))}),2e3)}))}))}));const l=document.createElement("div");l.className="custom-tooltip",document.body.appendChild(l),document.querySelectorAll("[title]").forEach((function(e){const t=e.getAttribute("title");e.addEventListener("mouseenter",(function(n){e.setAttribute("data-original-title",t),e.removeAttribute("title"),l.textContent=t,l.style.opacity="1"})),e.addEventListener("mousemove",(function(e){l.style.top=e.pageY+10+"px",l.style.left=e.pageX+10+"px"})),e.addEventListener("mouseleave",(function(){l.style.opacity="0",e.setAttribute("title",e.getAttribute("data-original-title"))}))})),jQuery(".select2-dropdown").length&&jQuery(".select2-dropdown").select2({tags:!1,tokenSeparators:[","]});const s=document.getElementById("config-gptranslate"),c=document.querySelector("div.action-buttons-toolbar");if(s){document.querySelectorAll("h2.nav-tab-wrapper a.nav-tab").forEach((function(e,t){e.addEventListener("click",(function(t){const n=e.dataset.click;var a;a=n,document.querySelectorAll(".tab-content").forEach((e=>e.style.display="none")),document.querySelectorAll(".nav-tab").forEach((e=>e.classList.remove("nav-tab-active"))),document.getElementById("tab-"+a).style.display="block",event.currentTarget.classList.add("nav-tab-active"),localStorage.setItem("gptranslate_selected_tab",a)}))}));const g=document.querySelector('select[name="gptranslate_options[google_translate_engine]"]'),m=document.querySelectorAll(".chatgpt_ctrl"),y=document.getElementById("chatgpt_apikey");function f(e){m.forEach((function(t){const n=t.closest("tr")||t.closest(".control-group")||t.closest(".form-group");n&&("0"==e?(n.style.display="",y&&y.setAttribute("data-validation","required")):(n.style.display="none",y&&y.removeAttribute("data-validation")))}))}const v=document.querySelectorAll(".google_translate_method_ctrl");function E(e){v.forEach((function(t){const n=t.closest("tr")||t.closest(".control-group")||t.closest(".form-group");n&&(n.style.display="1"==e?"":"none")}))}g&&(f(g.value),E(g.value),g.addEventListener("change",(function(){f(this.value),E(this.value)})));const _=document.getElementById("chatgpt_model"),h=document.querySelector('label[for="chatgpt_apikey"]');if(_&&h){const z=()=>{const e=h.getAttribute("data-original-text")||h.textContent;h.getAttribute("data-original-text")||h.setAttribute("data-original-text",e),h.textContent=e.replace("DeepSeek","{aiengine}").replace("Gemini","{aiengine}").replace("Claude","{aiengine}").replace("Google Cloud Translation","{aiengine}").replace("ChatGPT","{aiengine}");const t=_.value;"deepseek-chat"===t?h.textContent=h.textContent.replace("{aiengine}","DeepSeek"):0===t.indexOf("gemini-")?h.textContent=h.textContent.replace("{aiengine}","Gemini"):0===t.indexOf("claude-")?h.textContent=h.textContent.replace("{aiengine}","Claude"):0===t.indexOf("google-")?h.textContent=h.textContent.replace("{aiengine}","Google Cloud Translation"):h.textContent="deepl-api"===t?h.textContent.replace("{aiengine}","DeepL"):h.textContent.replace("{aiengine}","ChatGPT")};z(),_.addEventListener("change",z)}if(y){var i=document.createElement("button");i.type="button",i.id="btn_test_apikey",i.className="button button-secondary chatgpt_ctrl",i.innerHTML='<span class="dashicons dashicons-yes-alt" style="vertical-align:text-top;margin-right:3px;"></span>'+("undefined"!=typeof gptranslate_vars?gptranslate_vars.i18n_test_apikey:"Test API Key");var d=document.createElement("span");d.id="apikey_test_result",d.style.display="none",d.style.marginLeft="8px",d.style.marginTop="4px";const Z=y.parentNode,ee=Z.parentNode;ee.insertBefore(i,Z.nextSibling),ee.insertBefore(d,i.nextSibling),i.parentNode.insertBefore(d,i.nextSibling);var u=f;f=function(e){u(e),"0"==e?i.style.display="":(i.style.display="none",d.style.display="none")},g&&f(g.value),i.addEventListener("click",(function(){var e=y.value.trim();if(!e)return d.style.display="inline-block",d.className="gpt-test-error",void(d.textContent=gptranslate_vars.i18n_test_apikey_empty);var t=document.getElementById("chatgpt_model"),n=t?t.value:"";i.disabled=!0,i.innerHTML='<span class="dashicons dashicons-update icon-spin" style="vertical-align:text-top;margin-right:3px;"></span>'+gptranslate_vars.i18n_test_apikey_testing,d.style.display="none";var a=new FormData;a.append("action","gptranslate_test_apikey"),a.append("nonce",gptranslate_vars.testApikeyNonce),a.append("apikey",e),a.append("model",n),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:a}).then((function(e){return e.json()})).then((function(e){if(d.style.display="inline-block",e.success)d.className="gpt-test-success",d.textContent=gptranslate_vars.i18n_test_apikey_success;else{d.className="gpt-test-error";var t=gptranslate_vars.i18n_test_apikey_error;e.data&&e.data.error_code&&(t+=" ("+e.data.error_code+")"),e.data&&e.data.message&&(t+=": "+e.data.message),d.textContent=t}})).catch((function(e){d.style.display="inline-block",d.className="gpt-test-error",d.textContent=gptranslate_vars.i18n_test_apikey_error+": "+e.message})).finally((function(){i.disabled=!1,i.innerHTML='<span class="dashicons dashicons-yes-alt" style="vertical-align:text-top;margin-right:3px;"></span>'+gptranslate_vars.i18n_test_apikey}))}))}const b=document.querySelectorAll('input[name="gptranslate_options[enable_dropdown]"]'),L=document.querySelectorAll(".enable_dropdown_ctrl");function S(e){L.forEach((t=>{const n=t.closest("tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")}))}const A=Array.from(b).find((e=>e.checked))?.value;"1"!=A&&S(0),b.forEach((e=>{e.addEventListener("change",(function(){S(this.value)}))}));const T=document.querySelectorAll('input[name="gptranslate_options[rewrite_language_url]"]'),q=document.querySelectorAll(".urlrewriting");function w(e){q.forEach((t=>{const n=t.closest("tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")})),"1"==e&&(k("gptranslate_options[detect_current_language]","1"),k("gptranslate_options[autotranslate_detected_language]","1"))}function k(e,t){document.querySelectorAll(`input[name="${e}"]`).forEach((e=>{const n=e.closest("label");e.value===t?(e.checked=!0,n?.classList.add("active")):n?.classList.remove("active")}))}parseInt(Array.from(T).find((e=>e.checked))?.value)||w(0),T.forEach((e=>{e.addEventListener("change",(function(){w(this.value)}))}));const x=document.querySelectorAll('input[name="gptranslate_options[serverside_translations]"]'),N=document.querySelectorAll(".serverside_translations");function P(e){N.forEach((t=>{const n=t.closest(".form-table tr")||t.closest(".control-group");n&&(n.style.display="1"==e?"":"none")}))}const G=Array.from(x).find((e=>e.checked))?.value;P(G),x.forEach((e=>{e.addEventListener("change",(function(){if(P(this.value),"1"==this.value){const e=document.querySelector('input[name="gptranslate_options[rewrite_language_url]"][value="0"]'),t=document.querySelector('input[name="gptranslate_options[rewrite_language_url]"][value="1"]');e&&t&&(t.checked=!0,e.checked=!1,t.dispatchEvent(new Event("change",{bubbles:!0})))}}))}));const I=document.querySelectorAll('input[name="gptranslate_options[translate_altimages]"]'),R=document.getElementById("css_selector_classes_translate_altimages_excluded")?.closest(".form-table tr"),C=document.getElementById("translate_srcimages")?.closest(".form-table tr");function B(e){R&&(R.style.display="1"==e?"":"none"),C&&(C.style.display="1"==e?"":"none")}const O=Array.from(I).find((e=>e.checked))?.value;B(O),I.forEach((e=>{e.addEventListener("change",(function(){B(this.value)}))}));const D=document.getElementById("serverside_translations_method"),$=document.getElementById("css_selector_serverside_leafnodes_excluded")?.closest(".form-table tr"),M=document.getElementById("serverside_translations_matchquotes1")?.closest("label");function j(e){if($&&($.style.display="strireplace"===e?"none":""),"simplehtmldom"===e&&M){const e=M.querySelector("input");e&&(e.checked=!0)}}if(D){1==document.querySelector('input[name="gptranslate_options[serverside_translations]"]:checked').value&&j(D.value),D.addEventListener("change",(function(){j(this.value)}))}const H=document.querySelectorAll('input[name="gptranslate_options[detect_current_language]"]'),K=document.querySelectorAll('input[name="gptranslate_options[detect_default_language]"]');H.forEach((e=>{e.addEventListener("click",(()=>{if(1===parseInt(e.value)){const e=document.querySelector('input[name="gptranslate_options[detect_default_language]"][value="0"]');e&&!e.checked&&(e.checked=!0)}}))})),K.forEach((e=>{e.addEventListener("click",(()=>{if(1===parseInt(e.value)){const e=document.querySelector('input[name="gptranslate_options[detect_current_language]"][value="0"]');e&&!e.checked&&(e.checked=!0)}}))}));const U=document.getElementById("words_leafnodes_excluded_bylanguage_repeatable-body"),Y=document.getElementById("add-repeatable-row"),X=document.getElementById("words_leafnodes_excluded_bylanguage_repeatable");function F(e={}){const t=document.createElement("tr");t.classList.add("repeatable-row");const n=document.querySelector('select[name="gptranslate_options[language]"]'),a=`\n <option value="*">All languages</option>\n ${n?n.innerHTML:""}\n `;t.innerHTML=`\n <td><input type="text" class="widefat word" value="${e.word||""}" /></td>\n <td>\n <select class="widefat lang-original">\n ${a}\n </select>\n </td>\n <td>\n <select class="widefat lang-translated">\n ${a}\n </select>\n </td>\n <td><input type="text" class="widefat optional-translation" value="${e.optionalTranslation||""}" /></td>\n <td>\n <button type="button" class="button button-secondary move-row" draggable="true" title="${PLG_GPTRANSLATE_MOVE}">⇅</button>\n <button type="button" class="button button-secondary remove-row" title="${PLG_GPTRANSLATE_REMOVE}">✖</button>\n </td>\n `;const o=t.querySelector(".lang-original"),r=t.querySelector(".lang-translated");return o.value=e.langOriginal||"*",r.value=e.langTranslated||"*",t}function J(){const e=U.querySelectorAll("tr.repeatable-row"),t=[];e.forEach((e=>{t.push({word:e.querySelector(".word").value,langOriginal:e.querySelector(".lang-original").value,langTranslated:e.querySelector(".lang-translated").value,optionalTranslation:e.querySelector(".optional-translation").value})})),X.value=JSON.stringify(t)}Y.addEventListener("click",(function(){const e=F();U.appendChild(e),J()})),U.addEventListener("click",(function(e){e.target.classList.contains("remove-row")&&(e.target.closest("tr").remove(),J())})),U.addEventListener("input",(function(){J()}));let W=null,Q=null,V=null;U.addEventListener("dragstart",(function(e){if(e.target.classList.contains("move-row")){W=e.target.closest("tr");const t=W.getBoundingClientRect(),n=e.clientX-t.left,a=e.clientY-t.top;e.dataTransfer.setDragImage(W,n,a),e.dataTransfer.effectAllowed="move",setTimeout((function(){W.style.opacity="0.5"}),0)}})),U.addEventListener("dragend",(function(){W&&(W.style.opacity="",W=null),U.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),Q=null,V=null})),U.addEventListener("dragover",(function(e){if(!W)return;e.preventDefault(),e.dataTransfer.dropEffect="move";const t=e.target.closest("tr");if(t&&t!==W){const n=t.getBoundingClientRect(),a=e.clientY-n.top>n.height/2?"after":"before";t===Q&&a===V||(U.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),t.classList.add("gpt-drag-over-"+a),Q=t,V=a,"after"===a?t.after(W):t.before(W))}})),U.addEventListener("drop",(function(){U.querySelectorAll("tr.gpt-drag-over-before, tr.gpt-drag-over-after").forEach((function(e){e.classList.remove("gpt-drag-over-before","gpt-drag-over-after")})),W&&J()})),document.querySelector(".button-import").addEventListener("click",(function(){document.querySelectorAll(".toggle-import").forEach((e=>{e.classList.toggle("hidden")}))}));try{JSON.parse(X.value||"[]").forEach((e=>{const t=F(e);U.appendChild(t)}))}catch(te){}}else c&&(document.getElementById("notranslations-notice")&&document.querySelectorAll(".form-filter-container, .action-buttons-toolbar, table.widefat.fixed.striped,div.tablenav").forEach((function(e){e.style.display="none"})),document.querySelector(".button-import").addEventListener("click",(function(){document.querySelectorAll(".toggle-import").forEach((e=>{e.classList.toggle("hidden")}))})),document.getElementById("toggle-migration").addEventListener("click",(function(){const e=document.getElementById("migraterow");e.style.display="none"===e.style.display||""===e.style.display?"flex":"none",e.style.alignItems="center"})),document.getElementById("migrationcancel").addEventListener("click",(function(){document.getElementById("migratetranslations_currentdomain").value="",document.getElementById("migratetranslations_newdomain").value=""})),document.getElementById("migrationconfirm").addEventListener("click",(function(){const e=document.getElementById("migratetranslations_currentdomain").value.trim(),t=document.getElementById("migratetranslations_newdomain").value.trim();if(!e||!t)return void alert("Please enter both domains.");const n=new FormData;n.append("action","gptranslate_migrate_translations"),n.append("old_domain",e),n.append("new_domain",t),n.append("_wpnonce",gptranslate_vars.nonce),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:n}).then((e=>e.json())).then((e=>{const t=document.createElement("div");if(t.className="notice is-dismissible",e.success)t.classList.add("notice-success"),t.innerHTML="<p>"+PLG_GPTRANSLATE_MIGRATION_SUCCESS+"</p>",document.querySelector("#wpcontent").prepend(t),setTimeout((()=>{window.location.reload()}),1500);else{t.classList.add("notice-error");const n=e.data||PLG_GPTRANSLATE_UNKNOWN_ERROR;t.innerHTML="<p>"+PLG_GPTRANSLATE_MIGRATION_FAILED+": "+n+"</p>",document.querySelector("#wpcontent").prepend(t)}})).catch((()=>{const e=document.createElement("div");e.className="notice notice-error is-dismissible",e.innerHTML="<p>"+PLG_GPTRANSLATE_NETWORK_ERROR+"</p>",document.querySelector("#wpcontent").prepend(e)}))})),document.getElementById("bulk-delete-btn").addEventListener("click",(function(){const e=Array.from(document.querySelectorAll('input[name="gptid[]"]:checked')).map((e=>e.value));if(0===e.length)return void alert(PLG_GPTRANSLATE_BULK_DELETE_SELECT_ONE);if(!confirm(PLG_GPTRANSLATE_BULK_DELETE_CONFIRM))return;const t=new FormData;t.append("action","gptranslate_bulk_delete"),t.append("_wpnonce",gptranslate_vars.deletenonce),e.forEach((e=>t.append("gptid[]",e))),fetch(gptranslate_vars.ajaxurl,{method:"POST",body:t}).then((e=>e.json())).then((e=>{const t=document.createElement("div");t.className="notice is-dismissible",e.success?(t.classList.add("notice-success"),t.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_SUCCESS+"</p>",document.querySelector("#wpcontent").prepend(t),setTimeout((()=>{window.location.href=window.location.origin+window.location.pathname+"?page=gptranslate"}),250)):(t.classList.add("notice-error"),t.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_ERROR+": "+(e.data||PLG_GPTRANSLATE_UNKNOWN_ERROR)+"</p>",document.querySelector("#wpcontent").prepend(t))})).catch((()=>{const e=document.createElement("div");e.className="notice notice-error is-dismissible",e.innerHTML="<p>"+PLG_GPTRANSLATE_BULK_DELETE_NETWORK+"</p>",document.querySelector("#wpcontent").prepend(e)}))})));document.querySelectorAll(".wrapper").forEach((function(e){e.addEventListener("click",(function(t){if(t.isSynthetic)return;if("INPUT"===t.target.tagName)return;t.preventDefault();const n=e.querySelectorAll('input[type="radio"]'),a=e.querySelector('input[type="radio"]:checked');n.forEach((function(e){if(e!==a){e.checked=!0;const t=new MouseEvent("click",{bubbles:!0,cancelable:!0,view:window});t.isSynthetic=!0,e.dispatchEvent(t);const n=new Event("change",{bubbles:!0});n.isSynthetic=!0,e.dispatchEvent(n)}}))}))}));const p=document.getElementById("gptranslate-start-crawler");p&&p.addEventListener("click",(function(e){e.preventDefault(),p.disabled=!0,p.classList.add("disabled");const t=document.querySelector("input.button-crawler");t&&t.click(),setTimeout((function(){document.getElementById("crawler_dialog")&&setTimeout((function(){const e=document.getElementById("start_analysis");e&&e.click()}),100)}),100)}))}));
Note: See TracChangeset
for help on using the changeset viewer.