Commit caa12fd5 authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Add $() and use it for getting elements by ID.

This allows DOM Distiller's JS code to be more consistent with other
Chromium code.

Bug: 1027612
Change-Id: I837f00e68eada2b5ff0f3026b725497ac8c44a41
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1954705
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Auto-Submit: Aran Gilman <gilmanmh@google.com>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729468}
parent 77b87d27
...@@ -9,10 +9,18 @@ if (typeof distillerOnIos === 'undefined') { ...@@ -9,10 +9,18 @@ if (typeof distillerOnIos === 'undefined') {
distillerOnIos = false; distillerOnIos = false;
} }
// The style guide recommends preferring $() to getElementById(). Chrome's
// standard implementation of $() is imported from chrome://resources, which the
// distilled page is prohibited from accessing. A version of it is
// re-implemented here to allow stylistic consistency with other JS code.
function $(id) {
return document.getElementById(id);
}
function addToPage(html) { function addToPage(html) {
const div = document.createElement('div'); const div = document.createElement('div');
div.innerHTML = html; div.innerHTML = html;
document.getElementById('content').appendChild(div); $('content').appendChild(div);
fillYouTubePlaceholders(); fillYouTubePlaceholders();
} }
...@@ -42,15 +50,12 @@ function fillYouTubePlaceholders() { ...@@ -42,15 +50,12 @@ function fillYouTubePlaceholders() {
} }
function showLoadingIndicator(isLastPage) { function showLoadingIndicator(isLastPage) {
document.getElementById('loading-indicator').className = $('loading-indicator').className = isLastPage ? 'hidden' : 'visible';
isLastPage ? 'hidden' : 'visible';
} }
// Sets the title. // Sets the title.
function setTitle(title) { function setTitle(title) {
const holder = document.getElementById('title-holder'); $('title-holder').textContent = title;
holder.textContent = title;
document.title = title; document.title = title;
} }
...@@ -94,7 +99,7 @@ function updateToolbarColor(theme) { ...@@ -94,7 +99,7 @@ function updateToolbarColor(theme) {
} else { } else {
toolbarColor = '#F5F5F5'; toolbarColor = '#F5F5F5';
} }
document.getElementById('theme-color').content = toolbarColor; $('theme-color').content = toolbarColor;
} }
function useFontScaling(scaling) { function useFontScaling(scaling) {
...@@ -161,8 +166,7 @@ class FontSizeSlider { ...@@ -161,8 +166,7 @@ class FontSizeSlider {
} }
const fontSizeSlider = new FontSizeSlider( const fontSizeSlider = new FontSizeSlider(
document.querySelector('#font-size-selection'), $('font-size-selection'), [14, 15, 16, 18, 20, 24, 28, 32, 40, 48]);
[14, 15, 16, 18, 20, 24, 28, 32, 40, 48]);
updateToolbarColor(getClassFromElement(document.body, themeClasses)); updateToolbarColor(getClassFromElement(document.body, themeClasses));
maybeSetWebFont(); maybeSetWebFont();
...@@ -272,7 +276,7 @@ class Pincher { ...@@ -272,7 +276,7 @@ class Pincher {
this.restoreCenter_(); this.restoreCenter_();
let img = document.getElementById('fontscaling-img'); let img = $('fontscaling-img');
if (!img) { if (!img) {
img = document.createElement('img'); img = document.createElement('img');
img.id = 'fontscaling-img'; img.id = 'fontscaling-img';
...@@ -432,9 +436,9 @@ class Pincher { ...@@ -432,9 +436,9 @@ class Pincher {
const pincher = new Pincher; const pincher = new Pincher;
document.querySelector('#settings-toggle').addEventListener('click', (e) => { $('settings-toggle').addEventListener('click', (e) => {
const dialog = document.querySelector('#settings-dialog'); const dialog = $('settings-dialog');
const toggle = document.querySelector('#settings-toggle'); const toggle = $('settings-toggle');
if (dialog.open) { if (dialog.open) {
toggle.classList.remove('activated'); toggle.classList.remove('activated');
dialog.close(); dialog.close();
...@@ -444,17 +448,15 @@ document.querySelector('#settings-toggle').addEventListener('click', (e) => { ...@@ -444,17 +448,15 @@ document.querySelector('#settings-toggle').addEventListener('click', (e) => {
} }
}); });
document.querySelector('#close-settings-button') $('close-settings-button').addEventListener('click', (e) => {
.addEventListener('click', (e) => { $('settings-toggle').classList.remove('activated');
document.querySelector('#settings-toggle').classList.remove('activated'); $('settings-dialog').close();
document.querySelector('#settings-dialog').close(); });
});
document.querySelector('#theme-selection').addEventListener('change', (e) => { $('theme-selection').addEventListener('change', (e) => {
useTheme(e.target.value); useTheme(e.target.value);
}); });
document.querySelector('#font-family-selection') $('font-family-selection').addEventListener('change', (e) => {
.addEventListener('change', (e) => { useFontFamily(e.target.value);
useFontFamily(e.target.value); });
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment