# @kbourro/ts-browser

> Automated browser in TypeScript

Latest version **1.1.2** (published 2021-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kbourro/ts-browser
pnpm add @kbourro/ts-browser
yarn add @kbourro/ts-browser
bun add @kbourro/ts-browser
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2021-01-25 |
| First published | 2021-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 18 |
| Unpacked size | 121.3 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Kristofer Bourro |
| Maintainers | kristoferbourro |

## Links

- npm: https://www.npmjs.com/package/@kbourro/ts-browser
- Repository: https://github.com/kbourro/ts-browser
- Homepage: https://github.com/kbourro/ts-browser#readme
- Issues: https://github.com/kbourro/ts-browser/issues
- npm.io page: https://npm.io/package/@kbourro/ts-browser

## Dependencies (18)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [tldts](https://npm.io/package/tldts.md) ^5.6.83
- [chance](https://npm.io/package/chance.md) ^1.1.7
- [is-empty](https://npm.io/package/is-empty.md) ^1.2.0
- [bezier-js](https://npm.io/package/bezier-js.md) ^3.1.0
- [user-agents](https://npm.io/package/user-agents.md) ^1.0.559
- [ghost-cursor](https://npm.io/package/ghost-cursor.md) ^1.1.0
- [@types/chance](https://npm.io/package/@types/chance.md) ^1.1.1
- [@kbourro/proxy](https://npm.io/package/@kbourro/proxy.md) ^1.0.5
- [puppeteer-core](https://npm.io/package/puppeteer-core.md) ^5.5.0
- [@types/is-empty](https://npm.io/package/@types/is-empty.md) ^1.2.0
- [puppeteer-extra](https://npm.io/package/puppeteer-extra.md) ^3.1.16
- [@types/puppeteer](https://npm.io/package/@types/puppeteer.md) ^5.4.2
- [https-proxy-agent](https://npm.io/package/https-proxy-agent.md) ^5.0.0
- [@types/user-agents](https://npm.io/package/@types/user-agents.md) ^1.0.0
- [@types/puppeteer-core](https://npm.io/package/@types/puppeteer-core.md) ^5.4.0
- [puppeteer-extra-plugin](https://npm.io/package/puppeteer-extra-plugin.md) ^3.1.8
- [puppeteer-extra-plugin-stealth](https://npm.io/package/puppeteer-extra-plugin-stealth.md) ^2.6.6

## Recent versions

- 1.1.2 (latest) — 2021-01-25
- 1.1.1 — 2021-01-25
- 1.1.0 — 2021-01-22
- 1.0.9 — 2021-01-22
- 1.0.8 — 2021-01-19
- 1.0.7 — 2021-01-19
- 1.0.6 — 2021-01-19
- 1.0.5 — 2021-01-18
- 1.0.4 — 2021-01-18
- 1.0.3 — 2021-01-18
- 1.0.2 — 2021-01-18
- 1.0.1 — 2021-01-18
- 1.0.0 — 2021-01-18

## README

```typescript
import {
  Browser, Page, ElementHandleExtended, BrowserSettings, UserAgent
} from './browser';
import helpers from './helpers';

async function main() {
  const device = new UserAgent((
    data: {
      viewportHeight: null;
      viewportWidth: null;
      screenHeight: null;
      screenWidth: null;
      userAgent: string | string[];
      deviceCategory: any;
    },
  ) => {
    if (
      data.viewportHeight === null
      || data.viewportWidth === null
      || data.screenHeight === null
      || data.screenWidth === null
    ) {
      return false;
    }
    return (
      data.deviceCategory === 'desktop'
      && !data.userAgent.includes('-CN')
      && !data.userAgent.includes('baidu')
      && !data.userAgent.includes('alibaba')
      && !data.userAgent.includes('iPhone')
    );
  });
  const browserSettings: BrowserSettings = {
    location: {
      latitude: helpers.chance.floating({ min: 37.670777, max: 38.25975 }),
      longitude: helpers.chance.floating({ min: 23.640615, max: 23.975921 }),
    },
    languages: ['el-GR', 'el', 'en-US', 'en'],
    userAgent: device,
    proxy: new Proxy({ url: 'IP:PORT:USERNAME:PASSWORD' }), // OR url: 'http://USERNAME:PASSWORD@IP:PORT' OR username: 'USERNAME', password: 'PASSWORD' ...
  };
  const browserInstance: Browser = new Browser(browserSettings);
  const page = await browserInstance.launch();
  // browserInstance.goto(page,"https://bot.sannysoft.com/")
  await test(browserInstance, page);
}

async function test(browserObj: Browser, page: Page) {
  let element: ElementHandleExtended|null = null;
  let elements:ElementHandleExtended[]|null = null;
  await browserObj.goto(page, 'https://www.youtube.com');
  try {
    element = await browserObj.getElement(
      page,
      "//paper-button[@class='style-scope yt-button-renderer style-text size-small']",
      0,
      { visible: true, timeout: 5000 },
    );
    await browserObj.click(page, element);
    await browserObj.waitFor(5000);
  } catch (error) {
    element = null;
  }
  try {
    element = await browserObj.getElement(
      page,
      "//div[@id='introAgreeButton']/span",
      0,
      { visible: true, timeout: 2000 },
    );
    await browserObj.click(page, element);
    await browserObj.waitFor(5000);
  } catch (error) {
    element = null;
  }
  try {
    element = await browserObj.getElement(
      page,
      "//c3-material-button[@class='button-renderer compact upsell-dialog-dismiss-button']",
      0,
      { visible: true, timeout: 1000 },
    );
    await browserObj.click(page, element);
    await browserObj.waitFor(5000);
  } catch (error) {
    element = null;
  }
  try {
    element = await browserObj.getElement(
      page,
      "//div[@class='dialog mandatory-consent-kickin']//div[@class='consent-bump-button-wrapper']//button",
      0,
      { visible: true, timeout: 2000 },
    );
    await browserObj.click(page, element);
    await browserObj.waitFor(5000);
  } catch (error) {
    element = null;
  }

    element = await browserObj.getElement(
      page,
      "(//button[@class='icon-button topbar-menu-button-avatar-button'])[1]/c3-icon",
      0,
      { timeout: 2000 },
    );
    await browserObj.click(page, element);
    await browserObj.waitFor(5000);
    try {
      element = await browserObj.getElement(
        page,
        "//input[@name='search']",
        0,
        {
          visible: true,
          timeout: 5000,
        },
      );
      await browserObj.type(
        page,
        element,
        helpers.chance.pickone(['κλειδαράς πειραιά']),
        {
          delay: 500,
        },
        true,
      );
      await browserObj.waitFor(3000);
    } catch (error) {
      throw Error("Element //input[@name='search'] not found.");
    }
    try {
      element = await browserObj.getElement(
        page,
        "(//form[@class='searchbox-form cbox']/button)[2]",
        0,
        { visible: true, timeout: 5000 },
      );
      await browserObj.click(page, element);
    } catch (error) {
      throw Error(
        "Element //form[@class='searchbox-form cbox']/button not found.",
      );
    }
    await browserObj.waitFor(5000);

    elements = await browserObj.getElements(
      page,
      "//a[@class='compact-media-item-metadata-content']",
      { visible: true, timeout: 10000 },
    );
    for (const el of elements) {
      const href = await el.evaluate((el) => el.getAttribute('href'));
      if (href && href.includes('Q7sDE95WoxY')) {
        await browserObj.click(page, el);
        break;
      }
    }

    await browserObj.waitFor(3000);
    try {
      element = await browserObj.getElement(
        page,
        "//paper-button[@class='style-scope yt-button-renderer style-text size-small']",
        0,
        { visible: true, timeout: 2000 },
      );
      await browserObj.click(page, element);
      await browserObj.waitFor(5000);
    } catch (error) {
      element = null;
    }
    try {
      element = await browserObj.getElement(
        page,
        "//div[@id='introAgreeButton']/span",
        0,
        { visible: true, timeout: 2000 },
      );
      await browserObj.click(page, element);
      await browserObj.waitFor(5000);
    } catch (error) {
      element = null;
    }
    try {
      element = await browserObj.getElement(
        page,
        "//div[@class='watchPlayBugVerticalAlign']",
        0,
        { visible: true, timeout: 5000 },
      );
      await browserObj.click(page, element);
      await browserObj.waitFor(3000);
    } catch (error) {
      element = null;
    }
  await browserObj.close();
}
```

---
_Source: https://npm.io/package/@kbourro/ts-browser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
