Make the constrast-plugin work correctly on subpath

This commit is contained in:
Andrey Antukh
2026-02-10 18:58:17 +01:00
parent 4a6e6fce5b
commit 1229c2a5e5
8 changed files with 29 additions and 10 deletions

View File

@@ -12,7 +12,10 @@
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/apps/contrast-plugin",
"outputPath": {
"base": "dist/apps/contrast-plugin",
"browser": ""
},
"index": "apps/contrast-plugin/src/index.html",
"browser": "apps/contrast-plugin/src/main.ts",
"polyfills": ["zone.js"],
@@ -20,6 +23,7 @@
"assets": [
"apps/contrast-plugin/src/_headers",
"apps/contrast-plugin/src/favicon.ico",
"apps/contrast-plugin/src/manifest.json",
"apps/contrast-plugin/src/assets"
],
"styles": [

View File

@@ -7,9 +7,9 @@
"build": "ng build contrast-plugin && pnpm run build:plugin",
"build:dev": "ng build contrast-plugin --configuration development",
"build:plugin": "node ../../tools/scripts/build-plugin.mjs --plugin=contrast-plugin",
"build:plugin:watch": "node ../../tools/scripts/build-plugin.mjs --plugin=contrast-plugin --watch",
"watch": "node ../../tools/scripts/build-plugin.mjs --plugin=contrast-plugin --watch",
"serve": "ng serve contrast-plugin",
"init": "concurrently --kill-others --names plugin,serve \"pnpm run build:plugin:watch\" \"pnpm run serve\"",
"init": "concurrently --kill-others --names plugin,serve \"pnpm run watch\" \"pnpm run serve\"",
"lint": "eslint .",
"test": "vitest"
}

View File

@@ -1,6 +1,6 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideRouter, withHashLocation } from '@angular/router';
export const appConfig: ApplicationConfig = {
providers: [provideRouter([])],
providers: [provideRouter([], withHashLocation())],
};

View File

@@ -3,7 +3,6 @@
<head>
<meta charset="utf-8" />
<title>contrast-plugin</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>

View File

@@ -0,0 +1,8 @@
{
"name": "Contrast",
"description": "Measure contrast plugin",
"version": 2,
"code": "assets/plugin.js",
"icon": "assets/icon.png",
"permissions": ["content:read"]
}

View File

@@ -5,7 +5,11 @@ export function getValidUrl(host: string, path: string): URL {
return new URL(path, host);
}
export function prepareUrl(manifest: Manifest, url: string, params: object): string {
export function prepareUrl(
manifest: Manifest,
url: string,
params: object,
): string {
const result = getValidUrl(manifest.host, url);
for (const [k, v] of Object.entries(params)) {
if (!result.searchParams.has(k)) {

View File

@@ -72,7 +72,9 @@ describe('createPluginManager', () => {
vi.mocked(loadManifestCode).mockResolvedValue(
'console.log("Plugin loaded");',
);
vi.mocked(getValidUrl).mockReturnValue(new URL('https://example.com/plugin'));
vi.mocked(getValidUrl).mockReturnValue(
new URL('https://example.com/plugin'),
);
vi.mocked(prepareUrl).mockReturnValue('https://example.com/plugin');
});
@@ -112,7 +114,9 @@ describe('createPluginManager', () => {
height: 300,
});
expect(prepareUrl).toHaveBeenCalledWith(manifest, '/test-url', {theme: 'light'});
expect(prepareUrl).toHaveBeenCalledWith(manifest, '/test-url', {
theme: 'light',
});
expect(openUIApi).toHaveBeenCalledWith(
'Test Modal',
'https://example.com/plugin',

View File

@@ -81,7 +81,7 @@ export async function createPluginManager(
const openModal = (name: string, url: string, options?: OpenUIOptions) => {
const theme = context.theme as Theme;
const modalUrl = prepareUrl(manifest, url, {theme});
const modalUrl = prepareUrl(manifest, url, { theme });
if (modal?.getAttribute('iframe-src') === modalUrl) {
return;