favicon-stealer 1.9.0 → 2.0.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.
@@ -1,4 +1,5 @@
1
- import React from "react";
1
+ import React from 'react';
2
+
2
3
  interface IProps {
3
4
  url: string;
4
5
  src?: string;
@@ -15,4 +16,5 @@ interface IProps {
15
16
  preferSrc?: boolean;
16
17
  }
17
18
  declare const Favicon: ({ url, src, alt, size, className, timeout, border, padding, background, borderRadius, lazy, preferFallback, preferSrc, }: IProps) => React.JSX.Element;
18
- export default Favicon;
19
+
20
+ export { Favicon };
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+
3
+ interface IProps {
4
+ url: string;
5
+ src?: string;
6
+ alt?: string;
7
+ size?: number;
8
+ className?: string;
9
+ timeout?: number;
10
+ border?: boolean;
11
+ padding?: number;
12
+ background?: string;
13
+ borderRadius?: number;
14
+ lazy?: boolean;
15
+ preferFallback?: boolean;
16
+ preferSrc?: boolean;
17
+ }
18
+ declare const Favicon: ({ url, src, alt, size, className, timeout, border, padding, background, borderRadius, lazy, preferFallback, preferSrc, }: IProps) => React.JSX.Element;
19
+
20
+ export { Favicon };
package/dist/index.js CHANGED
@@ -1,8 +1,181 @@
1
+ "use client";
1
2
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
4
12
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Favicon = void 0;
7
- var Favicon_1 = require("./Favicon");
8
- Object.defineProperty(exports, "Favicon", { enumerable: true, get: function () { return __importDefault(Favicon_1).default; } });
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/index.tsx
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ Favicon: () => Favicon_default
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/lib/utils/index.ts
39
+ var getDomain = (url) => {
40
+ try {
41
+ const urlWithProtocol = url.startsWith("http") ? url : `https://${url}`;
42
+ const domain = new URL(urlWithProtocol).hostname;
43
+ return domain.replace(/^www\./, "");
44
+ } catch (error) {
45
+ return url;
46
+ }
47
+ };
48
+
49
+ // src/Favicon.tsx
50
+ var import_react = __toESM(require("react"));
51
+ var Favicon = ({
52
+ url,
53
+ src,
54
+ alt,
55
+ size = 32,
56
+ className = "",
57
+ timeout = 3e3,
58
+ // 增加到3秒,给网站自己的favicon更多加载时间
59
+ border = false,
60
+ padding = 0,
61
+ background = "transparent",
62
+ borderRadius = 0,
63
+ lazy = false,
64
+ preferFallback = false,
65
+ preferSrc = true
66
+ }) => {
67
+ const domain = getDomain(url);
68
+ const [imgSrc, setImgSrc] = (0, import_react.useState)("");
69
+ const [fallbackIndex, setFallbackIndex] = (0, import_react.useState)(0);
70
+ const [isLoading, setIsLoading] = (0, import_react.useState)(true);
71
+ const [hasError, setHasError] = (0, import_react.useState)(false);
72
+ const [isInitialized, setIsInitialized] = (0, import_react.useState)(false);
73
+ const standardSources = [
74
+ `https://${domain}/favicon.ico`,
75
+ `https://${domain}/logo.svg`,
76
+ `https://${domain}/logo.png`,
77
+ `https://${domain}/apple-touch-icon.png`,
78
+ `https://${domain}/apple-touch-icon-precomposed.png`,
79
+ `https://${domain}/static/img/favicon.ico`,
80
+ `https://${domain}/static/img/favicon.png`,
81
+ `https://${domain}/img/favicon.png`,
82
+ `https://${domain}/img/favicon.ico`,
83
+ `https://${domain}/static/img/logo.svg`,
84
+ `https://${domain}/apple-touch-icon-precomposed.png`
85
+ ];
86
+ const fallbackServices = [
87
+ `https://favicon.im/${domain}?larger=true`,
88
+ `https://favicon.im/${domain}`,
89
+ `https://www.google.com/s2/favicons?domain=https://${domain}&sz=64`,
90
+ `https://www.google.com/s2/favicons?domain=http://${domain}&sz=64`
91
+ ];
92
+ const fallbackSources = preferFallback ? [...fallbackServices, ...standardSources] : [...standardSources, ...fallbackServices];
93
+ (0, import_react.useEffect)(() => {
94
+ if (!isInitialized) {
95
+ if (src) {
96
+ setIsLoading(false);
97
+ } else {
98
+ setImgSrc(fallbackSources[0]);
99
+ }
100
+ setIsInitialized(true);
101
+ }
102
+ }, [isInitialized, fallbackSources, src]);
103
+ (0, import_react.useEffect)(() => {
104
+ let timeoutId;
105
+ if (isLoading && imgSrc && !src) {
106
+ timeoutId = setTimeout(() => {
107
+ handleError();
108
+ }, timeout);
109
+ }
110
+ return () => {
111
+ if (timeoutId) {
112
+ clearTimeout(timeoutId);
113
+ }
114
+ };
115
+ }, [imgSrc, isLoading, timeout, src]);
116
+ const handleError = () => {
117
+ const nextIndex = fallbackIndex + 1;
118
+ if (nextIndex < fallbackSources.length) {
119
+ setFallbackIndex(nextIndex);
120
+ setImgSrc(fallbackSources[nextIndex]);
121
+ setIsLoading(true);
122
+ } else {
123
+ setHasError(true);
124
+ setIsLoading(false);
125
+ }
126
+ };
127
+ const handleLoad = () => {
128
+ setIsLoading(false);
129
+ setHasError(false);
130
+ };
131
+ return /* @__PURE__ */ import_react.default.createElement(
132
+ "div",
133
+ {
134
+ className: `relative inline-block
135
+ ${className}
136
+ ${border ? "border" : ""}
137
+ ${hasError ? "opacity-0" : ""}
138
+ ${padding ? `p-[${padding}px]` : ""}
139
+ ${borderRadius ? `rounded-[${borderRadius}px]` : ""}
140
+ `,
141
+ style: {
142
+ width: size,
143
+ height: size,
144
+ background,
145
+ padding: padding ? `${padding}px` : 0,
146
+ borderRadius: borderRadius ? `${borderRadius}px` : 0
147
+ }
148
+ },
149
+ isLoading && /* @__PURE__ */ import_react.default.createElement("div", { className: "absolute inset-0 animate-pulse" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "w-full h-full rounded-md bg-gray-200/60" })),
150
+ (src || imgSrc) && /* @__PURE__ */ import_react.default.createElement(
151
+ "img",
152
+ {
153
+ src: src || imgSrc,
154
+ alt: alt || `${domain} logo`,
155
+ width: size,
156
+ height: size,
157
+ loading: lazy ? "lazy" : "eager",
158
+ onError: handleError,
159
+ onLoad: handleLoad,
160
+ className: `inline-block transition-opacity duration-300 ${isLoading ? "opacity-0" : "opacity-100"}`,
161
+ style: {
162
+ objectFit: "contain",
163
+ display: hasError ? "none" : "inline-block"
164
+ }
165
+ }
166
+ ),
167
+ hasError && /* @__PURE__ */ import_react.default.createElement(
168
+ "div",
169
+ {
170
+ className: "w-full h-full flex items-center justify-center bg-gray-100 rounded-md",
171
+ style: { fontSize: `${size * 0.5}px` }
172
+ },
173
+ domain.charAt(0).toUpperCase()
174
+ )
175
+ );
176
+ };
177
+ var Favicon_default = Favicon;
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ Favicon
181
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,145 @@
1
+ "use client";
2
+
3
+ // src/lib/utils/index.ts
4
+ var getDomain = (url) => {
5
+ try {
6
+ const urlWithProtocol = url.startsWith("http") ? url : `https://${url}`;
7
+ const domain = new URL(urlWithProtocol).hostname;
8
+ return domain.replace(/^www\./, "");
9
+ } catch (error) {
10
+ return url;
11
+ }
12
+ };
13
+
14
+ // src/Favicon.tsx
15
+ import React, { useEffect, useState } from "react";
16
+ var Favicon = ({
17
+ url,
18
+ src,
19
+ alt,
20
+ size = 32,
21
+ className = "",
22
+ timeout = 3e3,
23
+ // 增加到3秒,给网站自己的favicon更多加载时间
24
+ border = false,
25
+ padding = 0,
26
+ background = "transparent",
27
+ borderRadius = 0,
28
+ lazy = false,
29
+ preferFallback = false,
30
+ preferSrc = true
31
+ }) => {
32
+ const domain = getDomain(url);
33
+ const [imgSrc, setImgSrc] = useState("");
34
+ const [fallbackIndex, setFallbackIndex] = useState(0);
35
+ const [isLoading, setIsLoading] = useState(true);
36
+ const [hasError, setHasError] = useState(false);
37
+ const [isInitialized, setIsInitialized] = useState(false);
38
+ const standardSources = [
39
+ `https://${domain}/favicon.ico`,
40
+ `https://${domain}/logo.svg`,
41
+ `https://${domain}/logo.png`,
42
+ `https://${domain}/apple-touch-icon.png`,
43
+ `https://${domain}/apple-touch-icon-precomposed.png`,
44
+ `https://${domain}/static/img/favicon.ico`,
45
+ `https://${domain}/static/img/favicon.png`,
46
+ `https://${domain}/img/favicon.png`,
47
+ `https://${domain}/img/favicon.ico`,
48
+ `https://${domain}/static/img/logo.svg`,
49
+ `https://${domain}/apple-touch-icon-precomposed.png`
50
+ ];
51
+ const fallbackServices = [
52
+ `https://favicon.im/${domain}?larger=true`,
53
+ `https://favicon.im/${domain}`,
54
+ `https://www.google.com/s2/favicons?domain=https://${domain}&sz=64`,
55
+ `https://www.google.com/s2/favicons?domain=http://${domain}&sz=64`
56
+ ];
57
+ const fallbackSources = preferFallback ? [...fallbackServices, ...standardSources] : [...standardSources, ...fallbackServices];
58
+ useEffect(() => {
59
+ if (!isInitialized) {
60
+ if (src) {
61
+ setIsLoading(false);
62
+ } else {
63
+ setImgSrc(fallbackSources[0]);
64
+ }
65
+ setIsInitialized(true);
66
+ }
67
+ }, [isInitialized, fallbackSources, src]);
68
+ useEffect(() => {
69
+ let timeoutId;
70
+ if (isLoading && imgSrc && !src) {
71
+ timeoutId = setTimeout(() => {
72
+ handleError();
73
+ }, timeout);
74
+ }
75
+ return () => {
76
+ if (timeoutId) {
77
+ clearTimeout(timeoutId);
78
+ }
79
+ };
80
+ }, [imgSrc, isLoading, timeout, src]);
81
+ const handleError = () => {
82
+ const nextIndex = fallbackIndex + 1;
83
+ if (nextIndex < fallbackSources.length) {
84
+ setFallbackIndex(nextIndex);
85
+ setImgSrc(fallbackSources[nextIndex]);
86
+ setIsLoading(true);
87
+ } else {
88
+ setHasError(true);
89
+ setIsLoading(false);
90
+ }
91
+ };
92
+ const handleLoad = () => {
93
+ setIsLoading(false);
94
+ setHasError(false);
95
+ };
96
+ return /* @__PURE__ */ React.createElement(
97
+ "div",
98
+ {
99
+ className: `relative inline-block
100
+ ${className}
101
+ ${border ? "border" : ""}
102
+ ${hasError ? "opacity-0" : ""}
103
+ ${padding ? `p-[${padding}px]` : ""}
104
+ ${borderRadius ? `rounded-[${borderRadius}px]` : ""}
105
+ `,
106
+ style: {
107
+ width: size,
108
+ height: size,
109
+ background,
110
+ padding: padding ? `${padding}px` : 0,
111
+ borderRadius: borderRadius ? `${borderRadius}px` : 0
112
+ }
113
+ },
114
+ isLoading && /* @__PURE__ */ React.createElement("div", { className: "absolute inset-0 animate-pulse" }, /* @__PURE__ */ React.createElement("div", { className: "w-full h-full rounded-md bg-gray-200/60" })),
115
+ (src || imgSrc) && /* @__PURE__ */ React.createElement(
116
+ "img",
117
+ {
118
+ src: src || imgSrc,
119
+ alt: alt || `${domain} logo`,
120
+ width: size,
121
+ height: size,
122
+ loading: lazy ? "lazy" : "eager",
123
+ onError: handleError,
124
+ onLoad: handleLoad,
125
+ className: `inline-block transition-opacity duration-300 ${isLoading ? "opacity-0" : "opacity-100"}`,
126
+ style: {
127
+ objectFit: "contain",
128
+ display: hasError ? "none" : "inline-block"
129
+ }
130
+ }
131
+ ),
132
+ hasError && /* @__PURE__ */ React.createElement(
133
+ "div",
134
+ {
135
+ className: "w-full h-full flex items-center justify-center bg-gray-100 rounded-md",
136
+ style: { fontSize: `${size * 0.5}px` }
137
+ },
138
+ domain.charAt(0).toUpperCase()
139
+ )
140
+ );
141
+ };
142
+ var Favicon_default = Favicon;
143
+ export {
144
+ Favicon_default as Favicon
145
+ };
package/package.json CHANGED
@@ -1,8 +1,17 @@
1
1
  {
2
2
  "name": "favicon-stealer",
3
- "version": "1.9.0",
3
+ "version": "2.0.0",
4
4
  "main": "dist/index.js",
5
- "types": "dist/types/index.d.ts",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ }
13
+ },
14
+ "sideEffects": false,
6
15
  "files": [
7
16
  "dist",
8
17
  "LICENSE",
@@ -10,7 +19,7 @@
10
19
  ],
11
20
  "scripts": {
12
21
  "test": "echo \"Error: no test specified\" && exit 1",
13
- "build": "tsc",
22
+ "build": "tsup",
14
23
  "prepublishOnly": "npm run build"
15
24
  },
16
25
  "keywords": [
@@ -31,6 +40,7 @@
31
40
  "devDependencies": {
32
41
  "@types/react": "^19.0.8",
33
42
  "@types/react-dom": "^19.0.3",
34
- "typescript": "^5.7.3"
43
+ "typescript": "^5.7.3",
44
+ "tsup": "^8.3.5"
35
45
  }
36
46
  }
package/dist/Favicon.js DELETED
@@ -1,130 +0,0 @@
1
- "use strict";
2
- "use client";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || (function () {
20
- var ownKeys = function(o) {
21
- ownKeys = Object.getOwnPropertyNames || function (o) {
22
- var ar = [];
23
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
- return ar;
25
- };
26
- return ownKeys(o);
27
- };
28
- return function (mod) {
29
- if (mod && mod.__esModule) return mod;
30
- var result = {};
31
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
- __setModuleDefault(result, mod);
33
- return result;
34
- };
35
- })();
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- const utils_1 = require("./lib/utils");
38
- const react_1 = __importStar(require("react"));
39
- const Favicon = ({ url, src, alt, size = 32, className = "", timeout = 3000, // 增加到3秒,给网站自己的favicon更多加载时间
40
- border = false, padding = 0, background = "transparent", borderRadius = 0, lazy = false, preferFallback = false, preferSrc = true, }) => {
41
- const domain = (0, utils_1.getDomain)(url);
42
- const [imgSrc, setImgSrc] = (0, react_1.useState)("");
43
- const [fallbackIndex, setFallbackIndex] = (0, react_1.useState)(0);
44
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
45
- const [hasError, setHasError] = (0, react_1.useState)(false);
46
- const [isInitialized, setIsInitialized] = (0, react_1.useState)(false);
47
- const standardSources = [
48
- `https://${domain}/favicon.ico`,
49
- `https://${domain}/logo.svg`,
50
- `https://${domain}/logo.png`,
51
- `https://${domain}/apple-touch-icon.png`,
52
- `https://${domain}/apple-touch-icon-precomposed.png`,
53
- `https://${domain}/static/img/favicon.ico`,
54
- `https://${domain}/static/img/favicon.png`,
55
- `https://${domain}/img/favicon.png`,
56
- `https://${domain}/img/favicon.ico`,
57
- `https://${domain}/static/img/logo.svg`,
58
- `https://${domain}/apple-touch-icon-precomposed.png`,
59
- ];
60
- const fallbackServices = [
61
- `https://favicon.im/${domain}?larger=true`,
62
- `https://favicon.im/${domain}`,
63
- `https://www.google.com/s2/favicons?domain=https://${domain}&sz=64`,
64
- `https://www.google.com/s2/favicons?domain=http://${domain}&sz=64`,
65
- ];
66
- const fallbackSources = preferFallback
67
- ? [...fallbackServices, ...standardSources]
68
- : [...standardSources, ...fallbackServices];
69
- (0, react_1.useEffect)(() => {
70
- if (!isInitialized) {
71
- if (src) {
72
- setIsLoading(false);
73
- }
74
- else {
75
- setImgSrc(fallbackSources[0]);
76
- }
77
- setIsInitialized(true);
78
- }
79
- }, [isInitialized, fallbackSources, src]);
80
- (0, react_1.useEffect)(() => {
81
- let timeoutId;
82
- if (isLoading && imgSrc && !src) {
83
- timeoutId = setTimeout(() => {
84
- handleError();
85
- }, timeout);
86
- }
87
- return () => {
88
- if (timeoutId) {
89
- clearTimeout(timeoutId);
90
- }
91
- };
92
- }, [imgSrc, isLoading, timeout, src]);
93
- const handleError = () => {
94
- const nextIndex = fallbackIndex + 1;
95
- if (nextIndex < fallbackSources.length) {
96
- setFallbackIndex(nextIndex);
97
- setImgSrc(fallbackSources[nextIndex]);
98
- setIsLoading(true);
99
- }
100
- else {
101
- setHasError(true);
102
- setIsLoading(false);
103
- }
104
- };
105
- const handleLoad = () => {
106
- setIsLoading(false);
107
- setHasError(false);
108
- };
109
- return (react_1.default.createElement("div", { className: `relative inline-block
110
- ${className}
111
- ${border ? "border" : ""}
112
- ${hasError ? "opacity-0" : ""}
113
- ${padding ? `p-[${padding}px]` : ""}
114
- ${borderRadius ? `rounded-[${borderRadius}px]` : ""}
115
- `, style: {
116
- width: size,
117
- height: size,
118
- background: background,
119
- padding: padding ? `${padding}px` : 0,
120
- borderRadius: borderRadius ? `${borderRadius}px` : 0,
121
- } },
122
- isLoading && (react_1.default.createElement("div", { className: "absolute inset-0 animate-pulse" },
123
- react_1.default.createElement("div", { className: "w-full h-full rounded-md bg-gray-200/60" }))),
124
- (src || imgSrc) && (react_1.default.createElement("img", { src: src || imgSrc, alt: alt || `${domain} logo`, width: size, height: size, loading: lazy ? "lazy" : "eager", onError: handleError, onLoad: handleLoad, className: `inline-block transition-opacity duration-300 ${isLoading ? "opacity-0" : "opacity-100"}`, style: {
125
- objectFit: "contain",
126
- display: hasError ? "none" : "inline-block",
127
- } })),
128
- hasError && (react_1.default.createElement("div", { className: "w-full h-full flex items-center justify-center bg-gray-100 rounded-md", style: { fontSize: `${size * 0.5}px` } }, domain.charAt(0).toUpperCase()))));
129
- };
130
- exports.default = Favicon;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDomain = void 0;
4
- const getDomain = (url) => {
5
- try {
6
- // Add https:// protocol if not present
7
- const urlWithProtocol = url.startsWith('http') ? url : `https://${url}`;
8
- const domain = new URL(urlWithProtocol).hostname;
9
- // Remove 'www.' prefix if exists
10
- return domain.replace(/^www\./, '');
11
- }
12
- catch (error) {
13
- // Return original input if URL parsing fails
14
- return url;
15
- }
16
- };
17
- exports.getDomain = getDomain;
@@ -1 +0,0 @@
1
- export { default as Favicon } from './Favicon';
@@ -1 +0,0 @@
1
- export declare const getDomain: (url: string) => string;