tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

simulator.js (1994B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { Actor } = require("resource://devtools/shared/protocol.js");
      8 const {
      9  simulatorSpec,
     10 } = require("resource://devtools/shared/specs/accessibility.js");
     11 
     12 const {
     13  simulation: { COLOR_TRANSFORMATION_MATRICES },
     14 } = require("resource://devtools/server/actors/accessibility/constants.js");
     15 
     16 /**
     17 * The SimulatorActor is responsible for setting color matrices
     18 * based on the simulation type specified.
     19 */
     20 class SimulatorActor extends Actor {
     21  constructor(conn, targetActor) {
     22    super(conn, simulatorSpec);
     23    this.targetActor = targetActor;
     24  }
     25 
     26  /**
     27   * Simulates a type of visual impairment (i.e. color blindness or contrast loss).
     28   *
     29   * @param  {object} options
     30   *         Properties: {Array} types
     31   *                     Contains the types of visual impairment(s) to be simulated.
     32   *                     Set default color matrix if array is empty.
     33   * @return {boolean}
     34   *         True if matrix was successfully applied, false otherwise.
     35   */
     36  simulate(options) {
     37    if (options.types.length > 1) {
     38      return false;
     39    }
     40 
     41    return this.setColorMatrix(
     42      COLOR_TRANSFORMATION_MATRICES[
     43        options.types.length === 1 ? options.types[0] : "NONE"
     44      ]
     45    );
     46  }
     47 
     48  setColorMatrix(colorMatrix) {
     49    if (!this.docShell) {
     50      return false;
     51    }
     52 
     53    try {
     54      this.docShell.setColorMatrix(colorMatrix);
     55    } catch (error) {
     56      return false;
     57    }
     58 
     59    return true;
     60  }
     61 
     62  /**
     63   * Disables all simulations by setting the default color matrix.
     64   */
     65  disable() {
     66    this.simulate({ types: [] });
     67  }
     68 
     69  destroy() {
     70    super.destroy();
     71 
     72    this.disable();
     73    this.targetActor = null;
     74  }
     75 
     76  get docShell() {
     77    return this.targetActor && this.targetActor.docShell;
     78  }
     79 }
     80 
     81 exports.SimulatorActor = SimulatorActor;