browserpaw 5.0.0 → 5.0.1

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.
Files changed (50) hide show
  1. package/dist/v5/cli.cjs +1207 -335
  2. package/dist/v5/extension/background.js +8649 -6972
  3. package/dist/v5/extension/browserpaw-build.json +52 -45
  4. package/dist/v5/extension/chunks/{popup-DjxqduQH.js → popup-BSqTH70w.js} +39 -6
  5. package/dist/v5/extension/content-scripts/agent-cursor.js +30 -14
  6. package/dist/v5/extension/inject-scripts/accessibility-tree-helper.js +377 -229
  7. package/dist/v5/extension/inject-scripts/keyboard-helper.js +12 -2
  8. package/dist/v5/extension/inject-scripts/network-helper.js +166 -1
  9. package/dist/v5/extension/inject-scripts/screenshot-helper.js +397 -356
  10. package/dist/v5/extension/inpage-engine.js +7070 -6933
  11. package/dist/v5/extension/manifest.json +1 -1
  12. package/dist/v5/extension/popup.html +1 -1
  13. package/dist/v5/host.cjs +445 -73
  14. package/dist/v5/recipes/bilibili-popular-videos.json +364 -0
  15. package/dist/v5/recipes/bilibili-video-detail.json +395 -0
  16. package/dist/v5/recipes/github-issue-thread.json +313 -0
  17. package/dist/v5/recipes/github-releases.json +86 -45
  18. package/dist/v5/recipes/github-repo-readme.json +306 -0
  19. package/dist/v5/recipes/github-repo-search.json +313 -0
  20. package/dist/v5/recipes/mdn-search.json +93 -48
  21. package/dist/v5/recipes/npm-compare.json +6 -4
  22. package/dist/v5/recipes/reddit-post-read.json +289 -0
  23. package/dist/v5/recipes/reddit-subreddit-posts.json +331 -0
  24. package/dist/v5/recipes/v2ex-node-topics.json +334 -0
  25. package/dist/v5/recipes/v2ex-topic-read.json +325 -0
  26. package/dist/v5/recipes/x-profile-posts.json +332 -0
  27. package/dist/v5/recipes/x-search-thread.json +13 -41
  28. package/dist/v5/recipes/x-visible-thread.json +40 -83
  29. package/dist/v5/recipes/youtube-search-videos.json +279 -0
  30. package/dist/v5/recipes/youtube-video-read.json +349 -0
  31. package/dist/v5/skill/browserpaw/SKILL.md +4 -4
  32. package/dist/v5/skill/browserpaw/assets/recipes/bilibili-popular-videos.json +364 -0
  33. package/dist/v5/skill/browserpaw/assets/recipes/bilibili-video-detail.json +395 -0
  34. package/dist/v5/skill/browserpaw/assets/recipes/github-issue-thread.json +313 -0
  35. package/dist/v5/skill/browserpaw/assets/recipes/github-releases.json +86 -45
  36. package/dist/v5/skill/browserpaw/assets/recipes/github-repo-readme.json +306 -0
  37. package/dist/v5/skill/browserpaw/assets/recipes/github-repo-search.json +313 -0
  38. package/dist/v5/skill/browserpaw/assets/recipes/mdn-search.json +93 -48
  39. package/dist/v5/skill/browserpaw/assets/recipes/npm-compare.json +6 -4
  40. package/dist/v5/skill/browserpaw/assets/recipes/reddit-post-read.json +289 -0
  41. package/dist/v5/skill/browserpaw/assets/recipes/reddit-subreddit-posts.json +331 -0
  42. package/dist/v5/skill/browserpaw/assets/recipes/v2ex-node-topics.json +334 -0
  43. package/dist/v5/skill/browserpaw/assets/recipes/v2ex-topic-read.json +325 -0
  44. package/dist/v5/skill/browserpaw/assets/recipes/x-profile-posts.json +332 -0
  45. package/dist/v5/skill/browserpaw/assets/recipes/x-search-thread.json +13 -41
  46. package/dist/v5/skill/browserpaw/assets/recipes/x-visible-thread.json +40 -83
  47. package/dist/v5/skill/browserpaw/assets/recipes/youtube-search-videos.json +279 -0
  48. package/dist/v5/skill/browserpaw/assets/recipes/youtube-video-read.json +349 -0
  49. package/dist/v5/skill/browserpaw/references/recipe-contract.md +4 -3
  50. package/package.json +1 -1
@@ -10,399 +10,440 @@
10
10
  // chrome.tabs.sendMessage, hanging the tool forever. Always register fresh -
11
11
  // the alive listener resolves the message channel.
12
12
 
13
- // StyleStack: safely record and restore inline styles, classes, and injected stylesheets
14
- const StyleStack = {
15
- _stack: [],
16
- _fixedStack: [],
17
- _styleSheets: [],
18
- _sliceStyleSheets: [],
19
-
20
- // Record beforeCss and apply inline styles with !important
21
- add(el, styles) {
22
- if (!el || !el.style) return;
23
- const beforeCss = el.style.cssText;
24
- for (const [prop, val] of Object.entries(styles)) {
25
- const kebab = prop.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
26
- try {
27
- el.style.setProperty(kebab, val, 'important');
28
- } catch {
29
- el.style[prop] = val;
13
+ (() => {
14
+ // StyleStack: safely record and restore inline styles, classes, and injected stylesheets
15
+ const StyleStack = {
16
+ _stack: [],
17
+ _fixedStack: [],
18
+ _styleSheets: [],
19
+ _sliceStyleSheets: [],
20
+
21
+ // Record beforeCss and apply inline styles with !important
22
+ add(el, styles) {
23
+ if (!el || !el.style) return;
24
+ const beforeCss = el.style.cssText;
25
+ for (const [prop, val] of Object.entries(styles)) {
26
+ const kebab = prop.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
27
+ try {
28
+ el.style.setProperty(kebab, val, 'important');
29
+ } catch {
30
+ el.style[prop] = val;
31
+ }
30
32
  }
31
- }
32
- this._stack.push({ el, before: beforeCss });
33
- },
34
-
35
- addFixed(el, styles, options = {}) {
36
- if (!el || !el.style) return;
37
- const beforeCss = el.style.cssText;
38
- const hadOriginalId =
39
- typeof el.hasAttribute === 'function' ? el.hasAttribute('id') : Boolean(el.id);
40
- const originalId = hadOriginalId ? el.getAttribute('id') : null;
41
- const addedId = options.addedId || false;
42
- for (const [prop, val] of Object.entries(styles)) {
43
- const kebab = prop.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
44
- try {
45
- el.style.setProperty(kebab, val, 'important');
46
- } catch {
47
- el.style[prop] = val;
33
+ this._stack.push({ el, before: beforeCss });
34
+ },
35
+
36
+ addFixed(el, styles, options = {}) {
37
+ if (!el || !el.style) return;
38
+ const beforeCss = el.style.cssText;
39
+ const hadOriginalId =
40
+ typeof el.hasAttribute === 'function' ? el.hasAttribute('id') : Boolean(el.id);
41
+ const originalId = hadOriginalId ? el.getAttribute('id') : null;
42
+ const addedId = options.addedId || false;
43
+ for (const [prop, val] of Object.entries(styles)) {
44
+ const kebab = prop.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
45
+ try {
46
+ el.style.setProperty(kebab, val, 'important');
47
+ } catch {
48
+ el.style[prop] = val;
49
+ }
48
50
  }
49
- }
50
- this._fixedStack.push({ el, before: beforeCss, addedId, hadOriginalId, originalId });
51
- },
52
-
53
- addStyleSheet(css) {
54
- const styleEl = document.createElement('style');
55
- styleEl.type = 'text/css';
56
- styleEl.appendChild(document.createTextNode(css));
57
- const parent = document.head || document.documentElement || document.body;
58
- if (parent) {
59
- parent.appendChild(styleEl);
60
- this._styleSheets.push(styleEl);
61
- }
62
- },
63
-
64
- addSliceStyleSheet(css) {
65
- const styleEl = document.createElement('style');
66
- styleEl.type = 'text/css';
67
- styleEl.appendChild(document.createTextNode(css));
68
- const parent = document.head || document.documentElement || document.body;
69
- if (parent) {
70
- parent.appendChild(styleEl);
71
- this._sliceStyleSheets.push(styleEl);
72
- }
73
- },
74
-
75
- // Pop all fixed slice modifications (restores elements before applying next slice rules)
76
- popAllFixed() {
77
- while (this._fixedStack.length > 0) {
78
- const item = this._fixedStack.pop();
79
- if (item && item.el) {
80
- item.el.style.cssText = item.before;
81
- if (item.addedId) {
82
- if (item.hadOriginalId && item.originalId !== null) {
83
- item.el.setAttribute('id', item.originalId);
84
- } else {
85
- item.el.removeAttribute('id');
51
+ this._fixedStack.push({ el, before: beforeCss, addedId, hadOriginalId, originalId });
52
+ },
53
+
54
+ addStyleSheet(css) {
55
+ const styleEl = document.createElement('style');
56
+ styleEl.type = 'text/css';
57
+ styleEl.appendChild(document.createTextNode(css));
58
+ const parent = document.head || document.documentElement || document.body;
59
+ if (parent) {
60
+ parent.appendChild(styleEl);
61
+ this._styleSheets.push(styleEl);
62
+ }
63
+ },
64
+
65
+ addSliceStyleSheet(css) {
66
+ const styleEl = document.createElement('style');
67
+ styleEl.type = 'text/css';
68
+ styleEl.appendChild(document.createTextNode(css));
69
+ const parent = document.head || document.documentElement || document.body;
70
+ if (parent) {
71
+ parent.appendChild(styleEl);
72
+ this._sliceStyleSheets.push(styleEl);
73
+ }
74
+ },
75
+
76
+ // Pop all fixed slice modifications (restores elements before applying next slice rules)
77
+ popAllFixed() {
78
+ while (this._fixedStack.length > 0) {
79
+ const item = this._fixedStack.pop();
80
+ if (item && item.el) {
81
+ item.el.style.cssText = item.before;
82
+ if (item.addedId) {
83
+ if (item.hadOriginalId && item.originalId !== null) {
84
+ item.el.setAttribute('id', item.originalId);
85
+ } else {
86
+ item.el.removeAttribute('id');
87
+ }
86
88
  }
87
89
  }
88
90
  }
89
- }
90
- while (this._sliceStyleSheets.length > 0) {
91
- const sheet = this._sliceStyleSheets.pop();
92
- if (sheet && sheet.parentNode) {
93
- sheet.parentNode.removeChild(sheet);
91
+ while (this._sliceStyleSheets.length > 0) {
92
+ const sheet = this._sliceStyleSheets.pop();
93
+ if (sheet && sheet.parentNode) {
94
+ sheet.parentNode.removeChild(sheet);
95
+ }
94
96
  }
95
- }
96
- },
97
-
98
- // 100% complete rollback of all DOM style mutations and added stylesheets
99
- popAll() {
100
- this.popAllFixed();
101
- while (this._stack.length > 0) {
102
- const item = this._stack.pop();
103
- if (item && item.el) {
104
- item.el.style.cssText = item.before;
97
+ },
98
+
99
+ // 100% complete rollback of all DOM style mutations and added stylesheets
100
+ popAll() {
101
+ this.popAllFixed();
102
+ while (this._stack.length > 0) {
103
+ const item = this._stack.pop();
104
+ if (item && item.el) {
105
+ item.el.style.cssText = item.before;
106
+ }
105
107
  }
106
- }
107
- while (this._styleSheets.length > 0) {
108
- const sheet = this._styleSheets.pop();
109
- if (sheet && sheet.parentNode) {
110
- sheet.parentNode.removeChild(sheet);
108
+ while (this._styleSheets.length > 0) {
109
+ const sheet = this._styleSheets.pop();
110
+ if (sheet && sheet.parentNode) {
111
+ sheet.parentNode.removeChild(sheet);
112
+ }
111
113
  }
112
- }
113
- },
114
- };
115
-
116
- // Expose on window for debugging / test inspection
117
- if (typeof window !== 'undefined') {
118
- window.__mcpStyleStack = StyleStack;
119
- }
120
-
121
- let originalOverflowStyle = '';
122
- let hiddenFixedElements = [];
123
-
124
- // querySelectorAll('*') does not cross shadow boundaries; walk open shadow roots
125
- // (nested included) so shadow-DOM fixed/sticky elements are captured too.
126
- function collectElementsIncludingShadowRoots(root, out = []) {
127
- for (const el of root.querySelectorAll('*')) {
128
- out.push(el);
129
- if (el.shadowRoot) collectElementsIncludingShadowRoots(el.shadowRoot, out);
114
+ },
115
+ };
116
+
117
+ // Expose on window for debugging / test inspection
118
+ if (typeof window !== 'undefined') {
119
+ window.__mcpStyleStack = StyleStack;
130
120
  }
131
- return out;
132
- }
133
121
 
134
- /**
135
- * Categorize fixed, sticky, header, and floating footer elements
136
- */
137
- function categorizeFixedAndStickyElements() {
138
- const headers = [];
139
- const footers = [];
140
- const stickies = [];
141
- const otherFixed = [];
142
-
143
- const viewportHeight = window.innerHeight;
144
- const viewportWidth = window.innerWidth;
145
-
146
- collectElementsIncludingShadowRoots(document).forEach((el) => {
147
- const tag = el.tagName;
148
- if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'NOSCRIPT' || tag === 'LINK') return;
149
- if (el.id && (el.id.startsWith('chrome-mcp-') || el.id.startsWith('__mcp_'))) return;
150
-
151
- if (el.offsetWidth <= 1 && el.offsetHeight <= 1) return;
152
-
153
- const style = window.getComputedStyle(el);
154
- if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return;
155
-
156
- if (style.position === 'sticky') {
157
- stickies.push(el);
158
- } else if (style.position === 'fixed') {
159
- const rect = el.getBoundingClientRect();
160
- // Skip elements completely outside viewport
161
- if (
162
- rect.bottom <= 0 ||
163
- rect.right <= 0 ||
164
- rect.top >= viewportHeight ||
165
- rect.left >= viewportWidth
166
- ) {
167
- return;
168
- }
169
- // Fixed header: docked at top
170
- if (rect.top <= 45 && rect.height < viewportHeight * 0.5) {
171
- headers.push(el);
172
- }
173
- // Fixed bottom banner / floating footer: docked at bottom
174
- else if (rect.bottom >= viewportHeight - 45 && rect.height < viewportHeight * 0.5) {
175
- footers.push(el);
176
- } else {
177
- otherFixed.push(el);
178
- }
122
+ let originalOverflowStyle = '';
123
+
124
+ // querySelectorAll('*') does not cross shadow boundaries; walk open shadow roots
125
+ // (nested included) so shadow-DOM fixed/sticky elements are captured too.
126
+ function collectElementsIncludingShadowRoots(root, out = []) {
127
+ for (const el of root.querySelectorAll('*')) {
128
+ out.push(el);
129
+ if (el.shadowRoot) collectElementsIncludingShadowRoots(el.shadowRoot, out);
179
130
  }
180
- });
131
+ return out;
132
+ }
181
133
 
182
- return { headers, footers, stickies, otherFixed };
183
- }
134
+ /**
135
+ * Categorize fixed, sticky, header, and floating footer elements
136
+ */
137
+ function categorizeFixedAndStickyElements() {
138
+ const headers = [];
139
+ const footers = [];
140
+ const stickies = [];
141
+ const otherFixed = [];
184
142
 
185
- /**
186
- * Configure DOM visibility for the current capture slice.
187
- * - Screen 1 (stepIndex 0): Headers visible, bottom banners hidden (unless also last step).
188
- * - Screen 2+ (stepIndex > 0): Headers hidden (visibility: hidden) to prevent duplicate banners.
189
- * - Stickies: converted to relative to prevent repetitive sticking across slices.
190
- * - Last slice (isLastStep true): Bottom banners visible once at the bottom of the page.
191
- */
192
- function prepareSlice(stepIndex, isLastStep) {
193
- StyleStack.popAllFixed();
143
+ const viewportHeight = window.innerHeight;
144
+ const viewportWidth = window.innerWidth;
194
145
 
195
- const { headers, footers, stickies, otherFixed } = categorizeFixedAndStickyElements();
146
+ collectElementsIncludingShadowRoots(document).forEach((el) => {
147
+ const tag = el.tagName;
148
+ if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'NOSCRIPT' || tag === 'LINK') return;
149
+ if (el.id && (el.id.startsWith('chrome-mcp-') || el.id.startsWith('__mcp_'))) return;
196
150
 
197
- if (stepIndex > 0) {
198
- // Screen 2+: Hide fixed headers to prevent duplication across slices
199
- headers.forEach((el) => {
200
- StyleStack.addFixed(el, { visibility: 'hidden', overflow: 'hidden' });
201
- });
151
+ if (el.offsetWidth <= 1 && el.offsetHeight <= 1) return;
152
+
153
+ const style = window.getComputedStyle(el);
154
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')
155
+ return;
202
156
 
203
- // Stickies: convert to relative coordinates and inject stylesheet rule with !important
204
- stickies.forEach((el, idx) => {
205
- let addedId = false;
206
- if (!el.id) {
207
- el.id = `__mcp_sticky_${Date.now()}_${idx}`;
208
- addedId = true;
157
+ if (style.position === 'sticky') {
158
+ stickies.push(el);
159
+ } else if (style.position === 'fixed') {
160
+ const rect = el.getBoundingClientRect();
161
+ // Skip elements completely outside viewport
162
+ if (
163
+ rect.bottom <= 0 ||
164
+ rect.right <= 0 ||
165
+ rect.top >= viewportHeight ||
166
+ rect.left >= viewportWidth
167
+ ) {
168
+ return;
169
+ }
170
+ // Fixed header: docked at top
171
+ if (rect.top <= 45 && rect.height < viewportHeight * 0.5) {
172
+ headers.push(el);
173
+ }
174
+ // Fixed bottom banner / floating footer: docked at bottom
175
+ else if (rect.bottom >= viewportHeight - 45 && rect.height < viewportHeight * 0.5) {
176
+ footers.push(el);
177
+ } else {
178
+ otherFixed.push(el);
179
+ }
209
180
  }
210
- StyleStack.addFixed(
211
- el,
212
- {
213
- position: 'relative',
214
- top: 'auto',
215
- left: 'auto',
216
- right: 'auto',
217
- bottom: 'auto',
218
- },
219
- { addedId },
220
- );
221
181
  });
222
- if (stickies.length > 0 && typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {
223
- const stickySelectors = stickies.map((el) => `#${CSS.escape(el.id)}`).join(', ');
224
- StyleStack.addSliceStyleSheet(
225
- `${stickySelectors} { position: relative !important; left: auto !important; right: auto !important; top: auto !important; bottom: auto !important; }`,
226
- );
227
- }
228
182
 
229
- // Other fixed elements (floating buttons, widgets): hide on subsequent slices
230
- otherFixed.forEach((el) => {
231
- StyleStack.addFixed(el, { visibility: 'hidden' });
232
- });
183
+ return { headers, footers, stickies, otherFixed };
233
184
  }
234
185
 
235
- if (!isLastStep) {
236
- // Prior to the last slice: hide floating bottom banners so they only appear at the bottom
237
- footers.forEach((el) => {
238
- StyleStack.addFixed(el, { visibility: 'hidden', overflow: 'hidden' });
239
- });
240
- }
241
- }
186
+ /**
187
+ * Configure DOM visibility for the current capture slice.
188
+ * - Screen 1 (stepIndex 0): Headers visible, bottom banners hidden (unless also last step).
189
+ * - Screen 2+ (stepIndex > 0): Headers hidden (visibility: hidden) to prevent duplicate banners.
190
+ * - Stickies: converted to relative to prevent repetitive sticking across slices.
191
+ * - Last slice (isLastStep true): Bottom banners visible once at the bottom of the page.
192
+ */
193
+ function prepareSlice(stepIndex, isLastStep) {
194
+ StyleStack.popAllFixed();
242
195
 
243
- /**
244
- * Backward compatibility helpers
245
- */
246
- function hideFixedElements() {
247
- prepareSlice(1, false);
248
- }
196
+ const { headers, footers, stickies, otherFixed } = categorizeFixedAndStickyElements();
249
197
 
250
- function showFixedElements() {
251
- StyleStack.popAllFixed();
252
- }
198
+ if (stepIndex > 0) {
199
+ // Screen 2+: Hide fixed headers to prevent duplication across slices
200
+ headers.forEach((el) => {
201
+ StyleStack.addFixed(el, { visibility: 'hidden', overflow: 'hidden' });
202
+ });
253
203
 
254
- /**
255
- * Measure full page dimensions accurately
256
- */
257
- function getDocumentDimensions() {
258
- const body = document.body;
259
- const html = document.documentElement;
260
- const totalWidth = Math.max(
261
- body ? body.scrollWidth : 0,
262
- body ? body.offsetWidth : 0,
263
- html ? html.clientWidth : 0,
264
- html ? html.scrollWidth : 0,
265
- html ? html.offsetWidth : 0,
266
- );
267
- const totalHeight = Math.max(
268
- body ? body.scrollHeight : 0,
269
- body ? body.offsetHeight : 0,
270
- html ? html.clientHeight : 0,
271
- html ? html.scrollHeight : 0,
272
- html ? html.offsetHeight : 0,
273
- );
274
- return {
275
- totalWidth: totalWidth || window.innerWidth,
276
- totalHeight: totalHeight || window.innerHeight,
277
- viewportWidth: window.innerWidth,
278
- viewportHeight: window.innerHeight,
279
- devicePixelRatio: window.devicePixelRatio || 1,
280
- currentScrollX: window.scrollX,
281
- currentScrollY: window.scrollY,
282
- };
283
- }
284
-
285
- // Listen for messages from the extension
286
- chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
287
- // Respond to ping message
288
- if (
289
- request.action === 'chrome_screenshot_ping' ||
290
- String(request.action || '').startsWith('mcp_ping_')
291
- ) {
292
- sendResponse({ status: 'pong' });
293
- return false; // Synchronous response
294
- }
204
+ // Stickies: convert to relative coordinates and inject stylesheet rule with !important
205
+ stickies.forEach((el, idx) => {
206
+ let addedId = false;
207
+ if (!el.id) {
208
+ el.id = `__mcp_sticky_${Date.now()}_${idx}`;
209
+ addedId = true;
210
+ }
211
+ StyleStack.addFixed(
212
+ el,
213
+ {
214
+ position: 'relative',
215
+ top: 'auto',
216
+ left: 'auto',
217
+ right: 'auto',
218
+ bottom: 'auto',
219
+ },
220
+ { addedId },
221
+ );
222
+ });
223
+ if (stickies.length > 0 && typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {
224
+ const stickySelectors = stickies.map((el) => `#${CSS.escape(el.id)}`).join(', ');
225
+ StyleStack.addSliceStyleSheet(
226
+ `${stickySelectors} { position: relative !important; left: auto !important; right: auto !important; top: auto !important; bottom: auto !important; }`,
227
+ );
228
+ }
295
229
 
296
- // Prepare page for capture
297
- else if (request.action === 'preparePageForCapture') {
298
- originalOverflowStyle = document.documentElement.style.overflow;
299
- // GoFullPage industrial scrollbar hiding via non-destructive CSS stylesheet
300
- // Avoids setting overflow: hidden on html/body which could freeze programmatic window.scrollTo
301
- StyleStack.addStyleSheet(
302
- 'html::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important; width: 0 !important; height: 0 !important; }\n' +
303
- 'html, body { scrollbar-width: none !important; -ms-overflow-style: none !important; }',
304
- );
305
- if (request.options?.fullPage) {
306
- prepareSlice(0, false);
230
+ // Other fixed elements (floating buttons, widgets): hide on subsequent slices
231
+ otherFixed.forEach((el) => {
232
+ StyleStack.addFixed(el, { visibility: 'hidden' });
233
+ });
307
234
  }
308
- // Give styles a moment to apply
309
- setTimeout(() => {
310
- sendResponse({ success: true });
311
- }, 50);
312
- return true; // Async response
313
- }
314
235
 
315
- // Prepare specific slice for full-page capture
316
- else if (request.action === 'prepareSlice') {
317
- prepareSlice(request.stepIndex || 0, Boolean(request.isLastStep));
318
- setTimeout(() => {
319
- sendResponse({ success: true });
320
- }, 30);
321
- return true; // Async response
236
+ if (!isLastStep) {
237
+ // Prior to the last slice: hide floating bottom banners so they only appear at the bottom
238
+ footers.forEach((el) => {
239
+ StyleStack.addFixed(el, { visibility: 'hidden', overflow: 'hidden' });
240
+ });
241
+ }
322
242
  }
323
243
 
324
- // Pop slice-specific fixed styles between slices
325
- else if (request.action === 'popSliceFixed') {
326
- StyleStack.popAllFixed();
327
- sendResponse({ success: true });
328
- return false;
244
+ /**
245
+ * Measure full page dimensions accurately
246
+ */
247
+ function getDocumentDimensions() {
248
+ const body = document.body;
249
+ const html = document.documentElement;
250
+ const totalWidth = Math.max(
251
+ body ? body.scrollWidth : 0,
252
+ body ? body.offsetWidth : 0,
253
+ html ? html.clientWidth : 0,
254
+ html ? html.scrollWidth : 0,
255
+ html ? html.offsetWidth : 0,
256
+ );
257
+ const totalHeight = Math.max(
258
+ body ? body.scrollHeight : 0,
259
+ body ? body.offsetHeight : 0,
260
+ html ? html.clientHeight : 0,
261
+ html ? html.scrollHeight : 0,
262
+ html ? html.offsetHeight : 0,
263
+ );
264
+ return {
265
+ totalWidth: totalWidth || window.innerWidth,
266
+ totalHeight: totalHeight || window.innerHeight,
267
+ viewportWidth: window.innerWidth,
268
+ viewportHeight: window.innerHeight,
269
+ devicePixelRatio: window.devicePixelRatio || 1,
270
+ currentScrollX: window.scrollX,
271
+ currentScrollY: window.scrollY,
272
+ };
329
273
  }
330
274
 
331
- // Page warmup: rapid scroll down and back up to trigger IntersectionObserver, lazy loading, and skeletons
332
- else if (request.action === 'warmupPage') {
333
- const dims = getDocumentDimensions();
334
- const maxScrollY = Math.max(0, dims.totalHeight - window.innerHeight);
275
+ const formatError = (error) => (error instanceof Error ? error.message : String(error));
335
276
 
336
- if (maxScrollY > 0) {
337
- window.scrollTo({ left: 0, top: maxScrollY, behavior: 'instant' });
338
- }
277
+ const safeRespond = (sendResponse, payload) => {
278
+ try {
279
+ sendResponse(payload);
280
+ } catch {}
281
+ };
339
282
 
340
- setTimeout(() => {
341
- // GoFullPage standard: scroll cleanly back to (0, 0) ready for slice 0
342
- window.scrollTo({ left: 0, top: 0, behavior: 'instant' });
343
- setTimeout(() => {
344
- const settledDims = getDocumentDimensions();
345
- sendResponse({
346
- success: true,
347
- totalWidth: settledDims.totalWidth,
348
- totalHeight: settledDims.totalHeight,
349
- });
350
- }, 80);
351
- }, 80);
352
- return true; // Async response
283
+ const LISTENER_KEY = '__MCP_SCREENSHOT_HELPER_LISTENER__';
284
+ const priorListener = globalThis[LISTENER_KEY];
285
+ if (typeof priorListener === 'function') {
286
+ try {
287
+ chrome.runtime.onMessage.removeListener(priorListener);
288
+ } catch {}
353
289
  }
354
290
 
355
- // Get page details
356
- else if (request.action === 'getPageDetails') {
357
- sendResponse(getDocumentDimensions());
358
- return false;
359
- }
291
+ // Listen for messages from the extension
292
+ const onScreenshotMessage = (request, _sender, sendResponse) => {
293
+ if (!request || typeof request !== 'object') return false;
360
294
 
361
- // Get element details
362
- else if (request.action === 'getElementDetails') {
363
- const element = document.querySelector(request.selector);
364
- if (element) {
365
- element.scrollIntoView({ behavior: 'instant', block: 'nearest', inline: 'nearest' });
366
- setTimeout(() => {
367
- const rect = element.getBoundingClientRect();
368
- sendResponse({
369
- rect: { x: rect.left, y: rect.top, width: rect.width, height: rect.height },
370
- devicePixelRatio: window.devicePixelRatio || 1,
371
- });
372
- }, 200);
373
- return true;
374
- } else {
375
- sendResponse({ error: `Element with selector "${request.selector}" not found.` });
376
- return false;
377
- }
378
- }
295
+ try {
296
+ // Respond to ping message
297
+ if (
298
+ request.action === 'chrome_screenshot_ping' ||
299
+ String(request.action || '').startsWith('mcp_ping_')
300
+ ) {
301
+ sendResponse({ status: 'pong' });
302
+ return false; // Synchronous response
303
+ }
379
304
 
380
- // Scroll page with dynamic height change detection
381
- else if (request.action === 'scrollPage') {
382
- window.scrollTo({ left: request.x, top: request.y, behavior: 'instant' });
383
- setTimeout(() => {
384
- const dims = getDocumentDimensions();
385
- sendResponse({
386
- success: true,
387
- newScrollX: window.scrollX,
388
- newScrollY: window.scrollY,
389
- totalHeight: dims.totalHeight,
390
- totalWidth: dims.totalWidth,
391
- });
392
- }, request.scrollDelay || 300);
393
- return true;
394
- }
305
+ // Prepare page for capture
306
+ else if (request.action === 'preparePageForCapture') {
307
+ originalOverflowStyle = document.documentElement.style.overflow;
308
+ // GoFullPage industrial scrollbar hiding via non-destructive CSS stylesheet
309
+ // Avoids setting overflow: hidden on html/body which could freeze programmatic window.scrollTo
310
+ StyleStack.addStyleSheet(
311
+ 'html::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important; width: 0 !important; height: 0 !important; }\n' +
312
+ 'html, body { scrollbar-width: none !important; -ms-overflow-style: none !important; }',
313
+ );
314
+ if (request.options?.fullPage) {
315
+ prepareSlice(0, false);
316
+ }
317
+ // Give styles a moment to apply
318
+ setTimeout(() => {
319
+ try {
320
+ sendResponse({ success: true });
321
+ } catch (error) {
322
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
323
+ }
324
+ }, 50);
325
+ return true; // Async response
326
+ }
395
327
 
396
- // Reset page after capture: 100% restore original styles and DOM via StyleStack.popAll()
397
- else if (request.action === 'resetPageAfterCapture') {
398
- document.documentElement.style.overflow = originalOverflowStyle;
399
- StyleStack.popAll();
400
- if (typeof request.scrollX !== 'undefined' && typeof request.scrollY !== 'undefined') {
401
- window.scrollTo({ left: request.scrollX, top: request.scrollY, behavior: 'instant' });
328
+ // Prepare specific slice for full-page capture
329
+ else if (request.action === 'prepareSlice') {
330
+ prepareSlice(request.stepIndex || 0, Boolean(request.isLastStep));
331
+ setTimeout(() => {
332
+ try {
333
+ sendResponse({ success: true });
334
+ } catch (error) {
335
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
336
+ }
337
+ }, 30);
338
+ return true; // Async response
339
+ }
340
+
341
+ // Pop slice-specific fixed styles between slices
342
+ else if (request.action === 'popSliceFixed') {
343
+ StyleStack.popAllFixed();
344
+ sendResponse({ success: true });
345
+ return false;
346
+ }
347
+
348
+ // Page warmup: rapid scroll down and back up to trigger IntersectionObserver, lazy loading, and skeletons
349
+ else if (request.action === 'warmupPage') {
350
+ const dims = getDocumentDimensions();
351
+ const maxScrollY = Math.max(0, dims.totalHeight - window.innerHeight);
352
+
353
+ if (maxScrollY > 0) {
354
+ window.scrollTo({ left: 0, top: maxScrollY, behavior: 'instant' });
355
+ }
356
+
357
+ setTimeout(() => {
358
+ try {
359
+ // GoFullPage standard: scroll cleanly back to (0, 0) ready for slice 0
360
+ window.scrollTo({ left: 0, top: 0, behavior: 'instant' });
361
+ setTimeout(() => {
362
+ try {
363
+ const settledDims = getDocumentDimensions();
364
+ sendResponse({
365
+ success: true,
366
+ totalWidth: settledDims.totalWidth,
367
+ totalHeight: settledDims.totalHeight,
368
+ });
369
+ } catch (error) {
370
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
371
+ }
372
+ }, 80);
373
+ } catch (error) {
374
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
375
+ }
376
+ }, 80);
377
+ return true; // Async response
378
+ }
379
+
380
+ // Get page details
381
+ else if (request.action === 'getPageDetails') {
382
+ sendResponse(getDocumentDimensions());
383
+ return false;
384
+ }
385
+
386
+ // Get element details
387
+ else if (request.action === 'getElementDetails') {
388
+ const element = document.querySelector(request.selector);
389
+ if (element) {
390
+ element.scrollIntoView({ behavior: 'instant', block: 'nearest', inline: 'nearest' });
391
+ setTimeout(() => {
392
+ try {
393
+ const rect = element.getBoundingClientRect();
394
+ sendResponse({
395
+ rect: { x: rect.left, y: rect.top, width: rect.width, height: rect.height },
396
+ devicePixelRatio: window.devicePixelRatio || 1,
397
+ });
398
+ } catch (error) {
399
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
400
+ }
401
+ }, 200);
402
+ return true;
403
+ } else {
404
+ sendResponse({ error: `Element with selector "${request.selector}" not found.` });
405
+ return false;
406
+ }
407
+ }
408
+
409
+ // Scroll page with dynamic height change detection
410
+ else if (request.action === 'scrollPage') {
411
+ window.scrollTo({ left: request.x, top: request.y, behavior: 'instant' });
412
+ setTimeout(() => {
413
+ try {
414
+ const dims = getDocumentDimensions();
415
+ sendResponse({
416
+ success: true,
417
+ newScrollX: window.scrollX,
418
+ newScrollY: window.scrollY,
419
+ totalHeight: dims.totalHeight,
420
+ totalWidth: dims.totalWidth,
421
+ });
422
+ } catch (error) {
423
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
424
+ }
425
+ }, request.scrollDelay || 300);
426
+ return true;
427
+ }
428
+
429
+ // Reset page after capture: 100% restore original styles and DOM via StyleStack.popAll()
430
+ else if (request.action === 'resetPageAfterCapture') {
431
+ document.documentElement.style.overflow = originalOverflowStyle;
432
+ StyleStack.popAll();
433
+ if (typeof request.scrollX !== 'undefined' && typeof request.scrollY !== 'undefined') {
434
+ window.scrollTo({ left: request.scrollX, top: request.scrollY, behavior: 'instant' });
435
+ }
436
+ sendResponse({ success: true });
437
+ return false;
438
+ }
439
+
440
+ return false;
441
+ } catch (error) {
442
+ safeRespond(sendResponse, { success: false, error: formatError(error) });
443
+ return false;
402
444
  }
403
- sendResponse({ success: true });
404
- return false;
405
- }
445
+ };
406
446
 
407
- return false;
408
- });
447
+ globalThis[LISTENER_KEY] = onScreenshotMessage;
448
+ chrome.runtime.onMessage.addListener(onScreenshotMessage);
449
+ })();