github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/userscripts/bindings/ffi_bindings.ts (about)

     1  import { importModule, Application, heap, Pointer } from "@ffi";
     2  
     3  importModule("plat/js/webext/userscripts", (A: Application) => {
     4    const WEBEXT = typeof globalThis.browser === "undefined" ? globalThis.chrome : globalThis.browser;
     5  
     6    return {
     7      "store_ScriptSource": (ptr: Pointer, ref: heap.Ref<any>) => {
     8        const x = A.H.get<any>(ref);
     9  
    10        if (typeof x === "undefined") {
    11          A.store.Bool(ptr + 8, false);
    12          A.store.Ref(ptr + 0, undefined);
    13          A.store.Ref(ptr + 4, undefined);
    14        } else {
    15          A.store.Bool(ptr + 8, true);
    16          A.store.Ref(ptr + 0, x["code"]);
    17          A.store.Ref(ptr + 4, x["file"]);
    18        }
    19      },
    20      "load_ScriptSource": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    21        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    22  
    23        x["code"] = A.load.Ref(ptr + 0, undefined);
    24        x["file"] = A.load.Ref(ptr + 4, undefined);
    25        return create === A.H.TRUE ? A.H.push(x) : ref;
    26      },
    27  
    28      "store_RegisteredUserScript": (ptr: Pointer, ref: heap.Ref<any>) => {
    29        const x = A.H.get<any>(ref);
    30  
    31        if (typeof x === "undefined") {
    32          A.store.Bool(ptr + 25, false);
    33          A.store.Bool(ptr + 24, false);
    34          A.store.Bool(ptr + 0, false);
    35          A.store.Ref(ptr + 4, undefined);
    36          A.store.Ref(ptr + 8, undefined);
    37          A.store.Ref(ptr + 12, undefined);
    38          A.store.Ref(ptr + 16, undefined);
    39          A.store.Enum(ptr + 20, -1);
    40        } else {
    41          A.store.Bool(ptr + 25, true);
    42          A.store.Bool(ptr + 24, "allFrames" in x ? true : false);
    43          A.store.Bool(ptr + 0, x["allFrames"] ? true : false);
    44          A.store.Ref(ptr + 4, x["excludeMatches"]);
    45          A.store.Ref(ptr + 8, x["id"]);
    46          A.store.Ref(ptr + 12, x["js"]);
    47          A.store.Ref(ptr + 16, x["matches"]);
    48          A.store.Enum(ptr + 20, ["document_start", "document_end", "document_idle"].indexOf(x["runAt"] as string));
    49        }
    50      },
    51      "load_RegisteredUserScript": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    52        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    53  
    54        if (A.load.Bool(ptr + 24)) {
    55          x["allFrames"] = A.load.Bool(ptr + 0);
    56        } else {
    57          delete x["allFrames"];
    58        }
    59        x["excludeMatches"] = A.load.Ref(ptr + 4, undefined);
    60        x["id"] = A.load.Ref(ptr + 8, undefined);
    61        x["js"] = A.load.Ref(ptr + 12, undefined);
    62        x["matches"] = A.load.Ref(ptr + 16, undefined);
    63        x["runAt"] = A.load.Enum(ptr + 20, ["document_start", "document_end", "document_idle"]);
    64        return create === A.H.TRUE ? A.H.push(x) : ref;
    65      },
    66  
    67      "store_UserScriptFilter": (ptr: Pointer, ref: heap.Ref<any>) => {
    68        const x = A.H.get<any>(ref);
    69  
    70        if (typeof x === "undefined") {
    71          A.store.Bool(ptr + 4, false);
    72          A.store.Ref(ptr + 0, undefined);
    73        } else {
    74          A.store.Bool(ptr + 4, true);
    75          A.store.Ref(ptr + 0, x["ids"]);
    76        }
    77      },
    78      "load_UserScriptFilter": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    79        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    80  
    81        x["ids"] = A.load.Ref(ptr + 0, undefined);
    82        return create === A.H.TRUE ? A.H.push(x) : ref;
    83      },
    84      "has_GetScripts": (): heap.Ref<boolean> => {
    85        if (WEBEXT?.userScripts && "getScripts" in WEBEXT?.userScripts) {
    86          return A.H.TRUE;
    87        }
    88        return A.H.FALSE;
    89      },
    90      "func_GetScripts": (fn: Pointer): void => {
    91        A.store.Ref(fn, WEBEXT.userScripts.getScripts);
    92      },
    93      "call_GetScripts": (retPtr: Pointer, filter: Pointer): void => {
    94        const filter_ffi = {};
    95  
    96        filter_ffi["ids"] = A.load.Ref(filter + 0, undefined);
    97  
    98        const _ret = WEBEXT.userScripts.getScripts(filter_ffi);
    99        A.store.Ref(retPtr, _ret);
   100      },
   101      "try_GetScripts": (retPtr: Pointer, errPtr: Pointer, filter: Pointer): heap.Ref<boolean> => {
   102        try {
   103          const filter_ffi = {};
   104  
   105          filter_ffi["ids"] = A.load.Ref(filter + 0, undefined);
   106  
   107          const _ret = WEBEXT.userScripts.getScripts(filter_ffi);
   108          A.store.Ref(retPtr, _ret);
   109          return A.H.TRUE;
   110        } catch (err: any) {
   111          A.store.Ref(errPtr, err);
   112          return A.H.FALSE;
   113        }
   114      },
   115      "has_Register": (): heap.Ref<boolean> => {
   116        if (WEBEXT?.userScripts && "register" in WEBEXT?.userScripts) {
   117          return A.H.TRUE;
   118        }
   119        return A.H.FALSE;
   120      },
   121      "func_Register": (fn: Pointer): void => {
   122        A.store.Ref(fn, WEBEXT.userScripts.register);
   123      },
   124      "call_Register": (retPtr: Pointer, scripts: heap.Ref<object>): void => {
   125        const _ret = WEBEXT.userScripts.register(A.H.get<object>(scripts));
   126        A.store.Ref(retPtr, _ret);
   127      },
   128      "try_Register": (retPtr: Pointer, errPtr: Pointer, scripts: heap.Ref<object>): heap.Ref<boolean> => {
   129        try {
   130          const _ret = WEBEXT.userScripts.register(A.H.get<object>(scripts));
   131          A.store.Ref(retPtr, _ret);
   132          return A.H.TRUE;
   133        } catch (err: any) {
   134          A.store.Ref(errPtr, err);
   135          return A.H.FALSE;
   136        }
   137      },
   138      "has_Unregister": (): heap.Ref<boolean> => {
   139        if (WEBEXT?.userScripts && "unregister" in WEBEXT?.userScripts) {
   140          return A.H.TRUE;
   141        }
   142        return A.H.FALSE;
   143      },
   144      "func_Unregister": (fn: Pointer): void => {
   145        A.store.Ref(fn, WEBEXT.userScripts.unregister);
   146      },
   147      "call_Unregister": (retPtr: Pointer, filter: Pointer): void => {
   148        const filter_ffi = {};
   149  
   150        filter_ffi["ids"] = A.load.Ref(filter + 0, undefined);
   151  
   152        const _ret = WEBEXT.userScripts.unregister(filter_ffi);
   153        A.store.Ref(retPtr, _ret);
   154      },
   155      "try_Unregister": (retPtr: Pointer, errPtr: Pointer, filter: Pointer): heap.Ref<boolean> => {
   156        try {
   157          const filter_ffi = {};
   158  
   159          filter_ffi["ids"] = A.load.Ref(filter + 0, undefined);
   160  
   161          const _ret = WEBEXT.userScripts.unregister(filter_ffi);
   162          A.store.Ref(retPtr, _ret);
   163          return A.H.TRUE;
   164        } catch (err: any) {
   165          A.store.Ref(errPtr, err);
   166          return A.H.FALSE;
   167        }
   168      },
   169    };
   170  });