@eko-ai/eko-nodejs 2.0.8 → 2.1.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/dist/index.esm.js CHANGED
@@ -17,6 +17,10 @@ class BrowserAgent extends BaseBrowserLabelsAgent {
17
17
  this.browser = null;
18
18
  this.browser_context = null;
19
19
  this.current_page = null;
20
+ this.headless = false;
21
+ }
22
+ setHeadless(headless) {
23
+ this.headless = headless;
20
24
  }
21
25
  async screenshot(agentContext) {
22
26
  let page = await this.currentPage();
@@ -112,19 +116,8 @@ class BrowserAgent extends BaseBrowserLabelsAgent {
112
116
  return await page.evaluate(func, args);
113
117
  }
114
118
  async open_url(agentContext, url) {
115
- if (!this.browser) {
116
- this.current_page = null;
117
- this.browser_context = null;
118
- this.browser = await chromium.launch({
119
- headless: false,
120
- args: ["--no-sandbox"],
121
- });
122
- }
123
- if (!this.browser_context) {
124
- this.current_page = null;
125
- this.browser_context = await this.browser.newContext();
126
- }
127
- const page = await this.browser_context.newPage();
119
+ let browser_context = await this.getBrowserContext();
120
+ const page = await browser_context.newPage();
128
121
  // await page.setViewportSize({ width: 1920, height: 1080 });
129
122
  await page.setViewportSize({ width: 1536, height: 864 });
130
123
  try {
@@ -173,6 +166,62 @@ class BrowserAgent extends BaseBrowserLabelsAgent {
173
166
  sleep(time) {
174
167
  return new Promise((resolve) => setTimeout(() => resolve(), time));
175
168
  }
169
+ async getBrowserContext() {
170
+ if (!this.browser) {
171
+ this.current_page = null;
172
+ this.browser_context = null;
173
+ this.browser = await chromium.launch({
174
+ headless: this.headless,
175
+ args: ["--no-sandbox"],
176
+ });
177
+ }
178
+ if (!this.browser_context) {
179
+ this.current_page = null;
180
+ this.browser_context = await this.browser.newContext();
181
+ // Anti-crawling detection website:
182
+ // https://bot.sannysoft.com/
183
+ let init_script = await this.initScript();
184
+ this.browser_context.addInitScript(init_script);
185
+ }
186
+ return this.browser_context;
187
+ }
188
+ async initScript() {
189
+ return {
190
+ content: `
191
+ // Webdriver property
192
+ Object.defineProperty(navigator, 'webdriver', {
193
+ get: () => undefined
194
+ });
195
+
196
+ // Languages
197
+ Object.defineProperty(navigator, 'languages', {
198
+ get: () => ['en-US']
199
+ });
200
+
201
+ // Plugins
202
+ Object.defineProperty(navigator, 'plugins', {
203
+ get: () => [{name:"1"}, {name:"2"}, {name:"3"}, {name:"4"}, {name:"5"}]
204
+ });
205
+
206
+ // Chrome runtime
207
+ window.chrome = { runtime: {} };
208
+
209
+ // Permissions
210
+ const originalQuery = window.navigator.permissions.query;
211
+ window.navigator.permissions.query = (parameters) => (
212
+ parameters.name === 'notifications' ?
213
+ Promise.resolve({ state: Notification.permission }) :
214
+ originalQuery(parameters)
215
+ );
216
+ (function () {
217
+ const originalAttachShadow = Element.prototype.attachShadow;
218
+ Element.prototype.attachShadow = function attachShadow(options) {
219
+ return originalAttachShadow.call(this, { ...options, mode: "open" });
220
+ };
221
+ })();
222
+ `,
223
+ };
224
+ }
176
225
  }
177
226
 
178
227
  function getDefaultExportFromCjs (x) {
@@ -8099,11 +8148,11 @@ class FileAgent extends BaseFileAgent {
8099
8148
  name: file,
8100
8149
  path: filePath,
8101
8150
  isDirectory: stats.isDirectory(),
8102
- size: stats.size,
8103
- modified: stats.mtime,
8151
+ size: this.formatFileSize(stats.size),
8152
+ modified: stats.mtime.toLocaleString(),
8104
8153
  };
8105
8154
  }));
8106
- return fileDetails.map((s) => s.path);
8155
+ return fileDetails;
8107
8156
  }
8108
8157
  async file_read(agentContext, filePath) {
8109
8158
  return await fs.readFile(filePath, "utf-8");
@@ -8136,11 +8185,20 @@ class FileAgent extends BaseFileAgent {
8136
8185
  name: path$1.basename(file),
8137
8186
  path: file,
8138
8187
  isDirectory: stats.isDirectory(),
8139
- size: stats.size,
8140
- modified: stats.mtime,
8188
+ size: this.formatFileSize(stats.size),
8189
+ modified: stats.mtime.toLocaleString(),
8141
8190
  };
8142
8191
  }));
8143
- return fileDetails.map((s) => s.path);
8192
+ return fileDetails;
8193
+ }
8194
+ formatFileSize(size) {
8195
+ if (size < 1024) {
8196
+ return size + " B";
8197
+ }
8198
+ if (size < 1024 * 1024) {
8199
+ return (size / 1024).toFixed(1) + " KB";
8200
+ }
8201
+ return (size / 1024 / 1024).toFixed(1) + " MB";
8144
8202
  }
8145
8203
  }
8146
8204