pastebin

Paste Search Dynamic
Recent pastes
/utils.js
  1. /* Copyright 2018 The Chromium Authors. All rights reserved.
  2.  * Use of this source code is governed by a BSD-style license that can be
  3.  * found in the LICENSE file. */
  4.  
  5. /**
  6.  * Enum for classnames.
  7.  * @enum {string}
  8.  * @const
  9.  */
  10. const CLASSES = {
  11.   MAC: 'mac',                            // Applies MacOS specific properties.
  12.   WIN: 'win',                            // Applies Windows specific properties.
  13.   MOUSE_NAVIGATION: 'mouse-navigation',  // Removes blue focus ring.
  14. };
  15.  
  16. /**
  17.  * Alias for document.getElementById.
  18.  * @param {string} id The ID of the element to find.
  19.  * @return {Element} The found element or null if not found.
  20.  */
  21. function $(id) {
  22.   // eslint-disable-next-line no-restricted-properties
  23.   return document.getElementById(id);
  24. }
  25.  
  26. /**
  27.  * Contains common functions used in the main NTP page and its iframes.
  28.  */
  29. const utils = {};
  30.  
  31. /**
  32.  * Disables the focus outline for |element| on mousedown.
  33.  * @param {Element} element The element to remove the focus outline from.
  34.  */
  35. utils.disableOutlineOnMouseClick = function(element) {
  36.   element.addEventListener('mousedown', () => {
  37.     element.classList.add(CLASSES.MOUSE_NAVIGATION);
  38.     element.addEventListener('blur', () => {
  39.       element.classList.remove(CLASSES.MOUSE_NAVIGATION);
  40.     }, {once: true});
  41.   });
  42. };
  43.  
  44. /**
  45.  * Returns whether the given URL has a known, safe scheme.
  46.  * @param {string} url URL to check.
  47.  */
  48. utils.isSchemeAllowed = function(url) {
  49.   return url.startsWith('http://') || url.startsWith('https://') ||
  50.       url.startsWith('ftp://') || url.startsWith('chrome-extension://');
  51. };
  52.  
  53. /**
  54.  * Sets CSS class for |element| corresponding to the current platform.
  55.  * @param {Element} element The element to set the current platform.
  56.  */
  57. utils.setPlatformClass = function(element) {
  58.   element.classList.toggle(
  59.       CLASSES.WIN, navigator.userAgent.indexOf('Windows') > -1);
  60.   element.classList.toggle(
  61.       CLASSES.MAC, navigator.userAgent.indexOf('Mac') > -1);
  62. };
Parsed in 0.021 seconds