File: test_chrome_stubs.js

package info (click to toggle)
vimium 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,212 kB
  • sloc: javascript: 12,766; makefile: 7
file content (218 lines) | stat: -rw-r--r-- 4,261 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// This file contains stubs for a number of browser and chrome APIs which are missing in Deno.
//

window.document = {
  createElement() {
    return {};
  },
  addEventListener() {},
};

// There are 3 chrome.storage.* objects with identical APIs.
// - areaName: one of "local", "sync", "session".
const createStorageAPI = (areaName) => {
  const storage = {
    store: {},

    async set(items) {
      let key, value;
      chrome.runtime.lastError = undefined;
      for (key of Object.keys(items)) {
        value = items[key];
        this.store[key] = value;
      }
      for (key of Object.keys(items)) {
        value = items[key];
        window.chrome.storage.onChanged.call(key, value, areaName);
      }
    },

    async get(keysArg) {
      chrome.runtime.lastError = undefined;
      if (keysArg == null) {
        return globalThis.structuredClone(this.store);
      } else if (typeof keysArg == "string") {
        const result = {};
        result[keysArg] = globalThis.structuredClone(this.store[keysArg]);
        return result;
      } else {
        const result = {};
        for (key of keysArg) {
          result[key] = globalThis.structuredClone(this.store[key]);
        }
        return result;
      }
    },

    async remove(key) {
      chrome.runtime.lastError = undefined;
      if (key in this.store) {
        delete this.store[key];
      }
      window.chrome.storage.onChanged.callEmpty(key);
    },

    async clear() {
      // TODO: Consider firing the change listener if Chrome's API implementation does.
      this.store = {};
    },
  };

  // The "session" storage has one API that the others don't.
  if (areaName == "session") storage.setAccessLevel = () => {};
  return storage;
};

window.chrome = {
  areRunningVimiumTests: true,

  runtime: {
    getURL() {
      return "";
    },
    getManifest() {
      return { version: "1.2.3" };
    },
    onConnect: {
      addListener() {
        return true;
      },
    },
    onMessage: {
      addListener() {
        return true;
      },
    },
    onInstalled: {
      addListener() {},
    },
    onStartup: {
      addListener() {},
    },
  },

  extension: {
    getURL(path) {
      return path;
    },
    getBackgroundPage() {
      return {};
    },
    getViews() {
      return [];
    },
  },

  tabs: {
    get(_id) {},
    onUpdated: {
      addListener() {
        return true;
      },
    },
    onAttached: {
      addListener() {
        return true;
      },
    },
    onMoved: {
      addListener() {
        return true;
      },
    },
    onRemoved: {
      addListener() {
        return true;
      },
    },
    onActivated: {
      addListener() {
        return true;
      },
    },
    onReplaced: {
      addListener() {
        return true;
      },
    },
    query() {
      return true;
    },
    sendMessage(_id, _properties) {},
    update(_id, _properties) {},
  },

  webNavigation: {
    onHistoryStateUpdated: {
      addListener() {},
    },
    onReferenceFragmentUpdated: {
      addListener() {},
    },
    onCommitted: {
      addListener() {},
    },
  },

  windows: {
    onRemoved: {
      addListener() {
        return true;
      },
    },
    getAll() {
      return true;
    },
    getCurrent() {
      return {};
    },
    onFocusChanged: {
      addListener() {
        return true;
      },
    },
    update(_id, _properties) {},
  },

  browserAction: {
    setBadgeBackgroundColor() {},
  },

  sessions: {
    MAX_SESSION_RESULTS: 25,
  },

  storage: {
    onChanged: {
      addListener(func) {
        this.func = func;
      },

      // Fake a callback from chrome.storage.sync.
      call(key, value, area) {
        chrome.runtime.lastError = undefined;
        const key_value = {};
        key_value[key] = { newValue: value };
        if (this.func) return this.func(key_value, area);
      },

      callEmpty(key) {
        chrome.runtime.lastError = undefined;
        if (this.func) {
          const items = {};
          items[key] = {};
          this.func(items, "sync");
        }
      },
    },

    local: createStorageAPI("sync"),
    sync: createStorageAPI("sync"),
    session: createStorageAPI("session"),
  },

  bookmarks: {
    getTree: () => [],
  },
};