# 发现个Chrome自动操控小恐龙的脚本

**URL:** https://meta.appinn.net/t/topic/15025
**Category:** tim 的发现
**Tags:** chrome
**Created:** [2020 年3 月 31 日 04:29 UTC](https://meta.appinn.net/t/topic/15025 "2020-03-31T04:29:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### 作者： ![tim1103](https://meta-edge.appinn.com/user_avatar/meta.appinn.net/tim1103/32/5592_2.png) [@tim1103](https://meta.appinn.net/u/tim1103)
#### 发布日期： [2020 年3 月 31 日 04:29 UTC](https://meta.appinn.net/t/topic/15025/1 "2020-03-31T04:29:09Z")

</div>

```js
function TrexRunnerBot() {
  const makeKeyArgs = (keyCode) => {
    const preventDefault = () => void 0;
    return {keyCode, preventDefault};
  };

  const upKeyArgs = makeKeyArgs(38);
  const downKeyArgs = makeKeyArgs(40);
  const startArgs = makeKeyArgs(32);

  if (!Runner().playing) {
    Runner().onKeyDown(startArgs);
    setTimeout(() => {
      Runner().onKeyUp(startArgs);
    }, 500);
  }

  function conquerTheGame() {
    if (!Runner || !Runner().horizon.obstacles[0]) return;

    const obstacle = Runner().horizon.obstacles[0];

    if (obstacle.typeConfig && obstacle.typeConfig.type === 'SNACK') return;

    if (needsToTackle(obstacle) && closeEnoughToTackle(obstacle)) tackle(obstacle);
  }

  function needsToTackle(obstacle) {
    return obstacle.yPos !== 50;
  }

  function closeEnoughToTackle(obstacle) {
    return obstacle.xPos <= Runner().currentSpeed * 18;
  }

  function tackle(obstacle) {
    if (isDuckable(obstacle)) {
      duck();
    } else {
      jumpOver(obstacle);
    }
  }

  function isDuckable(obstacle) {
    return obstacle.yPos === 50;
  }

  function duck() {
    Runner().onKeyDown(downKeyArgs);

    setTimeout(() => {
      Runner().onKeyUp(downKeyArgs);
    }, 500);
  }

  function jumpOver(obstacle) {
    if (isNextObstacleCloseTo(obstacle))
      jumpFast();
    else
      Runner().onKeyDown(upKeyArgs);
  }

  function isNextObstacleCloseTo(currentObstacle) {
    const nextObstacle = Runner().horizon.obstacles[1];

    return nextObstacle && nextObstacle.xPos - currentObstacle.xPos <= Runner().currentSpeed * 42;
  }

  function jumpFast() {
    Runner().onKeyDown(upKeyArgs);
    Runner().onKeyUp(upKeyArgs);
  }

  return {conquerTheGame: conquerTheGame};
}

let bot = TrexRunnerBot();
let botInterval = setInterval(bot.conquerTheGame, 2);

```

* * *

食用方法:直接粘贴进`Console`里,按下空格,自动起跳  
效果:

 ![image](https://meta.appinn.net/uploads/default/original/2X/1/1fcd41c961ffe035ffd25ae0dd29823767605e48.png)  
这个不小心按到了空格…恐龙站在了鸟的头上… :joy:

---

<div class="post-metadata">

### 作者： ![liu](https://meta-edge.appinn.com/user_avatar/meta.appinn.net/liu/32/60234_2.png) [@liu](https://meta.appinn.net/u/liu)
#### 发布日期： [2020 年3 月 31 日 05:31 UTC](https://meta.appinn.net/t/topic/15025/2 "2020-03-31T05:31:57Z")

</div>

牛人啊，咋发现的？
