fck-honey 0.3.0 → 0.3.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.
package/README.md CHANGED
@@ -9,7 +9,7 @@ Open source lib for Merchants to detect if a customer has Honey browser extensio
9
9
  ## Usage (Browser Global)
10
10
 
11
11
  ```html
12
- <script src="https://cdn.jsdelivr.net/npm/fck-honey/dist/honey-detect.min.js"></script>
12
+ <script src="https://cdn.jsdelivr.net/npm/fck-honey@0/dist/honey-detect.js"></script>
13
13
  <script>
14
14
  window.fckHoney.listen((warn) => {
15
15
  // Decide how you want to handle this. Native warn function allows you to tell the user to disable Honey.
@@ -41,3 +41,9 @@ window.fckHoney.listen((warn, el) => {
41
41
  // Set removeHoney to false if you want to keep the Honey element for some reason.
42
42
  }, { removeHoney: false });
43
43
  ```
44
+
45
+ ```js
46
+ window.fckHoney.listen((warn) => {
47
+ // Stop observing if nothing is detected within 10 seconds.
48
+ }, { unbindAfterSeconds: 10 });
49
+ ```
@@ -9,6 +9,8 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
+ import { VERSION } from "./version";
13
+ export var version = VERSION;
12
14
  var DEFAULT_Z_NEAR_MAX = 2147480000;
13
15
  var UUIDISH_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
14
16
  var TARGET_SELECTOR = "div[id]";
@@ -81,19 +83,12 @@ function looksLikeTargetDiv(el, zNearMax, uuidGate, debug) {
81
83
  console.log("+++ match", el);
82
84
  return true;
83
85
  }
84
- function scanElement(el, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn) {
86
+ function scanElement(el, seen, zNearMax, uuidGate, debug, handleMatch) {
85
87
  var _a;
86
88
  if (el instanceof HTMLDivElement && el.matches(TARGET_SELECTOR)) {
87
89
  if (seen.indexOf(el) === -1 && looksLikeTargetDiv(el, zNearMax, uuidGate, debug)) {
88
90
  seen.push(el);
89
- if (removeHoney && el.parentNode)
90
- el.parentNode.removeChild(el);
91
- if (removeHoney) {
92
- onMatch(warn);
93
- }
94
- else {
95
- onMatch(warn, el);
96
- }
91
+ handleMatch(el);
97
92
  }
98
93
  }
99
94
  var divs = (_a = el.querySelectorAll) === null || _a === void 0 ? void 0 : _a.call(el, TARGET_SELECTOR);
@@ -103,14 +98,7 @@ function scanElement(el, seen, zNearMax, uuidGate, debug, removeHoney, onMatch,
103
98
  var d = divs[i];
104
99
  if (seen.indexOf(d) === -1 && looksLikeTargetDiv(d, zNearMax, uuidGate, debug)) {
105
100
  seen.push(d);
106
- if (removeHoney && d.parentNode)
107
- d.parentNode.removeChild(d);
108
- if (removeHoney) {
109
- onMatch(warn);
110
- }
111
- else {
112
- onMatch(warn, d);
113
- }
101
+ handleMatch(d);
114
102
  }
115
103
  }
116
104
  }
@@ -122,8 +110,29 @@ export function startHoneyOverlayObserver(options) {
122
110
  var uuidGate = (_b = options.uuidGate) !== null && _b !== void 0 ? _b : true;
123
111
  var debug = (_c = options.debug) !== null && _c !== void 0 ? _c : false;
124
112
  var removeHoney = (_d = options.removeHoney) !== null && _d !== void 0 ? _d : true;
113
+ var unbindAfterSeconds = options.unbindAfterSeconds;
125
114
  var warn = function (message) { return showOverlay(message); };
126
115
  var onMatch = (_e = options.onMatch) !== null && _e !== void 0 ? _e : (function () { });
116
+ var matched = false;
117
+ var unbindTimer;
118
+ var handleMatch = function (el) {
119
+ matched = true;
120
+ if (typeof unbindTimer === "number") {
121
+ clearTimeout(unbindTimer);
122
+ unbindTimer = undefined;
123
+ }
124
+ if (removeHoney && el.parentNode)
125
+ el.parentNode.removeChild(el);
126
+ if (removeHoney) {
127
+ onMatch(warn);
128
+ }
129
+ else {
130
+ onMatch(warn, el);
131
+ }
132
+ };
133
+ // Greedy, page-wide observer: Honey overlays can be inserted late, moved,
134
+ // or have z-index applied after insertion. We watch all DOM changes so we
135
+ // can catch the element no matter when it appears or how it's styled.
127
136
  var mo = new MutationObserver(function (mutations) {
128
137
  for (var _i = 0, mutations_1 = mutations; _i < mutations_1.length; _i++) {
129
138
  var m = mutations_1[_i];
@@ -140,17 +149,17 @@ export function startHoneyOverlayObserver(options) {
140
149
  for (var i = 0; i < m.addedNodes.length; i += 1) {
141
150
  var node = m.addedNodes[i];
142
151
  if (node.nodeType === Node.ELEMENT_NODE) {
143
- scanElement(node, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
152
+ scanElement(node, seen, zNearMax, uuidGate, debug, handleMatch);
144
153
  }
145
154
  }
146
155
  }
147
156
  if (m.target instanceof Element) {
148
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
157
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
149
158
  }
150
159
  }
151
160
  else if (m.type === "attributes") {
152
161
  if (m.target instanceof Element) {
153
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
162
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
154
163
  }
155
164
  }
156
165
  }
@@ -161,9 +170,21 @@ export function startHoneyOverlayObserver(options) {
161
170
  attributes: true,
162
171
  attributeFilter: ["style", "class", "id"]
163
172
  });
164
- scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
173
+ scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, handleMatch);
174
+ if (typeof unbindAfterSeconds === "number" && unbindAfterSeconds > 0) {
175
+ unbindTimer = window.setTimeout(function () {
176
+ if (!matched) {
177
+ mo.disconnect();
178
+ }
179
+ }, unbindAfterSeconds * 1000);
180
+ }
165
181
  return {
166
- stop: function () { return mo.disconnect(); }
182
+ stop: function () {
183
+ mo.disconnect();
184
+ if (typeof unbindTimer === "number") {
185
+ clearTimeout(unbindTimer);
186
+ }
187
+ }
167
188
  };
168
189
  }
169
190
  export function listen(onMatch, options) {
@@ -1,6 +1,6 @@
1
- import { listen, startHoneyOverlayObserver } from "./core";
1
+ import { listen, version } from "./core";
2
2
  if (typeof window !== "undefined") {
3
3
  window.fckHoney = window.fckHoney || {};
4
- window.fckHoney.startHoneyOverlayObserver = startHoneyOverlayObserver;
5
4
  window.fckHoney.listen = listen;
5
+ window.fckHoney.version = version;
6
6
  }
@@ -0,0 +1,2 @@
1
+ // Auto-generated from package.json. Do not edit by hand.
2
+ export var VERSION = "0.3.2";
package/dist/esm/core.js CHANGED
@@ -9,6 +9,8 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
+ import { VERSION } from "./version";
13
+ export var version = VERSION;
12
14
  var DEFAULT_Z_NEAR_MAX = 2147480000;
13
15
  var UUIDISH_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
14
16
  var TARGET_SELECTOR = "div[id]";
@@ -81,19 +83,12 @@ function looksLikeTargetDiv(el, zNearMax, uuidGate, debug) {
81
83
  console.log("+++ match", el);
82
84
  return true;
83
85
  }
84
- function scanElement(el, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn) {
86
+ function scanElement(el, seen, zNearMax, uuidGate, debug, handleMatch) {
85
87
  var _a;
86
88
  if (el instanceof HTMLDivElement && el.matches(TARGET_SELECTOR)) {
87
89
  if (seen.indexOf(el) === -1 && looksLikeTargetDiv(el, zNearMax, uuidGate, debug)) {
88
90
  seen.push(el);
89
- if (removeHoney && el.parentNode)
90
- el.parentNode.removeChild(el);
91
- if (removeHoney) {
92
- onMatch(warn);
93
- }
94
- else {
95
- onMatch(warn, el);
96
- }
91
+ handleMatch(el);
97
92
  }
98
93
  }
99
94
  var divs = (_a = el.querySelectorAll) === null || _a === void 0 ? void 0 : _a.call(el, TARGET_SELECTOR);
@@ -103,14 +98,7 @@ function scanElement(el, seen, zNearMax, uuidGate, debug, removeHoney, onMatch,
103
98
  var d = divs[i];
104
99
  if (seen.indexOf(d) === -1 && looksLikeTargetDiv(d, zNearMax, uuidGate, debug)) {
105
100
  seen.push(d);
106
- if (removeHoney && d.parentNode)
107
- d.parentNode.removeChild(d);
108
- if (removeHoney) {
109
- onMatch(warn);
110
- }
111
- else {
112
- onMatch(warn, d);
113
- }
101
+ handleMatch(d);
114
102
  }
115
103
  }
116
104
  }
@@ -122,8 +110,29 @@ export function startHoneyOverlayObserver(options) {
122
110
  var uuidGate = (_b = options.uuidGate) !== null && _b !== void 0 ? _b : true;
123
111
  var debug = (_c = options.debug) !== null && _c !== void 0 ? _c : false;
124
112
  var removeHoney = (_d = options.removeHoney) !== null && _d !== void 0 ? _d : true;
113
+ var unbindAfterSeconds = options.unbindAfterSeconds;
125
114
  var warn = function (message) { return showOverlay(message); };
126
115
  var onMatch = (_e = options.onMatch) !== null && _e !== void 0 ? _e : (function () { });
116
+ var matched = false;
117
+ var unbindTimer;
118
+ var handleMatch = function (el) {
119
+ matched = true;
120
+ if (typeof unbindTimer === "number") {
121
+ clearTimeout(unbindTimer);
122
+ unbindTimer = undefined;
123
+ }
124
+ if (removeHoney && el.parentNode)
125
+ el.parentNode.removeChild(el);
126
+ if (removeHoney) {
127
+ onMatch(warn);
128
+ }
129
+ else {
130
+ onMatch(warn, el);
131
+ }
132
+ };
133
+ // Greedy, page-wide observer: Honey overlays can be inserted late, moved,
134
+ // or have z-index applied after insertion. We watch all DOM changes so we
135
+ // can catch the element no matter when it appears or how it's styled.
127
136
  var mo = new MutationObserver(function (mutations) {
128
137
  for (var _i = 0, mutations_1 = mutations; _i < mutations_1.length; _i++) {
129
138
  var m = mutations_1[_i];
@@ -140,17 +149,17 @@ export function startHoneyOverlayObserver(options) {
140
149
  for (var i = 0; i < m.addedNodes.length; i += 1) {
141
150
  var node = m.addedNodes[i];
142
151
  if (node.nodeType === Node.ELEMENT_NODE) {
143
- scanElement(node, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
152
+ scanElement(node, seen, zNearMax, uuidGate, debug, handleMatch);
144
153
  }
145
154
  }
146
155
  }
147
156
  if (m.target instanceof Element) {
148
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
157
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
149
158
  }
150
159
  }
151
160
  else if (m.type === "attributes") {
152
161
  if (m.target instanceof Element) {
153
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
162
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
154
163
  }
155
164
  }
156
165
  }
@@ -161,9 +170,21 @@ export function startHoneyOverlayObserver(options) {
161
170
  attributes: true,
162
171
  attributeFilter: ["style", "class", "id"]
163
172
  });
164
- scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
173
+ scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, handleMatch);
174
+ if (typeof unbindAfterSeconds === "number" && unbindAfterSeconds > 0) {
175
+ unbindTimer = window.setTimeout(function () {
176
+ if (!matched) {
177
+ mo.disconnect();
178
+ }
179
+ }, unbindAfterSeconds * 1000);
180
+ }
165
181
  return {
166
- stop: function () { return mo.disconnect(); }
182
+ stop: function () {
183
+ mo.disconnect();
184
+ if (typeof unbindTimer === "number") {
185
+ clearTimeout(unbindTimer);
186
+ }
187
+ }
167
188
  };
168
189
  }
169
190
  export function listen(onMatch, options) {
@@ -0,0 +1,2 @@
1
+ // Auto-generated from package.json. Do not edit by hand.
2
+ export var VERSION = "0.3.2";
@@ -1,4 +1,7 @@
1
1
  (function() {
2
+ // dist/bundle-tmp/version.js
3
+ var VERSION = "0.3.2";
4
+
2
5
  // dist/bundle-tmp/core.js
3
6
  var __assign = function() {
4
7
  __assign = Object.assign || function(t) {
@@ -11,6 +14,7 @@
11
14
  };
12
15
  return __assign.apply(this, arguments);
13
16
  };
17
+ var version = VERSION;
14
18
  var DEFAULT_Z_NEAR_MAX = 214748e4;
15
19
  var UUIDISH_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
16
20
  var TARGET_SELECTOR = "div[id]";
@@ -82,18 +86,12 @@
82
86
  console.log("+++ match", el);
83
87
  return true;
84
88
  }
85
- function scanElement(el, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn) {
89
+ function scanElement(el, seen, zNearMax, uuidGate, debug, handleMatch) {
86
90
  var _a;
87
91
  if (el instanceof HTMLDivElement && el.matches(TARGET_SELECTOR)) {
88
92
  if (seen.indexOf(el) === -1 && looksLikeTargetDiv(el, zNearMax, uuidGate, debug)) {
89
93
  seen.push(el);
90
- if (removeHoney && el.parentNode)
91
- el.parentNode.removeChild(el);
92
- if (removeHoney) {
93
- onMatch(warn);
94
- } else {
95
- onMatch(warn, el);
96
- }
94
+ handleMatch(el);
97
95
  }
98
96
  }
99
97
  var divs = (_a = el.querySelectorAll) === null || _a === void 0 ? void 0 : _a.call(el, TARGET_SELECTOR);
@@ -103,13 +101,7 @@
103
101
  var d = divs[i];
104
102
  if (seen.indexOf(d) === -1 && looksLikeTargetDiv(d, zNearMax, uuidGate, debug)) {
105
103
  seen.push(d);
106
- if (removeHoney && d.parentNode)
107
- d.parentNode.removeChild(d);
108
- if (removeHoney) {
109
- onMatch(warn);
110
- } else {
111
- onMatch(warn, d);
112
- }
104
+ handleMatch(d);
113
105
  }
114
106
  }
115
107
  }
@@ -123,11 +115,28 @@
123
115
  var uuidGate = (_b = options.uuidGate) !== null && _b !== void 0 ? _b : true;
124
116
  var debug = (_c = options.debug) !== null && _c !== void 0 ? _c : false;
125
117
  var removeHoney = (_d = options.removeHoney) !== null && _d !== void 0 ? _d : true;
118
+ var unbindAfterSeconds = options.unbindAfterSeconds;
126
119
  var warn = function(message) {
127
120
  return showOverlay(message);
128
121
  };
129
122
  var onMatch = (_e = options.onMatch) !== null && _e !== void 0 ? _e : (function() {
130
123
  });
124
+ var matched = false;
125
+ var unbindTimer;
126
+ var handleMatch = function(el) {
127
+ matched = true;
128
+ if (typeof unbindTimer === "number") {
129
+ clearTimeout(unbindTimer);
130
+ unbindTimer = void 0;
131
+ }
132
+ if (removeHoney && el.parentNode)
133
+ el.parentNode.removeChild(el);
134
+ if (removeHoney) {
135
+ onMatch(warn);
136
+ } else {
137
+ onMatch(warn, el);
138
+ }
139
+ };
131
140
  var mo = new MutationObserver(function(mutations) {
132
141
  for (var _i = 0, mutations_1 = mutations; _i < mutations_1.length; _i++) {
133
142
  var m = mutations_1[_i];
@@ -142,16 +151,16 @@
142
151
  for (var i = 0; i < m.addedNodes.length; i += 1) {
143
152
  var node = m.addedNodes[i];
144
153
  if (node.nodeType === Node.ELEMENT_NODE) {
145
- scanElement(node, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
154
+ scanElement(node, seen, zNearMax, uuidGate, debug, handleMatch);
146
155
  }
147
156
  }
148
157
  }
149
158
  if (m.target instanceof Element) {
150
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
159
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
151
160
  }
152
161
  } else if (m.type === "attributes") {
153
162
  if (m.target instanceof Element) {
154
- scanElement(m.target, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
163
+ scanElement(m.target, seen, zNearMax, uuidGate, debug, handleMatch);
155
164
  }
156
165
  }
157
166
  }
@@ -162,10 +171,20 @@
162
171
  attributes: true,
163
172
  attributeFilter: ["style", "class", "id"]
164
173
  });
165
- scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, removeHoney, onMatch, warn);
174
+ scanElement(document.documentElement, seen, zNearMax, uuidGate, debug, handleMatch);
175
+ if (typeof unbindAfterSeconds === "number" && unbindAfterSeconds > 0) {
176
+ unbindTimer = window.setTimeout(function() {
177
+ if (!matched) {
178
+ mo.disconnect();
179
+ }
180
+ }, unbindAfterSeconds * 1e3);
181
+ }
166
182
  return {
167
183
  stop: function() {
168
- return mo.disconnect();
184
+ mo.disconnect();
185
+ if (typeof unbindTimer === "number") {
186
+ clearTimeout(unbindTimer);
187
+ }
169
188
  }
170
189
  };
171
190
  }
@@ -179,7 +198,7 @@
179
198
  // dist/bundle-tmp/global.js
180
199
  if (typeof window !== "undefined") {
181
200
  window.fckHoney = window.fckHoney || {};
182
- window.fckHoney.startHoneyOverlayObserver = startHoneyOverlayObserver;
183
201
  window.fckHoney.listen = listen;
202
+ window.fckHoney.version = version;
184
203
  }
185
204
  })();
@@ -1,3 +1,4 @@
1
+ export declare const version = "0.3.2";
1
2
  export type WarnCallback = (message: string) => () => void;
2
3
  export type MatchCallback = (warn: WarnCallback, el?: HTMLDivElement) => void;
3
4
  export interface ObserverOptions {
@@ -6,6 +7,7 @@ export interface ObserverOptions {
6
7
  zNearMax?: number;
7
8
  debug?: boolean;
8
9
  removeHoney?: boolean;
10
+ unbindAfterSeconds?: number;
9
11
  }
10
12
  export interface ObserverHandle {
11
13
  stop: () => void;
@@ -0,0 +1 @@
1
+ export declare const VERSION = "0.3.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fck-honey",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Detects Honey browser extension overlays for merchants.",
5
5
  "license": "MIT",
6
6
  "main": "dist/honey-detect.js",
@@ -17,6 +17,7 @@
17
17
  "dist"
18
18
  ],
19
19
  "scripts": {
20
+ "prebuild": "node scripts/write-version.cjs",
20
21
  "build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.bundle.json && esbuild dist/bundle-tmp/global.js --bundle --format=iife --platform=browser --target=es5 --outfile=dist/honey-detect.js",
21
22
  "prepublishOnly": "npm run build",
22
23
  "clean": "rm -rf dist"