@letsscrapedata/controller 0.0.7 → 0.0.9

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/dist/index.cjs CHANGED
@@ -327,6 +327,7 @@ var PlaywrightPage = class extends import_node_events.default {
327
327
  #pageId;
328
328
  #resquestInterceptionOptions;
329
329
  #responseInterceptionOptions;
330
+ #client;
330
331
  #hasValidUrl(page) {
331
332
  const url = page.url();
332
333
  return url.toLowerCase().startsWith("http");
@@ -538,6 +539,7 @@ var PlaywrightPage = class extends import_node_events.default {
538
539
  this.#pageId = `page${browserIdx}-${browserContextIdx}-${pageIdx}`;
539
540
  this.#resquestInterceptionOptions = [];
540
541
  this.#responseInterceptionOptions = [];
542
+ this.#client = null;
541
543
  this.#addPageOn();
542
544
  }
543
545
  async bringToFront() {
@@ -793,6 +795,24 @@ var PlaywrightPage = class extends import_node_events.default {
793
795
  );
794
796
  return true;
795
797
  }
798
+ async sendCDPMessage(method, params = null, detach = true) {
799
+ if (!this.#client) {
800
+ const origContext = this.browserContext()._origBrowserContext();
801
+ if (!origContext) {
802
+ throw new Error(`Invalid playwright browserContext`);
803
+ }
804
+ this.#client = origContext.newCDPSession(this.#page);
805
+ }
806
+ if (!this.#client) {
807
+ throw new Error("No valid CDP session to send message");
808
+ }
809
+ const response = params ? await this.#client.send(method, params) : await this.#client.send(method);
810
+ if (detach) {
811
+ await this.#client.detach();
812
+ this.#client = null;
813
+ }
814
+ return response;
815
+ }
796
816
  async setCookies(cookies) {
797
817
  if (!this.#page) {
798
818
  throw new Error("No valid page");
@@ -1080,7 +1100,25 @@ var PlaywrightPage = class extends import_node_events.default {
1080
1100
  retObj = retObj[key];
1081
1101
  }
1082
1102
  }
1083
- return JSON.stringify(retObj);
1103
+ if (typeof retObj === "string") {
1104
+ return retObj;
1105
+ } else if (typeof retObj === "number") {
1106
+ return String(retObj);
1107
+ } else if (typeof retObj === "boolean") {
1108
+ return String(Number(retObj));
1109
+ } else if (!retObj) {
1110
+ return "";
1111
+ } else if (typeof retObj === "object") {
1112
+ try {
1113
+ return JSON.stringify(retObj);
1114
+ } catch (err) {
1115
+ return "";
1116
+ }
1117
+ } else if (typeof retObj === "bigint") {
1118
+ return String(retObj);
1119
+ } else {
1120
+ return "";
1121
+ }
1084
1122
  },
1085
1123
  keys
1086
1124
  );
@@ -1769,6 +1807,7 @@ var PuppeteerPage = class extends import_node_events4.default {
1769
1807
  #pageId;
1770
1808
  #requestInterceptionNum;
1771
1809
  #responseInterceptionNum;
1810
+ #client;
1772
1811
  #hasValidUrl(page) {
1773
1812
  const url = page.url();
1774
1813
  return url.toLowerCase().startsWith("http");
@@ -1965,6 +2004,7 @@ var PuppeteerPage = class extends import_node_events4.default {
1965
2004
  this.#pageId = `page${browserIdx}-${browserContextIdx}-${pageIdx}`;
1966
2005
  this.#requestInterceptionNum = 0;
1967
2006
  this.#responseInterceptionNum = 0;
2007
+ this.#client = null;
1968
2008
  this.#addPageOn();
1969
2009
  }
1970
2010
  async bringToFront() {
@@ -2223,6 +2263,23 @@ var PuppeteerPage = class extends import_node_events4.default {
2223
2263
  );
2224
2264
  return true;
2225
2265
  }
2266
+ async sendCDPMessage(method, params = null, detach = true) {
2267
+ if (!this.#page) {
2268
+ throw new Error("No valid page");
2269
+ }
2270
+ if (!this.#client) {
2271
+ this.#client = await this.#page.createCDPSession();
2272
+ }
2273
+ if (!this.#client) {
2274
+ throw new Error("No valid CDP session to send message");
2275
+ }
2276
+ const response = params ? await this.#client.send(method, params) : await this.#client.send(method);
2277
+ if (detach) {
2278
+ await this.#client.detach();
2279
+ this.#client = null;
2280
+ }
2281
+ return response;
2282
+ }
2226
2283
  async setCookies(cookies) {
2227
2284
  if (!this.#page) {
2228
2285
  throw new Error("No valid page");
@@ -2493,7 +2550,25 @@ var PuppeteerPage = class extends import_node_events4.default {
2493
2550
  retObj = retObj[key];
2494
2551
  }
2495
2552
  }
2496
- return JSON.stringify(retObj);
2553
+ if (typeof retObj === "string") {
2554
+ return retObj;
2555
+ } else if (typeof retObj === "number") {
2556
+ return String(retObj);
2557
+ } else if (typeof retObj === "boolean") {
2558
+ return String(Number(retObj));
2559
+ } else if (!retObj) {
2560
+ return "";
2561
+ } else if (typeof retObj === "object") {
2562
+ try {
2563
+ return JSON.stringify(retObj);
2564
+ } catch (err) {
2565
+ return "";
2566
+ }
2567
+ } else if (typeof retObj === "bigint") {
2568
+ return String(retObj);
2569
+ } else {
2570
+ return "";
2571
+ }
2497
2572
  },
2498
2573
  keys
2499
2574
  );
@@ -17012,6 +17087,9 @@ var CheerioPage = class extends import_node_events7.default {
17012
17087
  async scrollTo() {
17013
17088
  throw new Error("Not supported in CheerioElement.");
17014
17089
  }
17090
+ async sendCDPMessage() {
17091
+ throw new Error("Method not implemented.");
17092
+ }
17015
17093
  async setCookies() {
17016
17094
  throw new Error("Not supported in CheerioPage.");
17017
17095
  }
package/dist/index.d.cts CHANGED
@@ -806,6 +806,14 @@ interface LsdPage extends EventEmitter {
806
806
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
807
807
  scrollBy(x: number, y: number): Promise<boolean>;
808
808
  scrollTo(x: number, y: number): Promise<boolean>;
809
+ /**
810
+ *
811
+ * Send a CDP message over the current(not detached) or new CDP session
812
+ * @param method protocol method name
813
+ * @param params default null(ignored), method parameters
814
+ * @param detach default true, whether to detach the CDPSession from target
815
+ */
816
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
809
817
  setCookies(cookies: CookieItem[]): Promise<boolean>;
810
818
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
811
819
  /**
@@ -997,6 +1005,7 @@ declare class PlaywrightPage extends EventEmitter implements LsdPage {
997
1005
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
998
1006
  scrollBy(x: number, y: number): Promise<boolean>;
999
1007
  scrollTo(x: number, y: number): Promise<boolean>;
1008
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
1000
1009
  setCookies(cookies: CookieItem[]): Promise<boolean>;
1001
1010
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
1002
1011
  setLocalStroage(localStorageItems: LocalStorageItem[]): Promise<boolean>;
@@ -1104,6 +1113,7 @@ declare class PuppeteerPage extends EventEmitter implements LsdPage {
1104
1113
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
1105
1114
  scrollBy(x: number, y: number): Promise<boolean>;
1106
1115
  scrollTo(x: number, y: number): Promise<boolean>;
1116
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
1107
1117
  setCookies(cookies: CookieItem[]): Promise<boolean>;
1108
1118
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
1109
1119
  setLocalStroage(localStorageItems: LocalStorageItem[]): Promise<boolean>;
@@ -1175,6 +1185,7 @@ declare class CheerioPage extends EventEmitter implements LsdPage {
1175
1185
  screenshot(): Promise<Buffer>;
1176
1186
  scrollBy(): Promise<boolean>;
1177
1187
  scrollTo(): Promise<boolean>;
1188
+ sendCDPMessage(): Promise<any>;
1178
1189
  setCookies(): Promise<boolean>;
1179
1190
  setExtraHTTPHeaders(): Promise<boolean>;
1180
1191
  setLocalStroage(): Promise<boolean>;
package/dist/index.d.ts CHANGED
@@ -806,6 +806,14 @@ interface LsdPage extends EventEmitter {
806
806
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
807
807
  scrollBy(x: number, y: number): Promise<boolean>;
808
808
  scrollTo(x: number, y: number): Promise<boolean>;
809
+ /**
810
+ *
811
+ * Send a CDP message over the current(not detached) or new CDP session
812
+ * @param method protocol method name
813
+ * @param params default null(ignored), method parameters
814
+ * @param detach default true, whether to detach the CDPSession from target
815
+ */
816
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
809
817
  setCookies(cookies: CookieItem[]): Promise<boolean>;
810
818
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
811
819
  /**
@@ -997,6 +1005,7 @@ declare class PlaywrightPage extends EventEmitter implements LsdPage {
997
1005
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
998
1006
  scrollBy(x: number, y: number): Promise<boolean>;
999
1007
  scrollTo(x: number, y: number): Promise<boolean>;
1008
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
1000
1009
  setCookies(cookies: CookieItem[]): Promise<boolean>;
1001
1010
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
1002
1011
  setLocalStroage(localStorageItems: LocalStorageItem[]): Promise<boolean>;
@@ -1104,6 +1113,7 @@ declare class PuppeteerPage extends EventEmitter implements LsdPage {
1104
1113
  screenshot(options?: ScreenshotOptions): Promise<Buffer>;
1105
1114
  scrollBy(x: number, y: number): Promise<boolean>;
1106
1115
  scrollTo(x: number, y: number): Promise<boolean>;
1116
+ sendCDPMessage(method: string, params?: object | null, detach?: boolean): Promise<any>;
1107
1117
  setCookies(cookies: CookieItem[]): Promise<boolean>;
1108
1118
  setExtraHTTPHeaders(headers: Record<string, string>): Promise<boolean>;
1109
1119
  setLocalStroage(localStorageItems: LocalStorageItem[]): Promise<boolean>;
@@ -1175,6 +1185,7 @@ declare class CheerioPage extends EventEmitter implements LsdPage {
1175
1185
  screenshot(): Promise<Buffer>;
1176
1186
  scrollBy(): Promise<boolean>;
1177
1187
  scrollTo(): Promise<boolean>;
1188
+ sendCDPMessage(): Promise<any>;
1178
1189
  setCookies(): Promise<boolean>;
1179
1190
  setExtraHTTPHeaders(): Promise<boolean>;
1180
1191
  setLocalStroage(): Promise<boolean>;
package/dist/index.js CHANGED
@@ -308,6 +308,7 @@ var PlaywrightPage = class extends EventEmitter {
308
308
  #pageId;
309
309
  #resquestInterceptionOptions;
310
310
  #responseInterceptionOptions;
311
+ #client;
311
312
  #hasValidUrl(page) {
312
313
  const url = page.url();
313
314
  return url.toLowerCase().startsWith("http");
@@ -519,6 +520,7 @@ var PlaywrightPage = class extends EventEmitter {
519
520
  this.#pageId = `page${browserIdx}-${browserContextIdx}-${pageIdx}`;
520
521
  this.#resquestInterceptionOptions = [];
521
522
  this.#responseInterceptionOptions = [];
523
+ this.#client = null;
522
524
  this.#addPageOn();
523
525
  }
524
526
  async bringToFront() {
@@ -774,6 +776,24 @@ var PlaywrightPage = class extends EventEmitter {
774
776
  );
775
777
  return true;
776
778
  }
779
+ async sendCDPMessage(method, params = null, detach = true) {
780
+ if (!this.#client) {
781
+ const origContext = this.browserContext()._origBrowserContext();
782
+ if (!origContext) {
783
+ throw new Error(`Invalid playwright browserContext`);
784
+ }
785
+ this.#client = origContext.newCDPSession(this.#page);
786
+ }
787
+ if (!this.#client) {
788
+ throw new Error("No valid CDP session to send message");
789
+ }
790
+ const response = params ? await this.#client.send(method, params) : await this.#client.send(method);
791
+ if (detach) {
792
+ await this.#client.detach();
793
+ this.#client = null;
794
+ }
795
+ return response;
796
+ }
777
797
  async setCookies(cookies) {
778
798
  if (!this.#page) {
779
799
  throw new Error("No valid page");
@@ -1061,7 +1081,25 @@ var PlaywrightPage = class extends EventEmitter {
1061
1081
  retObj = retObj[key];
1062
1082
  }
1063
1083
  }
1064
- return JSON.stringify(retObj);
1084
+ if (typeof retObj === "string") {
1085
+ return retObj;
1086
+ } else if (typeof retObj === "number") {
1087
+ return String(retObj);
1088
+ } else if (typeof retObj === "boolean") {
1089
+ return String(Number(retObj));
1090
+ } else if (!retObj) {
1091
+ return "";
1092
+ } else if (typeof retObj === "object") {
1093
+ try {
1094
+ return JSON.stringify(retObj);
1095
+ } catch (err) {
1096
+ return "";
1097
+ }
1098
+ } else if (typeof retObj === "bigint") {
1099
+ return String(retObj);
1100
+ } else {
1101
+ return "";
1102
+ }
1065
1103
  },
1066
1104
  keys
1067
1105
  );
@@ -1750,6 +1788,7 @@ var PuppeteerPage = class extends EventEmitter4 {
1750
1788
  #pageId;
1751
1789
  #requestInterceptionNum;
1752
1790
  #responseInterceptionNum;
1791
+ #client;
1753
1792
  #hasValidUrl(page) {
1754
1793
  const url = page.url();
1755
1794
  return url.toLowerCase().startsWith("http");
@@ -1946,6 +1985,7 @@ var PuppeteerPage = class extends EventEmitter4 {
1946
1985
  this.#pageId = `page${browserIdx}-${browserContextIdx}-${pageIdx}`;
1947
1986
  this.#requestInterceptionNum = 0;
1948
1987
  this.#responseInterceptionNum = 0;
1988
+ this.#client = null;
1949
1989
  this.#addPageOn();
1950
1990
  }
1951
1991
  async bringToFront() {
@@ -2204,6 +2244,23 @@ var PuppeteerPage = class extends EventEmitter4 {
2204
2244
  );
2205
2245
  return true;
2206
2246
  }
2247
+ async sendCDPMessage(method, params = null, detach = true) {
2248
+ if (!this.#page) {
2249
+ throw new Error("No valid page");
2250
+ }
2251
+ if (!this.#client) {
2252
+ this.#client = await this.#page.createCDPSession();
2253
+ }
2254
+ if (!this.#client) {
2255
+ throw new Error("No valid CDP session to send message");
2256
+ }
2257
+ const response = params ? await this.#client.send(method, params) : await this.#client.send(method);
2258
+ if (detach) {
2259
+ await this.#client.detach();
2260
+ this.#client = null;
2261
+ }
2262
+ return response;
2263
+ }
2207
2264
  async setCookies(cookies) {
2208
2265
  if (!this.#page) {
2209
2266
  throw new Error("No valid page");
@@ -2474,7 +2531,25 @@ var PuppeteerPage = class extends EventEmitter4 {
2474
2531
  retObj = retObj[key];
2475
2532
  }
2476
2533
  }
2477
- return JSON.stringify(retObj);
2534
+ if (typeof retObj === "string") {
2535
+ return retObj;
2536
+ } else if (typeof retObj === "number") {
2537
+ return String(retObj);
2538
+ } else if (typeof retObj === "boolean") {
2539
+ return String(Number(retObj));
2540
+ } else if (!retObj) {
2541
+ return "";
2542
+ } else if (typeof retObj === "object") {
2543
+ try {
2544
+ return JSON.stringify(retObj);
2545
+ } catch (err) {
2546
+ return "";
2547
+ }
2548
+ } else if (typeof retObj === "bigint") {
2549
+ return String(retObj);
2550
+ } else {
2551
+ return "";
2552
+ }
2478
2553
  },
2479
2554
  keys
2480
2555
  );
@@ -16993,6 +17068,9 @@ var CheerioPage = class extends EventEmitter7 {
16993
17068
  async scrollTo() {
16994
17069
  throw new Error("Not supported in CheerioElement.");
16995
17070
  }
17071
+ async sendCDPMessage() {
17072
+ throw new Error("Method not implemented.");
17073
+ }
16996
17074
  async setCookies() {
16997
17075
  throw new Error("Not supported in CheerioPage.");
16998
17076
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@letsscrapedata/controller",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Unified browser / HTML controller interfaces that support playwright, puppeteer and cheerio",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",