aboutsummaryrefslogtreecommitdiff
path: root/src/shared/messages.ts
blob: 2bc12d8490bb726c63cc8371adb2e365179c64c0 (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
75
76
77
78
type WebMessageSender = Window | MessagePort | ServiceWorker | null;
type WebMessageListener = (msg: any, sender: WebMessageSender | null) => void;

const onWebMessage = (listener: WebMessageListener) => {
  window.addEventListener('message', (event: MessageEvent) => {
    let sender = event.source;
    let message = null;
    try {
      message = JSON.parse(event.data);
    } catch (e) {
      // ignore unexpected message
      return;
    }
    listener(message, sender);
  });
};

const onBackgroundMessage = (
  listener: (msg: any, sender: browser.runtime.MessageSender,
) => void) => {
  browser.runtime.onMessage.addListener(listener);
};

const onMessage = (
  listener: (msg: any, sender: WebMessageSender | browser.runtime.MessageSender,
) => void) => {
  onWebMessage(listener);
  onBackgroundMessage(listener);
};

export default {
  BACKGROUND_OPERATION: 'background.operation',

  CONSOLE_UNFOCUS: 'console.unfocus',
  CONSOLE_ENTER_COMMAND: 'console.enter.command',
  CONSOLE_ENTER_FIND: 'console.enter.find',
  CONSOLE_QUERY_COMPLETIONS: 'console.query.completions',
  CONSOLE_SHOW_COMMAND: 'console.show.command',
  CONSOLE_SHOW_ERROR: 'console.show.error',
  CONSOLE_SHOW_INFO: 'console.show.info',
  CONSOLE_SHOW_FIND: 'console.show.find',
  CONSOLE_HIDE: 'console.hide',

  FOLLOW_START: 'follow.start',
  FOLLOW_REQUEST_COUNT_TARGETS: 'follow.request.count.targets',
  FOLLOW_RESPONSE_COUNT_TARGETS: 'follow.response.count.targets',
  FOLLOW_CREATE_HINTS: 'follow.create.hints',
  FOLLOW_SHOW_HINTS: 'follow.update.hints',
  FOLLOW_REMOVE_HINTS: 'follow.remove.hints',
  FOLLOW_ACTIVATE: 'follow.activate',
  FOLLOW_KEY_PRESS: 'follow.key.press',

  MARK_SET_GLOBAL: 'mark.set.global',
  MARK_JUMP_GLOBAL: 'mark.jump.global',

  TAB_SCROLL_TO: 'tab.scroll.to',

  FIND_NEXT: 'find.next',
  FIND_PREV: 'find.prev',
  FIND_GET_KEYWORD: 'find.get.keyword',
  FIND_SET_KEYWORD: 'find.set.keyword',

  ADDON_ENABLED_QUERY: 'addon.enabled.query',
  ADDON_ENABLED_RESPONSE: 'addon.enabled.response',
  ADDON_TOGGLE_ENABLED: 'addon.toggle.enabled',

  OPEN_URL: 'open.url',

  SETTINGS_CHANGED: 'settings.changed',
  SETTINGS_QUERY: 'settings.query',

  WINDOW_TOP_MESSAGE: 'window.top.message',
  CONSOLE_FRAME_MESSAGE: 'console.frame.message',

  onWebMessage,
  onBackgroundMessage,
  onMessage,
};