-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle-guard-v104.js
More file actions
29 lines (27 loc) · 1.28 KB
/
Copy pathlifecycle-guard-v104.js
File metadata and controls
29 lines (27 loc) · 1.28 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
(() => {
'use strict';
if (location.hostname !== 'chatgpt.com' || window.__NIAKGPT_LIFECYCLE_GUARD_104__) return;
window.__NIAKGPT_LIFECYCLE_GUARD_104__ = true;
const NativeBroadcastChannel=window.BroadcastChannel;
if(typeof NativeBroadcastChannel!=='function')return;
const closedMessage=e=>e?.name==='InvalidStateError'||/channel is closed|broadcastchannel.*closed|closed broadcastchannel/i.test(String(e?.message||e||''));
function SafeBroadcastChannel(name){
const channel=new NativeBroadcastChannel(name);
let closed=false;
const nativePost=channel.postMessage.bind(channel);
const nativeClose=channel.close.bind(channel);
channel.postMessage=function(data){
if(closed)return;
try{return nativePost(data);}catch(error){if(closedMessage(error)){closed=true;return;}throw error;}
};
channel.close=function(){
if(closed)return;closed=true;
try{return nativeClose();}catch(error){if(!closedMessage(error))throw error;}
};
return channel;
}
try{Object.defineProperty(SafeBroadcastChannel,'name',{value:'BroadcastChannel'});}catch{}
SafeBroadcastChannel.prototype=NativeBroadcastChannel.prototype;
try{Object.setPrototypeOf(SafeBroadcastChannel,NativeBroadcastChannel);}catch{}
window.BroadcastChannel=SafeBroadcastChannel;
})();