MCPcopy Create free account
hub / github.com/4as/Chat-DeMod / intercepter_fetch

Function intercepter_fetch

Chat DeMod.user.js:573–732  ·  view source on GitHub ↗
(target, this_arg, args)

Source from the content-addressed store, hash-verified

571
572 // Intercepter for old fetch() based communication
573 const intercepter_fetch = async function (target, this_arg, args) {
574 if (!is_on) {
575 return target.apply(this_arg, args);
576 }
577
578 var original_arg = args;
579 var fetch_url = args[0];
580 var is_request = false;
581 if (typeof(fetch_url) !== 'string') {
582 if( fetch_url.url !== null ) fetch_url = fetch_url.url;
583 else if( fetch_url.href !== null ) fetch_url = fetch_url.href;
584 else return target.apply(this_arg, args);
585 is_request = true;
586 }
587
588 if (fetch_url.indexOf('/share/create') != -1) {
589 console.log("[DEMOD] Share request detected, blocking.");
590 return new Response("", {
591 status: 404,
592 statusText: "Not found"
593 });
594 }
595
596 var is_conversation = fetch_url.indexOf('/complete') !== -1 || (fetch_url.indexOf('/conversation') !== -1 && fetch_url.indexOf('/conversations') === -1);
597 var convo_type = ConversationType.UNKNOWN;
598 if (is_conversation) {
599 if (fetch_url.indexOf("/gen_title") != -1) {
600 var init_url = fetch_url.replace("/gen_title", "");
601 if (is_request) {
602 console.log("[DEMOD] Generating title (Request).");
603 args = cloneRequest(args[0], init_url, "GET", null);
604 args.headers.delete("Content-Type");
605 }
606 else {
607 console.log("[DEMOD] Generating title (basic).");
608 args = JSON.parse(JSON.stringify(args));
609 args[0] = init_url;
610 args[1].method = "GET";
611 delete args[1].headers["Content-Type"];
612 delete args[1].body;
613 }
614 }
615
616 var conv_request = null;
617 if (is_request) {
618 if (args[0] !== undefined && args[0].hasOwnProperty('text') && (typeof args[0].text === 'function')) {
619 conv_request = await args[0].text();
620 }
621 }
622 else {
623 if (args[1] !== undefined && args[1].hasOwnProperty('body')) {
624 conv_request = args[1].body;
625 }
626 }
627
628 if (conv_request) {
629 convo_type = ConversationType.PROMPT;
630 var conv_body = JSON.parse(conv_request);

Callers

nothing calls this directly

Calls 3

cloneRequestFunction · 0.85
updateDeModMessageStateFunction · 0.85
clearFlaggingFunction · 0.85

Tested by

no test coverage detected