phantomas 2.11.0 → 2.12.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.
package/core/scope.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * phantomas browser "scope" with helper code
3
3
  *
4
- * Code below is executed in page's "scope" (injected by lib/browser.js)
4
+ * Code below is executed in page's "scope" (injected by the scope.injectJs() helper)
5
5
  */
6
6
  /* istanbul ignore next */
7
7
  (function coreScope(scope) {
package/lib/browser.js CHANGED
@@ -179,7 +179,7 @@ Browser.prototype.init = async (phantomasOptions) => {
179
179
  var meta = responses[data.requestId];
180
180
 
181
181
  /* istanbul ignore if */
182
- if (typeof meta === "undefined") {
182
+ if (typeof meta?.response === "undefined") {
183
183
  // the browser sometimes looses trace of a request, let's ignore.
184
184
  networkDebug(
185
185
  "Can't find request id %d in previous requests",
package/lib/index.js CHANGED
@@ -89,7 +89,10 @@ function phantomas(url, opts) {
89
89
  return page.$$(selector);
90
90
  },
91
91
 
92
- // @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageevaluateonnewdocumentpagefunction-args
92
+ // @see https://pptr.dev/api/puppeteer.page.evaluateonnewdocument
93
+ // Adds a function which would be invoked in when:
94
+ // - the child frame is attached or navigated
95
+ // - the page is navigated
93
96
  injectJs: async (script) => {
94
97
  const debug = require("debug")("phantomas:injectJs");
95
98
 
@@ -98,7 +101,9 @@ function phantomas(url, opts) {
98
101
  suffix = "}";
99
102
 
100
103
  const preloadFile =
101
- prefix + require("fs").readFileSync(script, "utf8") + suffix;
104
+ prefix +
105
+ (await require("fs/promises").readFile(script, "utf8")) +
106
+ suffix;
102
107
 
103
108
  await page.evaluateOnNewDocument(preloadFile);
104
109
 
@@ -117,8 +122,9 @@ function phantomas(url, opts) {
117
122
  // @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageexposefunctionname-puppeteerfunction
118
123
  await page.exposeFunction("__phantomas_emit", scope.emit);
119
124
 
120
- // Inject helper code into the browser's scope
121
- events.on("init", () => {
125
+ // Inject helper code into the browser's scope (but only once!)
126
+ events.once("init", () => {
127
+ debug("onInit: injecting the core/scope.js ...");
122
128
  scope.injectJs(__dirname + "/../core/scope.js");
123
129
  });
124
130
 
@@ -92,13 +92,17 @@
92
92
  })();
93
93
 
94
94
  // count <iframe> tags
95
- document.querySelectorAll("iframe").forEach(function (iframe) {
96
- phantomas.incrMetric("iframesCount"); // @desc number of iframe nodes
95
+ const iframes = document.querySelectorAll("iframe");
96
+ phantomas.setMetric("iframesCount", iframes.length); // @desc number of iframe nodes
97
+
98
+ for (const iframe of iframes) {
99
+ phantomas.log(`iframe: ${iframe.src}`);
100
+
97
101
  phantomas.addOffender("iframesCount", {
98
102
  element: phantomas.getDOMPath(iframe),
99
103
  url: iframe.src,
100
104
  });
101
- });
105
+ }
102
106
 
103
107
  phantomas.spyEnabled(true);
104
108
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phantomas",
3
- "version": "2.11.0",
3
+ "version": "2.12.0",
4
4
  "author": "macbre <maciej.brencz@gmail.com> (http://macbre.net)",
5
5
  "description": "Headless Chromium-based web performance metrics collector and monitoring tool",
6
6
  "main": "./lib/index.js",
@@ -34,7 +34,7 @@
34
34
  "decamelize": "^5.0.0",
35
35
  "fast-stats": "0.0.6",
36
36
  "js-yaml": "^4.0.0",
37
- "puppeteer": "^22.4.1"
37
+ "puppeteer": "^22.10.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@jest/globals": "^28.0.0",
@@ -62,7 +62,7 @@
62
62
  "coverageThreshold": {
63
63
  "global": {
64
64
  "statements": 89,
65
- "branches": 85,
65
+ "branches": 80,
66
66
  "functions": 89,
67
67
  "lines": 89
68
68
  }