-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive-fixes-v106.js
More file actions
99 lines (89 loc) · 4.75 KB
/
Copy pathlive-fixes-v106.js
File metadata and controls
99 lines (89 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(() => {
'use strict';
if(location.hostname!=='chatgpt.com'||window.__NIAKGPT_LIVE_FIXES_106__)return;
window.__NIAKGPT_LIVE_FIXES_106__=true;
let breadcrumbObserver=null,statusObserver=null,globalObserver=null;
let breadcrumbNode=null,statusNode=null,timer=0,suspended=false;
const LEGACY_PROJECT_CLASSES=[
'ng8-native-projects-suppressed','ng8-native-project-link-suppressed','ng8-native-project-chat-suppressed','ng8-native-project-label-suppressed','ng8-native-project-more-suppressed',
'ng107-native-project-row','ng107-native-project-cluster','ng107-native-project-label','ng107-native-project-more',
'ng108-native-project-expando','ng112-native-projects-authoritative'
];
const clean=v=>String(v||'').replace(/\s+/g,' ').trim();
const norm=v=>clean(v).normalize('NFD').replace(/[\u0300-\u036f]/g,'').toLowerCase();
const pidFromHref=h=>String(h||'').match(/\/g\/(g-p-[^/?#]+)/i)?.[1]||'';
function clearLegacyProjectMarks(){
const selector=LEGACY_PROJECT_CLASSES.map(name=>'.'+name).join(',');
for(const el of document.querySelectorAll(selector))el.classList.remove(...LEGACY_PROJECT_CLASSES);
}
function breadcrumbContext(){
const crumb=document.querySelector('#ng100-breadcrumb .ng100-bc-project');
if(!crumb)return null;
const name=clean(crumb.textContent||crumb.getAttribute('aria-label'));
if(!name||/^(hors projet|project)$/i.test(name))return null;
return{name,pid:pidFromHref(crumb.getAttribute('href')||'')};
}
function syncStatusProject(){
const ctx=breadcrumbContext(),status=document.querySelector('#ng8-status .ng8-status-project');
if(!ctx||!status)return false;
let changed=false;
if(clean(status.textContent)!==ctx.name){status.textContent=ctx.name;changed=true;}
let pin=null;
if(ctx.pid)pin=document.querySelector(`#ng8-pins [data-ng8-pin][href*="/g/${CSS.escape(ctx.pid)}/"],#ng8-pins a[href*="/g/${CSS.escape(ctx.pid)}/"]`);
if(!pin){
const target=norm(ctx.name);
pin=[...document.querySelectorAll('#ng8-pins [data-ng8-pin],#ng8-pins a')].find(x=>norm(x.querySelector?.('span')?.textContent||x.textContent)===target)||null;
}
if(pin){
const color=pin.style.getPropertyValue('--ng-project')||getComputedStyle(pin).getPropertyValue('--ng-project');
if(clean(color))document.documentElement.style.setProperty('--ng8-current-project',clean(color));
}
document.documentElement.dataset.ng106ProjectContext=ctx.name;
window.__NIAKGPT_DIAGNOSTICS__?.set('contexte-project',`OK · ${ctx.name} · breadcrumb`);
return changed;
}
function repair(){
if(suspended)return;
clearLegacyProjectMarks();
bindTargets();
syncStatusProject();
}
function schedule(delay=24){if(suspended)return;clearTimeout(timer);timer=setTimeout(()=>{timer=0;repair();},delay);}
function bindTargets(){
const bc=document.getElementById('ng100-breadcrumb');
if(bc&&bc!==breadcrumbNode){
breadcrumbObserver?.disconnect();breadcrumbNode=bc;
breadcrumbObserver=new MutationObserver(()=>schedule(12));
breadcrumbObserver.observe(bc,{childList:true,subtree:true,characterData:true,attributes:true,attributeFilter:['href','class']});
}
const status=document.getElementById('ng8-status');
if(status&&status!==statusNode){
statusObserver?.disconnect();statusNode=status;
statusObserver=new MutationObserver(()=>schedule(12));
statusObserver.observe(status,{childList:true,subtree:true,characterData:true});
}
}
function start(){
suspended=false;clearLegacyProjectMarks();bindTargets();
globalObserver?.disconnect();
globalObserver=new MutationObserver(records=>{
if(records.some(r=>[...r.addedNodes,...r.removedNodes].some(n=>n instanceof Element)))schedule(30);
});
globalObserver.observe(document.documentElement,{childList:true,subtree:true});
for(const delay of [0,120,450,1100,2400])setTimeout(()=>schedule(0),delay);
}
function stop(){
suspended=true;clearTimeout(timer);timer=0;
breadcrumbObserver?.disconnect();statusObserver?.disconnect();globalObserver?.disconnect();
breadcrumbObserver=statusObserver=globalObserver=null;
breadcrumbNode=statusNode=null;
}
document.addEventListener('niakgpt:pins-rendered',()=>schedule(0));
document.addEventListener('niakgpt:recovery-complete',()=>schedule(30));
document.addEventListener('click',()=>schedule(70),true);
window.addEventListener('popstate',()=>schedule(20));
if(window.navigation?.addEventListener)window.navigation.addEventListener('navigatesuccess',()=>schedule(20));
window.addEventListener('pagehide',stop);
window.addEventListener('pageshow',event=>{if(event.persisted)start();});
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',start,{once:true});else start();
})();