From 966100d606c2d4007d66d328cae06d4ac5130446 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sun, 6 Apr 2025 15:13:31 +0400 Subject: [PATCH] Added AMD loader initialization as separate entry, autoload everything --- manifest.json | 8 ++++++++ src/content/deps/amd.ts | 22 ++++++++++++++++++++++ src/types/amd-lite.d.ts | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/content/deps/amd.ts create mode 100644 src/types/amd-lite.d.ts diff --git a/manifest.json b/manifest.json index 59bfd2d..34d55d7 100644 --- a/manifest.json +++ b/manifest.json @@ -18,6 +18,14 @@ "*://*.furbooru.org/" ], "content_scripts": [ + { + "matches": [ + "*://*.furbooru.org/*" + ], + "js": [ + "src/content/deps/amd.ts" + ] + }, { "matches": [ "*://*.furbooru.org/", diff --git a/src/content/deps/amd.ts b/src/content/deps/amd.ts new file mode 100644 index 0000000..9f4f525 --- /dev/null +++ b/src/content/deps/amd.ts @@ -0,0 +1,22 @@ +import { amdLite } from "amd-lite"; + +const originalDefine = amdLite.define; + +amdLite.define = (name, dependencies, originalCallback) => { + return originalDefine(name, dependencies, function () { + const callbackResult = originalCallback(...arguments); + + // Workaround for the entry script not returning anything causing AMD-Lite to send warning about modules not + // being loaded/not existing. + return typeof callbackResult !== 'undefined' ? callbackResult : {}; + }) +} + +amdLite.init({ + publicScope: window +}); + +// We don't have anything asynchronous, so it's safe to execute everything on the next frame. +requestAnimationFrame(() => { + amdLite.resolveDependencies(Object.keys(amdLite.waitingModules)) +}); diff --git a/src/types/amd-lite.d.ts b/src/types/amd-lite.d.ts new file mode 100644 index 0000000..9549e1e --- /dev/null +++ b/src/types/amd-lite.d.ts @@ -0,0 +1,23 @@ +// Types for the small untyped AMD loader. These types do not cover all the functions available in the package, only +// parts required for content scripts in extension to work. +declare module 'amd-lite' { + interface AMDLiteInitOptions { + publicScope: any; + verbosity: number; + } + + interface AMDLite { + waitingModules: Record; + readyModules: Record; + + init(options: Partial): void; + + define(name: string, dependencies: string[], callback: function): void; + + resolveDependency(dependencyPath: string); + + resolveDependencies(dependencyNames: string[], from?: string); + } + + export const amdLite: AMDLite; +}