File indexing completed on 2025-01-26 05:27:59

0001 import React from 'react';
0002 import ReactDOM from 'react-dom';
0003 import MetaHeader from './components/MetaHeader';
0004 import '@webcomponents/custom-elements';
0005 async function initConfig(target, redirect)
0006 {
0007          let url = `https://www.opendesktop.org/home/metamenubundlejs?target=${target}&url=${redirect}`;
0008          if (window.location.hostname.endsWith('cc')) {
0009             url = `https://www.opendesktop.cc/home/metamenubundlejs?target=${target}&url=${redirect}`;
0010           }
0011 
0012 
0013           try {
0014             const response = await fetch(url, {
0015               mode: 'cors',
0016               credentials: 'include'
0017             });
0018             if (!response.ok) {
0019               throw new Error('Network response error');
0020             }
0021             let config = await response.json();
0022             config.isAdmin = config.json_isAdmin;
0023             config.showModal = false;
0024             config.isExternal = true;
0025             config.modalUrl = '';
0026             return config;
0027           }
0028           catch (error) {
0029             console.error(error);
0030             return false;
0031           }
0032 
0033 
0034 }
0035 
0036 // async function f() {
0037 //   let config = await initConfig('opendesktop',window.location.href); // wait till the promise resolves (*)
0038 //   ReactDOM.render(<MetaHeader config={config} hostname={window.location.hostname}/>, document.getElementById('metaheader'))
0039 // }
0040 //
0041 // f();
0042 
0043 
0044 customElements.define('opendesktop-metaheader', class extends HTMLElement {
0045   constructor() {
0046     super();
0047     this.buildComponent();
0048   }
0049 
0050   async buildComponent() {
0051 
0052     const stylesheetElement = document.createElement('link');
0053     stylesheetElement.rel = 'stylesheet';
0054     stylesheetElement.href = 'https://www.opendesktop.org/theme/react/assets/css/metaheader.css';
0055 
0056     if (window.location.hostname.endsWith('cc')) {
0057       stylesheetElement.href = 'https://www.opendesktop.cc/theme/react/assets/css/metaheader.css';
0058     }/*else if(location.hostname.endsWith('localhost'))
0059     {
0060       //stylesheetElement.href = 'https://www.opendesktop.cc/theme/react/assets/css/metaheader.css';
0061     }*/
0062     // else if (location.hostname.endsWith('localhost')) {
0063     //   stylesheetElement.href = 'https://www.opendesktop.cc/theme/react/assets/css/metaheader.css';
0064     // }else if (location.hostname.endsWith('local')) {
0065     //     stylesheetElement.href = 'https://www.opendesktop.cc/theme/react/assets/css/metaheader.css';
0066     // }
0067     // else{
0068     //    stylesheetElement.href = 'https://www.opendesktop.org/theme/react/assets/css/metaheader.css';
0069     // }
0070     this.appendChild(stylesheetElement);
0071 
0072     const metaheaderElement = document.createElement('div');
0073     metaheaderElement.id = 'metaheader';
0074     let config = await initConfig(this.getAttribute('config-target'),window.location.href); // wait till the promise resolves (*)
0075 
0076 
0077     ReactDOM.render(<MetaHeader config={config} />, metaheaderElement);
0078 
0079 
0080     // Component must be capsule within Shadow DOM, and don't hack
0081     // context/scope of external sites.
0082     /*
0083     this.attachShadow({mode: 'open'});
0084     this.shadowRoot.appendChild(stylesheetElement);
0085     this.shadowRoot.appendChild(metaheaderElement);
0086     */
0087 
0088     // However, make this as Light DOM for now, because current
0089     // implementation is not real component design yet.
0090     // Need solve event handling, scoped CSS.
0091     this.appendChild(metaheaderElement);
0092   }
0093 });