Intelligence, Advanced Charting & Trading Platform

1 /**
2 * Reversible and fail-closed patch of the deployed ChartNova HTML bootstrap.
3 * Leaves provider APIs, cached values, bindings and market data untouched.
4 */
5 const beforeMarkets=' renderMarkets();renderTechnical();renderSelected();\n}\nasync function refresh(){';
6 const afterMarkets=[
7 ' // Render only the visible view; do not repeatedly build hidden 58-card grids.',
8 ' if(!el("markets").hidden)renderMarkets();',
9 ' if(!el("technical").hidden)renderTechnical();',
10 ' if(!el("dashboard").hidden)renderSelected();',
11 ' if(!el("widget").hidden)renderWidget();',
12 '}',
13 'async function refresh(){'
14 ].join('\n');
15 const beforeRefresh=[
16 'async function refresh(){',
17 ' el("status").textContent="در حال دریافت داده‌های ذخیره‌شده...";',
18 ' await Promise.allSettled([loadDomestic(),loadGold(),loadMarkets()]);',
19 ' renderSelected();renderMarkets();renderTechnical();',
20 ' const age=Date.parse(marketState.receivedAt||"");',
21 ' el("status").textContent=marketState.loaded?(Number.isFinite(age)&&Date.now()-age<=45*60000?"داده بازار به‌روز · بررسی هر ۱۵ دقیقه":"آخرین داده ذخیره‌شده · غیرلحظه‌ای"):"در انتظار داده نمادها · قیمت‌های موجود";',
22 '}'
23 ].join('\n');
24 const afterRefresh=[
25 'let chartnovaRefreshInFlight=null;',
26 'function refresh(){',
27 ' if(chartnovaRefreshInFlight)return chartnovaRefreshInFlight;',
28 ' const status=el("status");',
29 ' status.textContent="در حال دریافت قیمت‌های اصلی...";',
30 ' const onPrimaryLoaded=()=>{',
31 ' if(!el("dashboard").hidden)renderSelected();',
32 ' if(!marketState.loaded)status.textContent="قیمت‌های اصلی بررسی شدند · در حال دریافت نمادها...";',
33 ' };',
34 ' chartnovaRefreshInFlight=Promise.allSettled([',
35 ' loadDomestic().finally(onPrimaryLoaded),',
36 ' loadGold().finally(onPrimaryLoaded),',
37 ' loadMarkets()',
38 ' ]).then(()=>{',
39 ' const age=Date.parse(marketState.receivedAt||"");',
40 ' status.textContent=marketState.loaded?(Number.isFinite(age)&&Date.now()-age<=45*60000?',
41 ' "داده بازار به‌روز · بررسی هر ۱۵ دقیقه":"آخرین داده ذخیره‌شده · غیرلحظه‌ای"):',
42 ' "داده قیمت بررسی شد · نمادها در دسترس نیستند";',
43 ' }).finally(()=>{chartnovaRefreshInFlight=null});',
44 ' return chartnovaRefreshInFlight;',
45 '}'
46 ].join('\n');
47
48 function replaceExactlyOnce(value,oldPart,newPart,label){
49 const count=value.split(oldPart).length-1;
50 if(count!==1)throw new Error(label+' expected once, got '+count);
51 return value.replace(oldPart,newPart);
52 }
53 export function optimizeChartNovaWorker(worker){
54 if(typeof worker!=='string'||worker.length<10000)throw new Error('Unexpected Worker size');
55 const anchor='return new Response("<!doctype html>';
56 const start=worker.indexOf(anchor);
57 const end=worker.indexOf('",{headers:{"content-type":"text/html; charset=utf-8"',start);
58 if(start<0||end<0)throw new Error('Expected production HTML literal not found');
59 const literalStart=start+'return new Response('.length;
60 const htmlLiteral=worker.slice(literalStart,end+1);
61 let html;try{html=JSON.parse(htmlLiteral)}catch{throw new Error('HTML literal is not valid JSON string')}
62 const first=html.indexOf('<script>'),last=html.indexOf('</script>',first);
63 if(first<0||last<0||html.indexOf('<script>',first+1)!==-1)throw new Error('Unexpected inline script count');
64 const js=html.slice(first+8,last);
65 let updated=replaceExactlyOnce(js,beforeMarkets,afterMarkets,'lazy view');
66 updated=replaceExactlyOnce(updated,beforeRefresh,afterRefresh,'progressive refresh');
67 const newHTML=html.slice(0,first+8)+updated+html.slice(last);
68 if(newHTML.replace(updated,js)!==html)throw new Error('Unexpected HTML mutation');
69 const result=worker.slice(0,literalStart)+JSON.stringify(newHTML)+worker.slice(end+1);
70 if(result===worker)throw new Error('No source update');
71 return {source:result,oldHTML:html,newHTML,oldJS:js,newJS:updated};
72 }