File: highcontrast.js

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 10,671 bytes parent folder | download | duplicates (6)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var mode;
var enabled = false;
var scheme = '';
var timeoutId = null;

var filterMap = {
  '0': 'url("#hc_extension_off")',
  '1': 'url("#hc_extension_highcontrast")',
  '2': 'url("#hc_extension_grayscale")',
  '3': 'url("#hc_extension_invert")',
  '4': 'url("#hc_extension_invert_grayscale")',
  '5': 'url("#hc_extension_yellow_on_black")'
};

var svgContent = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><defs><filter x="0" y="0" width="99999" height="99999" id="hc_extension_off"><feComponentTransfer><feFuncR type="table" tableValues="0 1"/><feFuncG type="table" tableValues="0 1"/><feFuncB type="table" tableValues="0 1"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_highcontrast"><feComponentTransfer><feFuncR type="gamma" exponent="3.0"/><feFuncG type="gamma" exponent="3.0"/><feFuncB type="gamma" exponent="3.0"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_highcontrast_back"><feComponentTransfer><feFuncR type="gamma" exponent="0.33"/><feFuncG type="gamma" exponent="0.33"/><feFuncB type="gamma" exponent="0.33"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_grayscale"><feColorMatrix type="matrix" values="0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0"/><feComponentTransfer><feFuncR type="gamma" exponent="3"/><feFuncG type="gamma" exponent="3"/><feFuncB type="gamma" exponent="3"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_grayscale_back"><feComponentTransfer><feFuncR type="gamma" exponent="0.33"/><feFuncG type="gamma" exponent="0.33"/><feFuncB type="gamma" exponent="0.33"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_invert"><feComponentTransfer><feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_invert_back"><feComponentTransfer><feFuncR type="table" tableValues="1 0"/><feFuncG type="table" tableValues="1 0"/><feFuncB type="table" tableValues="1 0"/></feComponentTransfer><feComponentTransfer><feFuncR type="gamma" exponent="1.7"/><feFuncG type="gamma" exponent="1.7"/><feFuncB type="gamma" exponent="1.7"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_invert_grayscale"><feColorMatrix type="matrix" values="0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0"/><feComponentTransfer><feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/></feComponentTransfer></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_yellow_on_black"><feComponentTransfer><feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/><feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/></feComponentTransfer><feColorMatrix type="matrix" values="0.3 0.5 0.2 0 0 0.3 0.5 0.2 0 0 0 0 0 0 0 0 0 0 1 0"/></filter><filter x="0" y="0" width="99999" height="99999" id="hc_extension_yellow_on_black_back"><feComponentTransfer><feFuncR type="table" tableValues="1 0"/><feFuncG type="table" tableValues="1 0"/><feFuncB type="table" tableValues="1 0"/></feComponentTransfer><feComponentTransfer><feFuncR type="gamma" exponent="0.33"/><feFuncG type="gamma" exponent="0.33"/><feFuncB type="gamma" exponent="0.33"/></feComponentTransfer></filter></defs></svg>';

var cssTemplate = `
  html[hc="a0"] {
    -webkit-filter: url("#hc_extension_off");
  }
  html[hcx="0"] img[src*="jpg"], html[hcx="0"] img[src*="jpeg"],
  html[hcx="0"] svg image, html[hcx="0"] img.rg_i, html[hcx="0"] embed,
  html[hcx="0"] object, html[hcx="0"] video {
    -webkit-filter: url("#hc_extension_off");
  }

  html[hc="a1"] {
    -webkit-filter: url("#hc_extension_highcontrast");
  }
  html[hcx="1"] img[src*="jpg"], html[hcx="1"] img[src*="jpeg"],
  html[hcx="1"] img.rg_i, html[hcx="1"] svg image, html[hcx="1"] embed,
  html[hcx="1"] object, html[hcx="1"] video {
    -webkit-filter: url("#hc_extension_highcontrast_back");
  }

  html[hc="a2"] {
    -webkit-filter: url("#hc_extension_grayscale");
  }
  html[hcx="2"] img[src*="jpg"], html[hcx="2"] img[src*="jpeg"],
  html[hcx="2"] img.rg_i, html[hcx="2"] svg image, html[hcx="2"] embed,
  html[hcx="2"] object, html[hcx="2"] video {
    -webkit-filter: url("#hc_extension_grayscale_back");
  }

  html[hc="a3"] {
    -webkit-filter: url("#hc_extension_invert");
  }
  html[hcx="3"] img[src*="jpg"], html[hcx="3"] img[src*="jpeg"],
  html[hcx="3"] img.rg_i, html[hcx="3"] svg image, html[hcx="3"] embed,
  html[hcx="3"] object, html[hcx="3"] video {
    -webkit-filter: url("#hc_extension_invert_back");
  }

  html[hc="a4"] {
    -webkit-filter: url("#hc_extension_invert_grayscale");
  }
  html[hcx="4"] img[src*="jpg"], html[hcx="4"] img[src*="jpeg"],
  html[hcx="4"] img.rg_i, html[hcx="4"] svg image, html[hcx="4"] embed,
  html[hcx="4"] object, html[hcx="4"] video {
    -webkit-filter: url("#hc_extension_invert_back");
  }

  html[hc="a5"] {
    -webkit-filter: url("#hc_extension_yellow_on_black");
  }
  html[hcx="5"] img[src*="jpg"], html[hcx="5"] img[src*="jpeg"],
  html[hcx="5"] img.rg_i, html[hcx="5"] svg image, html[hcx="5"] embed,
  html[hcx="5"] object, html[hcx="5"] video {
    -webkit-filter: url("#hc_extension_yellow_on_black_back");
  }`;

/**
 * Add the elements to the pgae that make high-contrast adjustments possible.
 */
function addOrUpdateExtraElements() {
  if (!enabled)
    return;

  // We used to include the CSS, but that doesn't work when the document
  // uses the <base> element to set a relative url. So instead we
  // add a <style> element directly to the document with the right
  // urls hard-coded into it.
  var style = document.getElementById('hc_style');
  if (!style) {
    var baseUrl = window.location.href.replace(window.location.hash, '');
    var css = cssTemplate.replace(/#/g, baseUrl + '#');
    style = document.createElement('style');
    style.id = 'hc_style';
    style.setAttribute('type', 'text/css');
    style.innerHTML = css;
    document.head.appendChild(style);
  }

  // Starting in Chrome 45 we can't apply a filter to the html element,
  // so instead we create an element with low z-index that copies the
  // body's background.
  var bg = document.getElementById('hc_extension_bkgnd');
  if (!bg) {
    bg = document.createElement('div');
    bg.id = 'hc_extension_bkgnd';
    bg.style.position = 'fixed';
    bg.style.left = '0px';
    bg.style.top = '0px';
    bg.style.right = '0px';
    bg.style.bottom = '0px';
    bg.style.zIndex = -1999999999;
    document.body.appendChild(bg);
  }
  bg.style.display = 'block';
  bg.style.background = window.getComputedStyle(document.body).background;

  // As a special case, replace a zero-alpha background with white,
  // otherwise we can't invert it.
  var c = bg.style.backgroundColor;
  c = c.replace(/\s\s*/g,'');
  if (m = /^rgba\(([\d]+),([\d]+),([\d]+),([\d]+|[\d]*.[\d]+)\)/.exec(c)) {
    if (m[4] == '0') {
      bg.style.backgroundColor = '#fff';
    }
  }

  // Add a hidden element with the SVG filters.
  var wrap = document.getElementById('hc_extension_svg_filters');
  if (wrap)
    return;

  wrap = document.createElement('span');
  wrap.id = 'hc_extension_svg_filters';
  wrap.setAttribute('hidden', '');
  wrap.innerHTML = svgContent;
  document.body.appendChild(wrap);
}

/**
 * This is called on load and every time the mode might have changed
 * (i.e. enabling/disabling, or changing the type of contrast adjustment
 * for this page).
 */
function update() {
  var html = document.documentElement;
  if (enabled) {
    if (!document.body) {
      window.setTimeout(update, 100);
      return;
    }
    addOrUpdateExtraElements();
    if (html.getAttribute('hc') != mode + scheme)
      html.setAttribute('hc', mode + scheme);
    if (html.getAttribute('hcx') != scheme)
      html.setAttribute('hcx', scheme);

    if (window == window.top) {
      window.scrollBy(0, 1);
      window.scrollBy(0, -1);
    }
  } else {
    html.setAttribute('hc', mode + '0');
    html.setAttribute('hcx', '0');
    window.setTimeout(function() {
      html.removeAttribute('hc');
      html.removeAttribute('hcx');
      var bg = document.getElementById('hc_extension_bkgnd');
      if (bg)
        bg.style.display = 'none';
    }, 0);
  }
}

/**
 * Called when a pref changes.
 */
function maybeUpdate() {
  const newEnabled = getEnabled();
  const newScheme = getSiteScheme(siteFromUrl(location.href));
  if (enabled != newEnabled || scheme != newScheme) {
    enabled = newEnabled;
    scheme = newScheme;
    update();
  }
}

/**
 * KeyDown event handler
 */
function onKeyDown(evt) {
  if (evt.keyCode == 122 /* F11 */ &&
      evt.shiftKey) {
    chrome.runtime.sendMessage({toggle_global: true});
    evt.stopPropagation();
    evt.preventDefault();
    return false;
  }
  if (evt.keyCode == 123 /* F12 */ &&
      evt.shiftKey) {
    chrome.runtime.sendMessage({toggle_site: true});
    evt.stopPropagation();
    evt.preventDefault();
    return false;
  }
  return true;
}

function init() {
  if (window == window.top) {
    mode = 'a';
  } else {
    mode = 'b';
  }
  chrome.storage.local.onChanged.addListener(maybeUpdate);
  chrome.storage.local.get(null /* all values */).then(items => {
    if (items.enabled !== undefined && items.scheme !== undefined) {
      enabled = getEnabled();
      scheme = getSiteScheme(siteFromUrl(location.href));
      update();
    }
  });

  document.addEventListener('keydown', onKeyDown, false);

  // Update again after a few seconds and again after load so that
  // the background isn't wrong for long.
  window.setTimeout(addOrUpdateExtraElements, 2000);
  window.addEventListener('load', function() {
    addOrUpdateExtraElements();

    // Also update when the document body attributes change.
    var config = { attributes: true, childList: false, characterData: false };
    var observer = new MutationObserver(function(mutations) {
      addOrUpdateExtraElements();
    });
    observer.observe(document.body, config);
  });
}

init();