-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathChatGPT DeMod.user.js
865 lines (758 loc) · 23.9 KB
/
ChatGPT DeMod.user.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// ==UserScript==
// @name ChatGPT DeMod
// @namespace pl.4as.chatgpt
// @version 5.1
// @description Hides moderation results during conversations with ChatGPT
// @author 4as
// @match *://chatgpt.com/*
// @match *://chat.openai.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @downloadURL https://raw.githubusercontent.com/4as/ChatGPT-DeMod/main/ChatGPT%20DeMod.user.js
// @updateURL https://raw.githubusercontent.com/4as/ChatGPT-DeMod/main/ChatGPT%20DeMod.user.js
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
'use strict';
const target_window = typeof(unsafeWindow) === 'undefined' ? window : unsafeWindow;
const original_fetch = target_window.fetch;
const DEMOD_ID = 'demod-cont';
const DEMOD_KEY = 'DeModState';
var is_on = false;
var is_over = false;
const ButtonState = {
DISABLED: 0,
OFF: 1,
ON: 2,
};
var demod_button;
function updateDeModState() {
if (is_on) {
updateButton(demod_button, ButtonState.ON, "DeMod:");
} else {
updateButton(demod_button, ButtonState.OFF, "DeMod:");
}
}
const ModerationResult = {
UNKNOWN: 0,
SAFE: 1,
FLAGGED: 2,
BLOCKED: 3,
};
var demod_status;
function updateDeModMessageState(mod_result) {
if (demod_status === null)
return;
switch (mod_result) {
case ModerationResult.UNKNOWN:
demod_status.style.border = '0px';
demod_status.textContent = "Latest: None";
demod_status.style.backgroundColor = '#9A9A9A';
break;
case ModerationResult.SAFE:
demod_status.style.border = '0px';
demod_status.textContent = "Latest: Safe";
demod_status.style.backgroundColor = '#4CAF50';
break;
case ModerationResult.FLAGGED:
demod_status.style.border = '1px dotted white';
demod_status.textContent = "Latest: Flagged";
demod_status.style.backgroundColor = '#ACA950';
break;
case ModerationResult.BLOCKED:
demod_status.style.border = '1px solid white';
demod_status.textContent = "Latest: BLOCKED";
demod_status.style.backgroundColor = '#AF4C50';
break;
}
}
function updateButton(button, state, label) {
if (button === null)
return;
if (is_over) {
button.style.height = 'auto';
button.style.border = '0px';
button.style.padding = '4px 12px';
button.style.opacity = 1;
button.style.borderRadius = '4px';
switch (state) {
case ButtonState.DISABLED:
button.style.opacity = 0.5;
button.style.backgroundColor = '#AAAAAA';
button.textContent = label + " N/A";
break;
case ButtonState.OFF:
button.style.backgroundColor = '#AF4C50';
button.textContent = label + " Off";
break;
case ButtonState.ON:
button.style.border = '1px dotted white';
button.style.padding = '3px 11px';
button.style.backgroundColor = '#4CAF50';
button.textContent = label + " On";
break;
}
} else {
button.textContent = "";
button.style.height = '6px';
button.style.padding = '0px';
button.style.opacity = 1;
button.style.borderRadius = '0px';
switch (state) {
case ButtonState.DISABLED:
button.style.opacity = 0.5;
button.style.backgroundColor = '#AAAAAA';
break;
case ButtonState.OFF:
button.style.backgroundColor = '#AF4C50';
break;
case ButtonState.ON:
button.style.border = '1px dotted white';
button.style.backgroundColor = '#4CAF50';
break;
}
}
}
function hasFlagged(text) {
return text.match(/(\"blocked\"|\"flagged\"): ?true/ig);
}
function hasBlocked(text) {
return text.match(/(\"blocked\"): ?true/ig);
}
function clearFlagging(text) {
// repeated replacement to ensure the style stays consistant
text = text.replaceAll(/\"flagged\": true/ig, "\"flagged\": false");
text = text.replaceAll(/\"blocked\": true/ig, "\"blocked\": false");
text = text.replaceAll(/\"flagged\":true/ig, "\"flagged\":false");
text = text.replaceAll(/\"blocked\":true/ig, "\"blocked\":false");
return text;
}
const ConversationType = {
UNKNOWN: 0,
INIT: 1,
PROMPT: 2,
};
// DeMod state control
function getDeModState() {
var state = target_window.localStorage.getItem(DEMOD_KEY);
if (state == null)
return true;
return (state == "false") ? false : true;
}
function setDeModState(demod_on) {
target_window.localStorage.setItem(DEMOD_KEY, demod_on);
}
// Interceptors shared data
const DONE = "[DONE]";
var init_cache = null;
var backup_cache = null;
var response_blocked = false;
var payload = null;
var last_response = null;
var last_conv_id = null;
var mod_result = ModerationResult.UNKNOWN;
var sequence_shift = 0; //each sequence in the generated response has an id and all ids need to match, so DeMod shifts them to insert own messages if needed.
var decoder = new TextDecoder();
var encoder = new TextEncoder();
function decodeData(data) {
if (typeof data == 'string') {
return data;
} else if (data.byteLength != undefined) {
return decoder.decode(new Uint8Array(data));
}
return null;
}
function cloneRequest(request, fetch_url, method, body) {
return new Request(fetch_url, {
method: method,
headers: request.headers,
body: JSON.stringify(body),
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
mode: request.mode,
credentials: request.credentials,
cache: request.cache,
redirect: request.redirect,
integrity: request.integrity,
});
}
function cloneEvent(event, new_data) {
return new MessageEvent('message', {
data: new_data,
origin: event.origin,
lastEventId: event.lastEventId,
source: event.source,
ports: event.ports
});
}
function redirectConversations(url, conversation_id) {
var idx = url.indexOf("/textdocs");
if (idx !== -1)
return url.substring(0, idx);
idx = url.indexOf("/conversations");
if (idx === -1)
return url;
return url.substring(0, idx) + "/conversation/" + conversation_id;
}
function parseLatest(redownload_text) {
var latest = null;
var redownload_object = null;
try {
redownload_object = JSON.parse(redownload_text);
} catch (e) {
console.log("[DEMOD] Failed to parse re-downloaded response.");
}
if (redownload_object !== null && redownload_object.hasOwnProperty('mapping')) {
var latest_time = 0;
for (var map_key in redownload_object.mapping) {
var map_obj = redownload_object.mapping[map_key];
if (map_obj.hasOwnProperty('message') && map_obj.message != null
&& map_obj.message.hasOwnProperty('create_time') && map_obj.message.create_time > latest_time) {
latest = map_obj.message;
}
}
}
return latest;
}
async function redownloadLatest() {
var original_request = init_cache;
if (original_request === null) {
if (last_conv_id === null)
return null;
original_request = backup_cache;
}
var fetch_url = null;
if (typeof(original_request[0]) !== 'string') {
fetch_url = redirectConversations(original_request[0].href, last_conv_id);
original_request[0] = cloneRequest(original_request[0], fetch_url, "GET", "");
} else {
fetch_url = redirectConversations(original_request[0], last_conv_id);
original_request[0] = fetch_url;
original_request[1].method = "GET";
delete original_request[1].body;
}
var latest = null;
var init_redownload = original_fetch(...original_request);
var redownload_result = await init_redownload;
if (redownload_result.ok) {
var redownload_text = await redownload_result.text();
latest = parseLatest(redownload_text);
}
if (latest === null) {
await new Promise(r => setTimeout(r, 3000));
init_redownload = original_fetch(...original_request);
redownload_result = await init_redownload;
if (redownload_result.ok) {
redownload_text = await redownload_result.text();
latest = parseLatest(redownload_text);
}
}
if (latest !== null)
console.log("[DEMOD] Latest response redownloaded successfully.");
return latest;
}
class ChatPayload {
data;
message;
constructor() {
this.data = {
"p": "",
"o": "patch",
"v": [{
"p": "/message/content/parts/0",
"o": "append",
"v": ""
}, {
"p": "/message/status",
"o": "replace",
"v": "finished_successfully"
}, {
"p": "/message/end_turn",
"o": "replace",
"v": true
}, {
"p": "/message/metadata",
"o": "append",
"v": {
"is_complete": true,
"finish_details": {
"type": "stop",
"stop_tokens": [200002]
}
}
}
]
}
}
getData() {
return "event: delta\ndata: " + JSON.stringify(this.data) + "\n\n";
}
update(chunk_data) {
if (ChatPayload.isPatch(chunk_data)) {
this.data = chunk_data;
if (this.message !== null)
this.setMessage(this.message);
}
}
setText(text) {
var v = this.data.v;
for (let i = 0; i < v.length; ++i) {
var entry = v[i];
if (entry.hasOwnProperty("p") && entry.p.indexOf("parts") !== -1) {
entry.o = "replace";
entry.v = text;
}
}
}
setMessage(message) {
var v = this.data.v;
for (let i = v.length - 1; i > -1; --i) {
var value = v[i];
if (value.hasOwnProperty("p") && value.p.indexOf("parts") !== -1) {
v.splice(i, 1);
}
}
var parts = message.content.parts;
for (let i = parts.length - 1; i > -1; --i) {
var part = parts[i];
v.push({
"p": "/message/content/parts/" + i,
"o": "replace",
"v": part
});
}
}
static isPatch(chunk_data) {
return chunk_data.hasOwnProperty("o") && chunk_data.o === "patch" && chunk_data.hasOwnProperty("v");
}
static getConversationId(chunk_data) {
if (chunk_data.hasOwnProperty('conversation_id'))
return chunk_data.conversation_id;
if (chunk_data.hasOwnProperty('v') && chunk_data.v.hasOwnProperty('conversation_id'))
return chunk_data.v.conversation_id;
return null;
}
}
class ChatResponse {
chunk;
chunk_start;
payload;
conversation_id;
is_done = false;
is_blocked = false;
handle_latest = false;
mod_result = ModerationResult.SAFE;
queue = [];
constructor(existing_payload, decoded_chunk, download_latest) {
this.payload = existing_payload;
this.chunk = decoded_chunk;
this.chunk_start = this.chunk.indexOf("data: ");
this.handle_latest = download_latest;
}
async process(current_blocked) {
this.is_blocked = current_blocked;
if (this.chunk_start == -1) {
this.queue.push(this.chunk);
return;
}
if (hasFlagged(this.chunk) || ChatPayload.isPatch(this.chunk) || (this.is_blocked && this.chunk.indexOf(DONE) !== -1)) {
while (this.chunk_start != -1 && !this.is_done) {
var chunk_end = this.chunk.indexOf("\n", this.chunk_start);
if (chunk_end == -1)
chunk_end = this.chunk.length - 1;
var chunk_text = this.chunk.substring(this.chunk_start + 5, chunk_end).trim();
if (chunk_text === DONE) {
this.is_done = true;
if (this.handle_latest && this.is_blocked) {
console.log("[DEMOD] Blocked response finished, attempting to reload it from history.");
var latest = await redownloadLatest();
if (latest !== null) {
this.payload.setMessage(latest);
this.queue.push(this.payload.getData());
} else {
this.payload.setText("DeMod: Request completed, but DeMod failed to access the history. Try refreshing the conversation instead.");
this.queue.push(this.payload.getData());
}
}
} else {
var chunk_data = null;
try {
chunk_data = JSON.parse(chunk_text);
var conv_id = ChatPayload.getConversationId(chunk_data);
if (conv_id !== null) {
this.conversation_id = conv_id;
last_conv_id = conv_id;
}
} catch (e) {}
if (chunk_data !== null) {
if (chunk_data.hasOwnProperty('moderation_response')) {
var has_flag = chunk_data.moderation_response.flagged === true;
var has_block = chunk_data.moderation_response.blocked === true;
if (has_flag || has_block) {
if (has_flag) {
if (this.mod_result !== ModerationResult.BLOCKED)
this.mod_result = ModerationResult.FLAGGED;
}
console.log("[DEMOD] Received chunk contains flagging/blocking properties, clearing");
chunk_data.moderation_response.flagged = false;
chunk_data.moderation_response.blocked = false;
}
if (has_block) {
console.log("[DEMOD] Message has been BLOCKED. Waiting for ChatGPT to finalize the request...");
this.mod_result = ModerationResult.BLOCKED;
this.is_blocked = true;
this.payload.setText("DeMod: Moderation has intercepted the response and is actively blocking it. Waiting for ChatGPT to finalize the request so DeMod can fetch it from the conversation's history...");
//this.queue.push(this.payload.getData());
}
}
}
}
this.chunk_start = this.chunk.indexOf("data: ", chunk_end + 1);
}
var cleaned = clearFlagging(this.chunk);
this.queue.push(cleaned);
} else {
this.queue.push(this.chunk);
}
return true;
}
}
class ChatEvent {
event;
response_data;
response_object;
response_body;
response = null;
sequence_id;
constructor(current_payload, event) {
this.event = event;
this.response_data = decodeData(event.data);
this.response_object = JSON.parse(this.response_data);
this.sequence_id = this.response_object.sequenceId;
if (this.has_body) {
this.response_body = atob(this.response_object.data.body);
this.response = new ChatResponse(current_payload, this.response_body, false);
}
}
get is_valid() {
return this.response != null;
}
get conversation_id() {
return this.response.conversation_id;
}
get has_body() {
return this.response_object != null && this.response_object.type == "message" && this.response_object.dataType == "json" && this.response_object.data.body != null;
}
get is_blocked() {
return this.response.is_blocked;
}
get is_done() {
return this.response.is_done;
}
get mod_result() {
return this.response.mod_result;
}
get payload() {
if (this.is_valid)
return this.response.payload;
else
return null;
}
async process(current_blocked) {
await this.response.process(current_blocked);
var data = "";
for (const entry of this.response.queue) {
data += entry;
}
this.response_body = data;
}
getEvent() {
this.response_object.sequenceId = this.sequence_id;
this.response_object.data.body = btoa(this.response_body);
var updated_data = JSON.stringify(this.response_object);
return cloneEvent(this.event, updated_data);
}
clone() {
var copy = new ChatEvent(this.payload, this.event);
copy.response_body = this.response_body;
return copy;
}
}
// Intercepter for old fetch() based communication
const intercepter_fetch = async function (target, this_arg, args) {
if (!is_on) {
return target.apply(this_arg, args);
}
var original_arg = args;
var fetch_url = args[0];
var is_request = false;
if (typeof(fetch_url) !== 'string') {
fetch_url = fetch_url.href;
is_request = true;
}
if (fetch_url.indexOf('/share/create') != -1) {
console.log("[DEMOD] Share request detected, blocking.");
return new Response("", {
status: 404,
statusText: "Not found"
});
}
var is_conversation = fetch_url.indexOf('/complete') !== -1 || (fetch_url.indexOf('/conversation') !== -1 && fetch_url.indexOf('/conversations') === -1);
var convo_type = ConversationType.UNKNOWN;
if (is_conversation) {
if (fetch_url.indexOf("/gen_title") != -1) {
var init_url = fetch_url.replace("/gen_title", "");
if (is_request) {
console.log("[DEMOD] Generating title (Request).");
args = cloneRequest(args[0], init_url, "GET", null);
args.headers.delete("Content-Type");
} else {
console.log("[DEMOD] Generating title (basic).");
args = JSON.parse(JSON.stringify(args));
args[0] = init_url;
args[1].method = "GET";
delete args[1].headers["Content-Type"];
delete args[1].body;
}
}
var conv_request = null;
if (is_request) {
if (args[0] !== undefined && args[0].hasOwnProperty('text') && (typeof args[0].text === 'function')) {
conv_request = await args[0].text();
}
} else {
if (args[1] !== undefined && args[1].hasOwnProperty('body')) {
conv_request = args[1].body;
}
}
if (conv_request) {
convo_type = ConversationType.PROMPT;
var conv_body = JSON.parse(conv_request);
if (is_request) {
args[0] = cloneRequest(args[0], fetch_url, args[0].method, conv_body);
} else {
args[1].body = JSON.stringify(conv_body);
}
} else {
convo_type = ConversationType.INIT;
init_cache = args;
}
} else if (fetch_url.indexOf('/conversations') !== -1) {
backup_cache = JSON.parse(JSON.stringify(args));
}
var original_promise = target.apply(this_arg, original_arg);
if (is_conversation) {
var original_result = await original_promise;
if (!original_result.ok) {
return original_result;
}
switch (convo_type) {
case ConversationType.PROMPT: {
payload = new ChatPayload();
sequence_shift = 0;
last_response = null;
response_blocked = false;
mod_result = ModerationResult.SAFE;
updateDeModMessageState(mod_result);
console.log("[DEMOD] Processing basic prompted conversation. Scanning for moderation results...");
const stream = new ReadableStream({
async start(controller) {
var reader = original_result.body.getReader();
while (true) {
const {
done,
value
} = await reader.read();
var raw_chunk = value || new Uint8Array;
var chunk = decoder.decode(raw_chunk);
var response = new ChatResponse(payload, chunk, true);
await response.process(response_blocked);
if (mod_result < response.mod_result) {
mod_result = response.mod_result;
updateDeModMessageState(mod_result);
}
response_blocked = response.is_blocked;
payload = response.payload;
for (const entry of response.queue) {
const encoded_chunk = encoder.encode(entry);
controller.enqueue(encoded_chunk);
}
if (response.is_done || done) {
controller.close();
break;
}
}
},
});
return new Response(stream, {
status: original_result.status,
statusText: original_result.statusText,
headers: original_result.headers,
});
break;
}
case ConversationType.INIT: {
console.log("[DEMOD] Processing conversation initialization. Checking if the conversation has existing moderation results.");
var convo_init = await original_result.text();
convo_init = clearFlagging(convo_init);
updateDeModMessageState(ModerationResult.UNKNOWN);
return new Response(convo_init, {
status: original_result.status,
statusText: original_result.statusText,
headers: original_result.headers,
});
break;
}
}
}
return original_promise;
}
const intercepter = new Proxy(original_fetch, {
apply: intercepter_fetch
});
target_window.fetch = intercepter;
// Interceptor for new WebSocket communication (credit to WebSocket Logger for making this possible)
var original_websocket = target_window.WebSocket;
target_window.WebSocket = new Proxy(original_websocket, {
construct: function (target, args, newTarget) {
var ws = new target(...args);
console.log("[DEMOD] WebSocket interceptor created, connecting to: " + args[0]);
var buffer = [];
async function processMessage(original_onmessage, event) {
if (!is_on)
original_onmessage(event);
var response = new ChatEvent(payload, event);
if (response.is_valid) {
await response.process(response_blocked);
if (mod_result < response.mod_result) {
mod_result = response.mod_result;
updateDeModMessageState(mod_result);
}
response_blocked = response.is_blocked;
payload = response.payload;
if (response.has_body) {
last_response = response.clone();
last_response.sequence_id += sequence_shift;
}
if (response_blocked) {
if (response.is_done) {
if (last_response != null) {
console.log("[DEMOD] Response blocked, redownloading from history.");
var latest = await redownloadLatest();
last_response.replace(latest);
buffer.push(last_response);
sequence_shift++;
}
}
}
response.sequence_id += sequence_shift;
buffer.push(response);
var entry;
try {
for (entry of buffer) {
var entry_event = entry.getEvent();
original_onmessage(entry_event);
}
buffer.length = 0;
} catch (e) {
console.log("[DEMOD] Failed to send parsed response: " + entry.response_data + "\n\nWith body: " + entry.response_body);
}
} else {
original_onmessage(event);
}
};
var ws_proxy = {
set: function (target, prop, v) {
if (prop == 'onmessage') {
var original_onmessage = v;
v = (e) => processMessage(original_onmessage, e);
}
return (target[prop] = v);
}
};
return new Proxy(ws, ws_proxy);
}
});
var demod_init = async function () {
if (document.getElementById(DEMOD_ID) || !document.body)
return;
// Adding the "hover" area for the DeMod button.
const demod_div = document.createElement('div');
demod_div.setAttribute('id', DEMOD_ID);
demod_div.style.position = 'fixed';
demod_div.style.top = '0px';
demod_div.style.left = '50%';
demod_div.style.transform = 'translate(-50%, 0%)';
demod_div.style.width = '254px';
demod_div.style.height = '24px';
demod_div.style.display = 'inline-block';
demod_div.style.verticalAlign = 'top';
demod_div.style.zIndex = 999;
// Adding the actual DeMod button
demod_button = document.createElement('button');
demod_button.style.color = 'white';
demod_button.style.height = '6px';
demod_button.style.width = '124px';
demod_button.style.border = 'none';
demod_button.style.cursor = 'pointer';
demod_button.style.outline = 'none';
demod_button.style.display = 'inline-block';
demod_button.style.verticalAlign = 'top';
demod_div.appendChild(demod_button);
const demod_space = document.createElement('div');
demod_space.style.width = '4px';
demod_space.style.display = 'inline-block';
demod_space.style.verticalAlign = 'top';
demod_div.appendChild(demod_space);
// Adding the last message status indicator
demod_status = document.createElement('div');
demod_status.style.color = 'white';
demod_status.style.height = '6px';
demod_status.style.border = '0px';
demod_status.style.padding = '0px';
demod_status.style.width = '124px';
demod_status.style.fontSize = '0px';
demod_status.style.border = 'none';
demod_status.style.outline = 'none';
demod_status.style.display = 'inline-block';
demod_status.style.verticalAlign = 'top';
demod_status.style.textAlign = 'center';
demod_status.style.backgroundColor = '#9A9A9A';
demod_status.textContent = "Latest: None";
demod_div.appendChild(demod_status);
demod_div.onmouseover = function () {
is_over = true;
demod_status.style.fontSize = '10px';
demod_status.style.height = '32px';
demod_status.style.padding = '7px 3px';
updateDeModState();
};
demod_div.onmouseout = function () {
is_over = false;
demod_status.style.fontSize = '0px';
demod_status.style.height = '6px';
demod_status.style.padding = '0px';
updateDeModState();
};
demod_button.addEventListener('click', () => {
is_on = !is_on;
setDeModState(is_on);
updateDeModState();
});
document.body.appendChild(demod_div);
is_on = getDeModState();
updateDeModState();
console.log("[DEMOD] DeMod UI attached.");
};
if (document.readyState === 'loading') {
target_window.addEventListener("DOMContentLoaded", demod_init);
} else {
demod_init();
}
const observer = new MutationObserver(demod_init);
observer.observe(document.documentElement || document.body, {
childList: true,
subtree: true
});
window.addEventListener('popstate', demod_init);
})();