|
| 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 | + |
| 23 | + this.reset() |
| 24 | + this.addButtons() |
| 25 | + this.addCanonicalUrls() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + disconnectedCallback() { |
| 30 | + console.log('SPIDebugPanel disconnectedCallback') |
| 31 | + } |
| 32 | + |
| 33 | + reset() { |
| 34 | + const buttonsContainer = this.querySelector('.buttons') |
| 35 | + if (buttonsContainer) buttonsContainer.remove() |
| 36 | + |
| 37 | + const dynamicRowElements = this.querySelectorAll('tr:not([server-side])') |
| 38 | + dynamicRowElements.forEach((row) => row.remove()) |
| 39 | + } |
| 40 | + |
| 41 | + addButtons() { |
| 42 | + const hideButton = document.createElement('button') |
| 43 | + hideButton.innerText = 'Hide' |
| 44 | + hideButton.title = 'Temporarily hide this panel' |
| 45 | + hideButton.addEventListener('click', () => { |
| 46 | + this.classList.add('hidden') |
| 47 | + }) |
| 48 | + |
| 49 | + const disableButton = document.createElement('button') |
| 50 | + disableButton.innerText = 'Disable' |
| 51 | + disableButton.title = 'Disable the debug panel' |
| 52 | + disableButton.addEventListener('click', () => { |
| 53 | + localStorage.setItem('spiDebug', 'false') |
| 54 | + this.classList.add('hidden') |
| 55 | + }) |
| 56 | + |
| 57 | + const buttonsContainer = document.createElement('div') |
| 58 | + buttonsContainer.classList.add('buttons') |
| 59 | + buttonsContainer.appendChild(hideButton) |
| 60 | + buttonsContainer.appendChild(disableButton) |
| 61 | + this.prepend(buttonsContainer) |
| 62 | + } |
| 63 | + |
| 64 | + newTableRow(title, value, valueCssClass) { |
| 65 | + const tableElement = this.querySelector('table > tbody') |
| 66 | + const rowElement = document.createElement('tr') |
| 67 | + |
| 68 | + const titleCellElement = document.createElement('td') |
| 69 | + titleCellElement.innerText = title |
| 70 | + |
| 71 | + const valueSpanElement = document.createElement('span') |
| 72 | + valueSpanElement.innerText = value |
| 73 | + const valueCellElement = document.createElement('td') |
| 74 | + valueCellElement.appendChild(valueSpanElement) |
| 75 | + if (valueCssClass) valueCellElement.classList.add(valueCssClass) |
| 76 | + |
| 77 | + rowElement.appendChild(titleCellElement) |
| 78 | + rowElement.appendChild(valueCellElement) |
| 79 | + tableElement.appendChild(rowElement) |
| 80 | + } |
| 81 | + |
| 82 | + addCanonicalUrls() { |
| 83 | + const canonicalUrl = document.querySelector('link[rel="canonical"]').href |
| 84 | + const windowUrl = window.location.href |
| 85 | + const matchingCanonicalUrl = canonicalUrl === windowUrl |
| 86 | + |
| 87 | + this.newTableRow('Canonical URL', canonicalUrl) |
| 88 | + this.newTableRow('Window URL', windowUrl) |
| 89 | + this.newTableRow('Canonical Match', matchingCanonicalUrl, matchingCanonicalUrl ? 'green' : 'red') |
| 90 | + } |
| 91 | +} |
0 commit comments