| 1 |
(function(){"use strict";(function(){if(!window.parent||window.parent===window)return;const p=window;if(p.wp?.desktop?.iframe)return;const d=window.location.origin,l={},g=[],w={},m={},M=(n,e,o)=>{try{window.parent.postMessage({type:"desktop-mode-bridge-publish",connectionId:n,topic:e,payload:o},d)}catch{}};window.addEventListener("message",n=>{if(n.origin!==d)return;const e=n?.data;if(!(!e||typeof e!="object"||typeof e.type!="string")){if(e.type==="desktop-mode-bridge-handshake"&&typeof e.connectionId=="string"){if(l[e.connectionId]){try{window.parent.postMessage({type:"desktop-mode-bridge-handshake-ack",connectionId:e.connectionId},d)}catch{}return}const o={id:e.connectionId,topics:Array.isArray(e.topics)?e.topics.slice():[]};l[o.id]=o;try{window.parent.postMessage({type:"desktop-mode-bridge-handshake-ack",connectionId:o.id},d)}catch{}for(const t of g)try{t({id:o.id,topics:o.topics.slice()})}catch{}return}if(e.type==="desktop-mode-bridge-publish"&&typeof e.topic=="string"){const o={topic:e.topic,connectionId:e.connectionId},t=w[e.topic];if(t)for(const r of t)try{r(e.payload,o)}catch{}const s=w["*"];if(s)for(const r of s)try{r(e.payload,o)}catch{}return}if(e.type==="desktop-mode-bridge-disconnect"&&typeof e.connectionId=="string"&&delete l[e.connectionId],e.type==="desktop-mode-window-send"&&typeof e.channel=="string"){const o=e,t={channel:o.channel},s=m[o.channel];if(s)for(const a of s.slice())try{a(o.payload,t)}catch{}const r=m["*"];if(r)for(const a of r.slice())try{a(o.payload,t)}catch{}}}});const I={publish(n,e){if(!(typeof n!="string"||n===""))for(const o of Object.keys(l))M(o,n,e)},subscribe(n,e){if(typeof n!="string"||n===""||typeof e!="function")return()=>{};let o=w[n];return o||(o=[],w[n]=o),o.push(e),()=>{const t=o.indexOf(e);t>=0&&o.splice(t,1)}},onConnection(n){if(typeof n!="function")return()=>{};g.push(n);for(const e of Object.keys(l))try{n({id:l[e].id,topics:l[e].topics.slice()})}catch{}return()=>{const e=g.indexOf(n);e>=0&&g.splice(e,1)}},chrome:{setTheme(n){try{window.parent.postMessage({type:"desktop-mode-chrome-theme",tokens:n??{}},d)}catch{}},setControls(n){try{window.parent.postMessage({type:"desktop-mode-chrome-controls",config:n??null},d)}catch{}},setSlot(n,e){if(!(typeof n!="string"||n===""))try{window.parent.postMessage({type:"desktop-mode-chrome-slot",slot:n,html:typeof e=="string"?e:""},d)}catch{}}},requestConnection(n){const e=n??{},o=Array.isArray(e.topics)?e.topics.slice():[],t="wpdir-"+Math.random().toString(36).slice(2,10);return new Promise((s,r)=>{let a=!1;const k=typeof e.timeoutMs=="number"?e.timeoutMs:5e3,u=(c,i)=>{a||(a=!0,window.removeEventListener("message",b),clearTimeout(f),c?s(i):r(i))},b=c=>{if(c.origin!==d)return;const i=c?.data;if(!(!i||typeof i!="object"||i.type!=="desktop-mode-bridge-connection-ack"||i.requestId!==t))if(i.accepted){const y={id:typeof i.connectionId=="string"?i.connectionId:"",topics:o.slice()};if(typeof e.onOpen=="function")try{e.onOpen(y)}catch{}u(!0,y)}else u(!1,new Error(i.reason||"rejected"))};window.addEventListener("message",b);const f=setTimeout(()=>{u(!1,new Error("timeout"))},k);try{window.parent.postMessage({type:"desktop-mode-bridge-connection-request",requestId:t,topics:o},d)}catch(c){u(!1,c)}})}};p.wp||(p.wp={}),p.wp.desktop||(p.wp.desktop={}),p.wp.desktop.iframe=I,typeof p.wp.desktop.send!="function"&&(p.wp.desktop.send=(n,e)=>{if(!(typeof n!="string"||n===""))try{window.parent.postMessage({type:"desktop-mode-window-publish",channel:n,payload:e},d)}catch{}}),typeof p.wp.desktop.on!="function"&&(p.wp.desktop.on=(n,e)=>{if(typeof n!="string"||n===""||typeof e!="function")return()=>{};let o=m[n];return o||(o=[],m[n]=o),o.push(e),()=>{const t=o.indexOf(e);t>=0&&o.splice(t,1)}});const h=window;if(h.__desktopModeScreenMetaInstalled||(h.__desktopModeScreenMetaInstalled=!0,E(d)),!h.__desktopModeOsFileDropForwarderInstalled){h.__desktopModeOsFileDropForwarderInstalled=!0;const n=t=>{const s=t.dataTransfer?.types;if(!s)return!1;const r=s;if(typeof r.includes=="function")return r.includes("Files");if(typeof r.contains=="function")return r.contains("Files");for(let a=0;a<r.length;a++)if(r[a]==="Files")return!0;return!1},e=[".components-drop-zone","[data-drop-zone]",".uploader-window",".media-frame-content"],o=t=>{const s=t;if(!s||!s.closest)return!1;for(const r of e)if(s.closest(r))return!0;return!1};document.addEventListener("dragover",t=>{n(t)&&(o(t.target)||(t.preventDefault(),t.dataTransfer&&(t.dataTransfer.dropEffect="copy")))},!0),document.addEventListener("drop",t=>{if(!n(t)||o(t.target))return;t.preventDefault(),t.stopPropagation();const s=[];if(t.dataTransfer?.files)for(let r=0;r<t.dataTransfer.files.length;r++)s.push(t.dataTransfer.files[r]);if(s.length!==0)try{window.parent.postMessage({type:"desktop-mode-os-file-drop",files:s,x:t.clientX,y:t.clientY},d)}catch{}},!0)}function E(n){const e=()=>{if(!document.getElementById("screen-meta-links"))return;const t=document.getElementById("show-settings-link"),s=document.getElementById("contextual-help-link"),r=[];if(t&&r.push("screen-options"),s&&r.push("help"),r.length===0)return;try{window.parent.postMessage({type:"desktop-mode-screen-meta",panels:r},n)}catch{}const a=()=>t&&t.getAttribute("aria-expanded")==="true"?"screen-options":s&&s.getAttribute("aria-expanded")==="true"?"help":null,k=()=>{try{window.parent.postMessage({type:"desktop-mode-screen-meta-state",open:a()},n)}catch{}};k();const u=new MutationObserver(k);t&&u.observe(t,{attributes:!0,attributeFilter:["aria-expanded"]}),s&&u.observe(s,{attributes:!0,attributeFilter:["aria-expanded"]});const b=f=>{if(!f||f.getAttribute("aria-expanded")!=="true")return;const c=f.getAttribute("aria-controls"),i=c?document.getElementById(c):null;if(!i)return;const y=window.jQuery;if(y)try{y(i).stop(!0,!1)}catch{}i.style.display="none",i.classList.add("hidden"),i.parentElement&&(i.parentElement.style.display="none"),f.classList.remove("screen-meta-active"),f.setAttribute("aria-expanded","false"),document.querySelectorAll(".screen-meta-toggle").forEach(O=>{O.style.visibility=""})};window.addEventListener("message",f=>{if(f.origin!==n)return;const c=f.data;if(!c||c.type!=="desktop-mode-toggle-panel")return;let i=null;c.panel==="screen-options"&&t?i=t:c.panel==="help"&&s&&(i=s),i&&(i.getAttribute("aria-expanded")!=="true"&&b(i===t?s:t),i.click())})};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",e,{once:!0}):e()}})()})(); |