@d-zero/puppeteer-screenshot 3.1.1 → 3.1.3

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.
@@ -0,0 +1,6 @@
1
+ import type { Page } from 'puppeteer';
2
+ /**
3
+ *
4
+ * @param page
5
+ */
6
+ export declare function getCSS(page: Page): Promise<Record<string, Record<string, string>>>;
@@ -0,0 +1,70 @@
1
+ /**
2
+ *
3
+ * @param page
4
+ */
5
+ export async function getCSS(page) {
6
+ // DevTools Protocolセッションを開始
7
+ const client = await page.createCDPSession();
8
+ await client.send('DOM.enable');
9
+ await client.send('CSS.enable');
10
+ // 要素のDOM Node IDを取得(まずドキュメントから探索)
11
+ const { root } = await client.send('DOM.getDocument');
12
+ const elements = await client.send('DOM.querySelectorAll', {
13
+ nodeId: root.nodeId,
14
+ selector: '*',
15
+ });
16
+ const result = {};
17
+ for (const nodeId of elements.nodeIds) {
18
+ // ノード情報を取得
19
+ const { node } = await client.send('DOM.describeNode', { nodeId });
20
+ const nodeName = node.localName;
21
+ // CSS.getMatchedStylesForNodeでスタイル情報を取得
22
+ const declarationData = await client.send('CSS.getMatchedStylesForNode', { nodeId });
23
+ const { computedStyle } = await client.send('CSS.getComputedStyleForNode', {
24
+ nodeId,
25
+ });
26
+ // UAスタイル以外のプロパティを抽出
27
+ const { inlineStyle, matchedCSSRules } = declarationData;
28
+ if (computedStyle.find((p) => p.name === 'display')?.value === 'none') {
29
+ continue;
30
+ }
31
+ const explicitProperties = {};
32
+ if (inlineStyle) {
33
+ for (const prop of inlineStyle.cssProperties) {
34
+ if (prop.disabled || !prop.value)
35
+ continue;
36
+ const computedStyleValue = computedStyle.find((p) => p.name === prop.name)?.value;
37
+ if (computedStyleValue === prop.value)
38
+ continue;
39
+ explicitProperties[prop.name + '(inline)'] = prop.value;
40
+ }
41
+ }
42
+ for (const ruleMatch of matchedCSSRules ?? []) {
43
+ if (ruleMatch.rule.origin === 'regular') {
44
+ // UAスタイルシート以外
45
+ for (const prop of ruleMatch.rule.style.cssProperties) {
46
+ if (!prop.value)
47
+ continue;
48
+ const computedStyleValue = computedStyle.find((p) => p.name === prop.name)?.value;
49
+ if (computedStyleValue === prop.value)
50
+ continue;
51
+ explicitProperties[prop.name + '(regular)'] = prop.value;
52
+ }
53
+ }
54
+ }
55
+ // Sort property name
56
+ const sortedProperties = Object.entries(explicitProperties).toSorted((a, b) => {
57
+ // Remove vendor prefix
58
+ a[0] = a[0].replace(/^-(?:webkit|moz|ms)-/, '');
59
+ b[0] = b[0].replace(/^-(?:webkit|moz|ms)-/, '');
60
+ return a[0].localeCompare(b[0]);
61
+ });
62
+ result[`${nodeName}[${nodeId}]`] = Object.fromEntries(sortedProperties);
63
+ await client.send('DOM.setAttributeValue', {
64
+ nodeId,
65
+ name: 'data-css-properties',
66
+ value: JSON.stringify(sortedProperties),
67
+ });
68
+ }
69
+ return result;
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/puppeteer-screenshot",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "Screenshot utility for puppeteer",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -24,14 +24,14 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@d-zero/puppeteer-general-actions": "1.2.1",
27
- "@d-zero/puppeteer-page-scan": "4.0.2",
28
- "@d-zero/shared": "0.9.1"
27
+ "@d-zero/puppeteer-page-scan": "4.0.4",
28
+ "@d-zero/shared": "0.9.2"
29
29
  },
30
30
  "devDependencies": {
31
- "puppeteer": "24.10.2"
31
+ "puppeteer": "24.17.1"
32
32
  },
33
33
  "peerDependencies": {
34
- "puppeteer": "24.10.2"
34
+ "puppeteer": "24.17.1"
35
35
  },
36
- "gitHead": "1218a023e62c79efeece6350d561f2e1906be7ea"
36
+ "gitHead": "6fa0fd190c54351762a70039cd8dfcafc51caa0b"
37
37
  }