link-interceptor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +51 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.mts +32 -0
- package/dist/index.mjs +50 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 babu-ch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/interceptor.ts
|
|
3
|
+
/**
|
|
4
|
+
* Intercept all `<a>` tag clicks on the document.
|
|
5
|
+
* Returns a cleanup function that removes the listener.
|
|
6
|
+
*/
|
|
7
|
+
function interceptLinks(options) {
|
|
8
|
+
const handler = createClickHandler(options);
|
|
9
|
+
document.addEventListener("click", handler, true);
|
|
10
|
+
return () => document.removeEventListener("click", handler, true);
|
|
11
|
+
}
|
|
12
|
+
function findAnchorFromEvent(event) {
|
|
13
|
+
for (const el of event.composedPath()) if (el instanceof HTMLAnchorElement) return el;
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
function createClickHandler(options) {
|
|
17
|
+
return (event) => {
|
|
18
|
+
if (event.button !== 0) return;
|
|
19
|
+
const isModifierClick = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
|
|
20
|
+
const anchor = findAnchorFromEvent(event);
|
|
21
|
+
if (!anchor) return;
|
|
22
|
+
const href = anchor.href;
|
|
23
|
+
if (!href) return;
|
|
24
|
+
let url;
|
|
25
|
+
try {
|
|
26
|
+
url = new URL(href);
|
|
27
|
+
} catch {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return;
|
|
31
|
+
const isExternal = url.origin !== window.location.origin;
|
|
32
|
+
const ctx = {
|
|
33
|
+
url,
|
|
34
|
+
anchor,
|
|
35
|
+
event,
|
|
36
|
+
get path() {
|
|
37
|
+
return this.url.pathname + this.url.search + this.url.hash;
|
|
38
|
+
},
|
|
39
|
+
isExternal,
|
|
40
|
+
isModifierClick,
|
|
41
|
+
preventDefault() {
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
if (isExternal) options.onExternalLink?.(ctx);
|
|
46
|
+
else options.onInternalLink?.(ctx);
|
|
47
|
+
if (anchor.href !== url.toString()) anchor.href = url.toString();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.interceptLinks = interceptLinks;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface LinkContext {
|
|
3
|
+
/** Parsed URL (mutable — changes are reflected on `anchor.href`) */
|
|
4
|
+
url: URL;
|
|
5
|
+
/** The clicked `<a>` element */
|
|
6
|
+
anchor: HTMLAnchorElement;
|
|
7
|
+
/** The original click event */
|
|
8
|
+
event: MouseEvent;
|
|
9
|
+
/** `url.pathname + url.search + url.hash` */
|
|
10
|
+
path: string;
|
|
11
|
+
/** Whether the link is external (different origin) */
|
|
12
|
+
isExternal: boolean;
|
|
13
|
+
/** Whether a modifier key (Ctrl/Meta/Shift/Alt) was held during the click */
|
|
14
|
+
isModifierClick: boolean;
|
|
15
|
+
/** Cancel the default navigation */
|
|
16
|
+
preventDefault(): void;
|
|
17
|
+
}
|
|
18
|
+
interface LinkInterceptorOptions {
|
|
19
|
+
/** Called when an internal (same-origin) link is clicked */
|
|
20
|
+
onInternalLink?: (ctx: LinkContext) => void;
|
|
21
|
+
/** Called when an external (different-origin) link is clicked */
|
|
22
|
+
onExternalLink?: (ctx: LinkContext) => void;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/interceptor.d.ts
|
|
26
|
+
/**
|
|
27
|
+
* Intercept all `<a>` tag clicks on the document.
|
|
28
|
+
* Returns a cleanup function that removes the listener.
|
|
29
|
+
*/
|
|
30
|
+
declare function interceptLinks(options: LinkInterceptorOptions): () => void;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { type LinkContext, type LinkInterceptorOptions, interceptLinks };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface LinkContext {
|
|
3
|
+
/** Parsed URL (mutable — changes are reflected on `anchor.href`) */
|
|
4
|
+
url: URL;
|
|
5
|
+
/** The clicked `<a>` element */
|
|
6
|
+
anchor: HTMLAnchorElement;
|
|
7
|
+
/** The original click event */
|
|
8
|
+
event: MouseEvent;
|
|
9
|
+
/** `url.pathname + url.search + url.hash` */
|
|
10
|
+
path: string;
|
|
11
|
+
/** Whether the link is external (different origin) */
|
|
12
|
+
isExternal: boolean;
|
|
13
|
+
/** Whether a modifier key (Ctrl/Meta/Shift/Alt) was held during the click */
|
|
14
|
+
isModifierClick: boolean;
|
|
15
|
+
/** Cancel the default navigation */
|
|
16
|
+
preventDefault(): void;
|
|
17
|
+
}
|
|
18
|
+
interface LinkInterceptorOptions {
|
|
19
|
+
/** Called when an internal (same-origin) link is clicked */
|
|
20
|
+
onInternalLink?: (ctx: LinkContext) => void;
|
|
21
|
+
/** Called when an external (different-origin) link is clicked */
|
|
22
|
+
onExternalLink?: (ctx: LinkContext) => void;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/interceptor.d.ts
|
|
26
|
+
/**
|
|
27
|
+
* Intercept all `<a>` tag clicks on the document.
|
|
28
|
+
* Returns a cleanup function that removes the listener.
|
|
29
|
+
*/
|
|
30
|
+
declare function interceptLinks(options: LinkInterceptorOptions): () => void;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { type LinkContext, type LinkInterceptorOptions, interceptLinks };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region src/interceptor.ts
|
|
2
|
+
/**
|
|
3
|
+
* Intercept all `<a>` tag clicks on the document.
|
|
4
|
+
* Returns a cleanup function that removes the listener.
|
|
5
|
+
*/
|
|
6
|
+
function interceptLinks(options) {
|
|
7
|
+
const handler = createClickHandler(options);
|
|
8
|
+
document.addEventListener("click", handler, true);
|
|
9
|
+
return () => document.removeEventListener("click", handler, true);
|
|
10
|
+
}
|
|
11
|
+
function findAnchorFromEvent(event) {
|
|
12
|
+
for (const el of event.composedPath()) if (el instanceof HTMLAnchorElement) return el;
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
function createClickHandler(options) {
|
|
16
|
+
return (event) => {
|
|
17
|
+
if (event.button !== 0) return;
|
|
18
|
+
const isModifierClick = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
|
|
19
|
+
const anchor = findAnchorFromEvent(event);
|
|
20
|
+
if (!anchor) return;
|
|
21
|
+
const href = anchor.href;
|
|
22
|
+
if (!href) return;
|
|
23
|
+
let url;
|
|
24
|
+
try {
|
|
25
|
+
url = new URL(href);
|
|
26
|
+
} catch {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return;
|
|
30
|
+
const isExternal = url.origin !== window.location.origin;
|
|
31
|
+
const ctx = {
|
|
32
|
+
url,
|
|
33
|
+
anchor,
|
|
34
|
+
event,
|
|
35
|
+
get path() {
|
|
36
|
+
return this.url.pathname + this.url.search + this.url.hash;
|
|
37
|
+
},
|
|
38
|
+
isExternal,
|
|
39
|
+
isModifierClick,
|
|
40
|
+
preventDefault() {
|
|
41
|
+
event.preventDefault();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
if (isExternal) options.onExternalLink?.(ctx);
|
|
45
|
+
else options.onInternalLink?.(ctx);
|
|
46
|
+
if (anchor.href !== url.toString()) anchor.href = url.toString();
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { interceptLinks };
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "link-interceptor",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Intercept all <a> tag clicks in your SPA — framework-agnostic core for handling internal and external link navigation.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"anchor",
|
|
7
|
+
"interceptor",
|
|
8
|
+
"link",
|
|
9
|
+
"router",
|
|
10
|
+
"spa",
|
|
11
|
+
"click",
|
|
12
|
+
"capture"
|
|
13
|
+
],
|
|
14
|
+
"author": "babu-ch",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://github.com/babu-ch/link-interceptor/tree/main/packages/core",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/babu-ch/link-interceptor.git",
|
|
20
|
+
"directory": "packages/core"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/babu-ch/link-interceptor/issues"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18.0.0"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"main": "./dist/index.cjs",
|
|
34
|
+
"module": "./dist/index.mjs",
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./dist/index.d.mts",
|
|
40
|
+
"default": "./dist/index.mjs"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./dist/index.d.cts",
|
|
44
|
+
"default": "./dist/index.cjs"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"jsdom": "^29.0.1",
|
|
50
|
+
"typescript": "^5.8.2",
|
|
51
|
+
"vite-plus": "^0.1.14"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "vp pack",
|
|
55
|
+
"test": "vp test run",
|
|
56
|
+
"lint": "vp lint"
|
|
57
|
+
}
|
|
58
|
+
}
|