diageo-age-gate-react 1.0.0 → 1.0.2

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,8 +1,8 @@
1
1
  export interface AgeGateProps {
2
- /** 客户 ID(域名或标识) */
3
- customerId: string;
4
2
  /** 用户哈希 */
5
3
  userHash: string;
4
+ /** 客户 ID(域名或标识) */
5
+ customerId?: string;
6
6
  /** 容器 ID,默认 ageGateWrapper */
7
7
  containerId?: string;
8
8
  /** 是否禁用 AgeGate(优先级高于 URL 参数) */
@@ -0,0 +1 @@
1
+ export declare function useUrlChange(onChange: (url: string) => void): void;
package/dist/index.cjs CHANGED
@@ -24,8 +24,39 @@ __export(index_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(index_exports);
26
26
 
27
- // src/components/AgeGate.tsx
27
+ // src/components/urlChange.tsx
28
28
  var import_react = require("react");
29
+ function useUrlChange(onChange) {
30
+ const urlRef = (0, import_react.useRef)("");
31
+ (0, import_react.useEffect)(() => {
32
+ urlRef.current = window.location.href;
33
+ const check = () => {
34
+ const current = window.location.href;
35
+ if (urlRef.current !== current) {
36
+ urlRef.current = current;
37
+ onChange(current);
38
+ }
39
+ };
40
+ window.addEventListener("popstate", check);
41
+ const push = history.pushState;
42
+ const replace = history.replaceState;
43
+ history.pushState = (...args) => {
44
+ push(...args);
45
+ setTimeout(check, 0);
46
+ };
47
+ history.replaceState = (...args) => {
48
+ replace(...args);
49
+ setTimeout(check, 0);
50
+ };
51
+ return () => {
52
+ window.removeEventListener("popstate", check);
53
+ history.pushState = push;
54
+ history.replaceState = replace;
55
+ };
56
+ }, [onChange]);
57
+ }
58
+
59
+ // src/components/AgeGate.tsx
29
60
  var import_jsx_runtime = require("react/jsx-runtime");
30
61
  function getCookieItem(key) {
31
62
  const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
@@ -49,11 +80,27 @@ function AgeGate({
49
80
  countryCode: propCountryCode,
50
81
  language: propLanguage
51
82
  }) {
52
- (0, import_react.useEffect)(() => {
83
+ useUrlChange((url) => {
84
+ console.log("\u5F53\u524DURL:", url);
85
+ const existingScript = document.querySelector(
86
+ `script[src*="prod.diageoagegate.com"]`
87
+ );
88
+ if (existingScript) {
89
+ existingScript.remove();
90
+ }
91
+ document.querySelector("#diageo-footer-wrap")?.remove();
92
+ if (window?.init) {
93
+ window.init();
94
+ } else {
95
+ initInjectFooterJs();
96
+ }
97
+ });
98
+ function initInjectFooterJs() {
53
99
  const noFooterFromQuery = getUrlQuery("pageStatus") === "noFooter";
54
100
  const noAgeGateFromQuery = getUrlQuery("agp") === "true";
101
+ const _customerId = customerId || location.hostname;
55
102
  let src = `https://prod.diageoagegate.com/api/loader?customerId=${encodeURIComponent(
56
- customerId
103
+ _customerId
57
104
  )}&userHash=${encodeURIComponent(userHash)}&footerSelectorId=${containerId}`;
58
105
  const finalNoAgeGate = noAgeGate || noAgeGateFromQuery;
59
106
  const finalNoFooter = noFooter || noFooterFromQuery;
@@ -87,18 +134,7 @@ function AgeGate({
87
134
  existingScript.remove();
88
135
  }
89
136
  document.head.appendChild(script);
90
- return () => {
91
- script.remove();
92
- };
93
- }, [
94
- customerId,
95
- userHash,
96
- containerId,
97
- noAgeGate,
98
- noFooter,
99
- propCountryCode,
100
- propLanguage
101
- ]);
137
+ }
102
138
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { id: containerId });
103
139
  }
104
140
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.js CHANGED
@@ -1,5 +1,36 @@
1
+ // src/components/urlChange.tsx
2
+ import { useEffect, useRef } from "react";
3
+ function useUrlChange(onChange) {
4
+ const urlRef = useRef("");
5
+ useEffect(() => {
6
+ urlRef.current = window.location.href;
7
+ const check = () => {
8
+ const current = window.location.href;
9
+ if (urlRef.current !== current) {
10
+ urlRef.current = current;
11
+ onChange(current);
12
+ }
13
+ };
14
+ window.addEventListener("popstate", check);
15
+ const push = history.pushState;
16
+ const replace = history.replaceState;
17
+ history.pushState = (...args) => {
18
+ push(...args);
19
+ setTimeout(check, 0);
20
+ };
21
+ history.replaceState = (...args) => {
22
+ replace(...args);
23
+ setTimeout(check, 0);
24
+ };
25
+ return () => {
26
+ window.removeEventListener("popstate", check);
27
+ history.pushState = push;
28
+ history.replaceState = replace;
29
+ };
30
+ }, [onChange]);
31
+ }
32
+
1
33
  // src/components/AgeGate.tsx
2
- import { useEffect } from "react";
3
34
  import { jsx } from "react/jsx-runtime";
4
35
  function getCookieItem(key) {
5
36
  const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
@@ -23,11 +54,27 @@ function AgeGate({
23
54
  countryCode: propCountryCode,
24
55
  language: propLanguage
25
56
  }) {
26
- useEffect(() => {
57
+ useUrlChange((url) => {
58
+ console.log("\u5F53\u524DURL:", url);
59
+ const existingScript = document.querySelector(
60
+ `script[src*="prod.diageoagegate.com"]`
61
+ );
62
+ if (existingScript) {
63
+ existingScript.remove();
64
+ }
65
+ document.querySelector("#diageo-footer-wrap")?.remove();
66
+ if (window?.init) {
67
+ window.init();
68
+ } else {
69
+ initInjectFooterJs();
70
+ }
71
+ });
72
+ function initInjectFooterJs() {
27
73
  const noFooterFromQuery = getUrlQuery("pageStatus") === "noFooter";
28
74
  const noAgeGateFromQuery = getUrlQuery("agp") === "true";
75
+ const _customerId = customerId || location.hostname;
29
76
  let src = `https://prod.diageoagegate.com/api/loader?customerId=${encodeURIComponent(
30
- customerId
77
+ _customerId
31
78
  )}&userHash=${encodeURIComponent(userHash)}&footerSelectorId=${containerId}`;
32
79
  const finalNoAgeGate = noAgeGate || noAgeGateFromQuery;
33
80
  const finalNoFooter = noFooter || noFooterFromQuery;
@@ -61,18 +108,7 @@ function AgeGate({
61
108
  existingScript.remove();
62
109
  }
63
110
  document.head.appendChild(script);
64
- return () => {
65
- script.remove();
66
- };
67
- }, [
68
- customerId,
69
- userHash,
70
- containerId,
71
- noAgeGate,
72
- noFooter,
73
- propCountryCode,
74
- propLanguage
75
- ]);
111
+ }
76
112
  return /* @__PURE__ */ jsx("div", { id: containerId });
77
113
  }
78
114
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diageo-age-gate-react",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",