|
| 1 | +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +export class SPIDebugPanel extends HTMLElement { |
| 16 | + connectedCallback() { |
| 17 | + // Debug console is Hidden by default. Make it visble by typing: |
| 18 | + // `localStorage.setItem('spiDebug', 'true');` |
| 19 | + // into the browser console. |
| 20 | + if (localStorage.getItem('spiDebug') === 'true') { |
| 21 | + this.classList.remove('hidden') |
| 22 | + this.addCanonicalUrls() |
| 23 | + } |
| 24 | + |
| 25 | + this.querySelector('.buttons > .hide').addEventListener('click', () => { |
| 26 | + this.classList.add('hidden') |
| 27 | + }) |
| 28 | + |
| 29 | + this.querySelector('.buttons > .disable').addEventListener('click', () => { |
| 30 | + this.classList.add('hidden') |
| 31 | + localStorage.setItem('spiDebug', 'false') |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + disconnectedCallback() { |
| 36 | + console.log('SPIDebugPanel disconnectedCallback') |
| 37 | + } |
| 38 | + |
| 39 | + newTableRow(title, value) { |
| 40 | + const rowElement = document.createElement('tr') |
| 41 | + |
| 42 | + const titleCellElement = document.createElement('td') |
| 43 | + titleCellElement.innerText = title |
| 44 | + |
| 45 | + const valueCellElement = document.createElement('td') |
| 46 | + valueCellElement.innerText = value |
| 47 | + |
| 48 | + rowElement.appendChild(titleCellElement) |
| 49 | + rowElement.appendChild(valueCellElement) |
| 50 | + return rowElement |
| 51 | + } |
| 52 | + |
| 53 | + addCanonicalUrls() { |
| 54 | + const tableElement = this.querySelector('table') |
| 55 | + const canonicalUrl = document.querySelector('link[rel="canonical"]').href |
| 56 | + const windowUrl = window.location.href |
| 57 | + |
| 58 | + tableElement.appendChild(this.newTableRow('Canonical URL', canonicalUrl)) |
| 59 | + tableElement.appendChild(this.newTableRow('Window URL', windowUrl)) |
| 60 | + tableElement.appendChild(this.newTableRow('URLs Match', canonicalUrl === windowUrl)) |
| 61 | + } |
| 62 | +} |
0 commit comments