aboutsummaryrefslogtreecommitdiff
path: root/src/content/client/FollowSlaveClient.ts
blob: eb9651ac1ca744d97d6d6d35c883d35d0ec9c3ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as messages from "../../shared/messages";

interface Size {
  width: number;
  height: number;
}

interface Point {
  x: number;
  y: number;
}

export default interface FollowSlaveClient {
  filterHints(prefix: string): void;

  requestHintCount(viewSize: Size, framePosition: Point): void;

  createHints(viewSize: Size, framePosition: Point, tags: string[]): void;

  clearHints(): void;

  activateIfExists(tag: string, newTab: boolean, background: boolean): void;
}

export class FollowSlaveClientImpl implements FollowSlaveClient {
  private target: Window;

  constructor(target: Window) {
    this.target = target;
  }

  filterHints(prefix: string): void {
    this.postMessage({
      type: messages.FOLLOW_SHOW_HINTS,
      prefix,
    });
  }

  requestHintCount(viewSize: Size, framePosition: Point): void {
    this.postMessage({
      type: messages.FOLLOW_REQUEST_COUNT_TARGETS,
      viewSize,
      framePosition,
    });
  }

  createHints(viewSize: Size, framePosition: Point, tags: string[]): void {
    this.postMessage({
      type: messages.FOLLOW_CREATE_HINTS,
      viewSize,
      framePosition,
      tags,
    });
  }

  clearHints(): void {
    this.postMessage({
      type: messages.FOLLOW_REMOVE_HINTS,
    });
  }

  activateIfExists(tag: string, newTab: boolean, background: boolean): void {
    this.postMessage({
      type: messages.FOLLOW_ACTIVATE,
      tag,
      newTab,
      background,
    });
  }

  private postMessage(msg: messages.Message): void {
    this.target.postMessage(JSON.stringify(msg), "*");
  }
}