File: interactionsViewer.js

package info (click to toggle)
firefox-esr 91.12.0esr-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,375,668 kB
  • sloc: cpp: 5,762,032; javascript: 5,481,714; ansic: 3,121,206; python: 851,492; asm: 331,174; xml: 178,949; java: 155,554; sh: 63,704; makefile: 20,127; perl: 12,825; yacc: 4,583; cs: 3,846; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; php: 436; lisp: 260; awk: 231; ruby: 103; sed: 53; sql: 46; csh: 45
file content (146 lines) | stat: -rw-r--r-- 4,247 bytes parent folder | download | duplicates (4)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env module */

const { Interactions } = ChromeUtils.import(
  "resource:///modules/Interactions.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { PlacesUtils } = ChromeUtils.import(
  "resource://gre/modules/PlacesUtils.jsm"
);

const metadataHandler = new (class {
  /**
   * Maximum number of rows to display by default.
   *
   * @typedef {number}
   */
  #maxRows = 100;

  /**
   * A reference to the database connection.
   *
   * @typedef {mozIStorageConnection}
   */
  #db = null;

  /**
   * A map of columns that are displayed by default.
   * @note If you change the number of columns, then you also need to change
   *       the css to account for the new number.
   *
   * - The key is the column name in the database.
   * - The header is the column header on the table.
   * - The modifier is a function to modify the returned value from the database
   *   for display.
   * - includeTitle determines if the title attribute should be set on that
   *   column, for tooltips, e.g. if an element is likely to overflow.
   *
   * @typedef {Map<string, object>}
   */
  #columnMap = new Map([
    ["id", { header: "ID" }],
    ["url", { header: "URL", includeTitle: true }],
    [
      "updated_at",
      {
        header: "Updated",
        modifier: updatedAt => new Date(updatedAt).toLocaleString(),
      },
    ],
    [
      "total_view_time",
      {
        header: "View Time (s)",
        modifier: totalViewTime => (totalViewTime / 1000).toFixed(2),
      },
    ],
    [
      "typing_time",
      {
        header: "Typing Time (s)",
        modifier: typingTime => (typingTime / 1000).toFixed(2),
      },
    ],
    ["key_presses", { header: "Key Presses" }],
  ]);

  async start() {
    this.#setupUI();
    this.#db = await PlacesUtils.promiseDBConnection();
    await this.#updateDisplay();
    setInterval(this.#updateDisplay.bind(this), 10000);
  }

  /**
   * Creates the initial table layout to the correct size.
   */
  #setupUI() {
    let tableBody = document.createDocumentFragment();
    let header = document.createDocumentFragment();
    for (let details of this.#columnMap.values()) {
      let columnDiv = document.createElement("div");
      columnDiv.textContent = details.header;
      header.appendChild(columnDiv);
    }
    tableBody.appendChild(header);

    for (let i = 0; i < this.#maxRows; i++) {
      let row = document.createDocumentFragment();
      for (let j = 0; j < this.#columnMap.size; j++) {
        row.appendChild(document.createElement("div"));
      }
      tableBody.appendChild(row);
    }
    let viewer = document.getElementById("metadataViewer");
    viewer.appendChild(tableBody);

    let limit = document.getElementById("metadataLimit");
    limit.textContent = `Maximum rows displayed: ${this.#maxRows}.`;
  }

  /**
   * Loads the current metadata from the database and updates the display.
   */
  async #updateDisplay() {
    let rows = await this.#db.executeCached(
      `SELECT m.id AS id, h.url AS url, updated_at, total_view_time,
              typing_time, key_presses FROM moz_places_metadata m
       JOIN moz_places h ON h.id = m.place_id
       ORDER BY updated_at DESC
       LIMIT ${this.#maxRows}`
    );
    let viewer = document.getElementById("metadataViewer");
    let index = this.#columnMap.size;
    for (let row of rows) {
      for (let [column, details] of this.#columnMap.entries()) {
        let value = row.getResultByName(column);

        if (details.includeTitle) {
          viewer.children[index].setAttribute("title", value);
        }

        viewer.children[index].textContent = details.modifier
          ? details.modifier(value)
          : value;

        index++;
      }
    }
  }
})();

function checkPrefs() {
  if (
    !Services.prefs.getBoolPref("browser.places.interactions.enabled", false)
  ) {
    let warning = document.getElementById("enabledWarning");
    warning.hidden = false;
  }
}

checkPrefs();
metadataHandler.start().catch(console.error);