|
| 1 | +/** |
| 2 | + * Amazon book pages: a button next to the title that opens the same book on |
| 3 | + * Goodreads. Isolated world, shadow DOM, textContent only. The page is never |
| 4 | + * parsed as HTML we did not write (invariant 11). |
| 5 | + */ |
| 6 | + |
| 7 | +import { extractIsbn, goodreadsUrlForIsbn, isAmazonProductPath } from '../lib/amazon-book'; |
| 8 | + |
| 9 | +const HOST_ID = 'bunnylol-goodreads'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Copied by hand from design/tokens.css, the same way go.html does. This sheet |
| 13 | + * lives in a shadow root on amazon.com, so the extension's tokens.css is not |
| 14 | + * on the page, and Inter is not loaded there either. The sand fill and dark |
| 15 | + * label are the brand pair that already meets contrast on a white Amazon page. |
| 16 | + */ |
| 17 | +const SHADOW_CSS = ` |
| 18 | +:host { all: initial; } |
| 19 | +.wrap { display: block; margin: 8px 0 12px; } |
| 20 | +button { |
| 21 | + font: 600 13px/1.3 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, system-ui, sans-serif; |
| 22 | + background: #e1ab76; |
| 23 | + color: #1a1a1a; |
| 24 | + border: 0; |
| 25 | + border-radius: 6px; |
| 26 | + padding: 6px 12px; |
| 27 | + cursor: pointer; |
| 28 | + display: inline-flex; |
| 29 | + align-items: center; |
| 30 | + gap: 6px; |
| 31 | +} |
| 32 | +button:hover { background: #d69b5d; } |
| 33 | +button:focus-visible { outline: 2px solid #895420; outline-offset: 2px; } |
| 34 | +svg { width: 14px; height: 14px; flex: none; } |
| 35 | +`; |
| 36 | + |
| 37 | +let mountedFor: string | null = null; |
| 38 | +let debounce = 0; |
| 39 | +let lastHref = location.href; |
| 40 | + |
| 41 | +function refresh(): void { |
| 42 | + if (location.href !== lastHref) { |
| 43 | + lastHref = location.href; |
| 44 | + unmount(); |
| 45 | + mountedFor = null; |
| 46 | + } |
| 47 | + |
| 48 | + if (!isAmazonProductPath(location.pathname)) { |
| 49 | + unmount(); |
| 50 | + mountedFor = null; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (mountedFor && document.getElementById(HOST_ID)) return; |
| 55 | + |
| 56 | + const isbn = extractIsbn(document.documentElement.innerHTML); |
| 57 | + if (!isbn) { |
| 58 | + unmount(); |
| 59 | + mountedFor = null; |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const url = goodreadsUrlForIsbn(isbn); |
| 64 | + unmount(); |
| 65 | + mount(url); |
| 66 | + mountedFor = url; |
| 67 | +} |
| 68 | + |
| 69 | +function schedule(): void { |
| 70 | + window.clearTimeout(debounce); |
| 71 | + debounce = window.setTimeout(refresh, 120); |
| 72 | +} |
| 73 | + |
| 74 | +function mount(url: string): void { |
| 75 | + const host = document.createElement('div'); |
| 76 | + host.id = HOST_ID; |
| 77 | + const shadow = host.attachShadow({ mode: 'open' }); |
| 78 | + |
| 79 | + const style = document.createElement('style'); |
| 80 | + style.textContent = SHADOW_CSS; |
| 81 | + |
| 82 | + const wrap = document.createElement('div'); |
| 83 | + wrap.className = 'wrap'; |
| 84 | + |
| 85 | + const button = document.createElement('button'); |
| 86 | + button.type = 'button'; |
| 87 | + button.setAttribute('aria-label', 'View this book on Goodreads (BunnyLol)'); |
| 88 | + button.title = 'Open the Goodreads page for this book'; |
| 89 | + button.append(bookIcon(), document.createTextNode('View on Goodreads')); |
| 90 | + button.addEventListener('click', (event) => { |
| 91 | + event.preventDefault(); |
| 92 | + event.stopPropagation(); |
| 93 | + location.assign(url); |
| 94 | + }); |
| 95 | + |
| 96 | + wrap.append(button); |
| 97 | + shadow.append(style, wrap); |
| 98 | + |
| 99 | + const target = mountPoint(); |
| 100 | + if (target) { |
| 101 | + target.insertAdjacentElement('afterend', host); |
| 102 | + return; |
| 103 | + } |
| 104 | + host.style.position = 'fixed'; |
| 105 | + host.style.top = '72px'; |
| 106 | + host.style.right = '16px'; |
| 107 | + host.style.zIndex = '2147483646'; |
| 108 | + document.documentElement.append(host); |
| 109 | +} |
| 110 | + |
| 111 | +function unmount(): void { |
| 112 | + document.getElementById(HOST_ID)?.remove(); |
| 113 | +} |
| 114 | + |
| 115 | +function mountPoint(): Element | null { |
| 116 | + return ( |
| 117 | + document.querySelector('#productTitle') ?? |
| 118 | + document.querySelector('#title') ?? |
| 119 | + document.querySelector('#titleSection') |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +function bookIcon(): SVGSVGElement { |
| 124 | + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 125 | + svg.setAttribute('viewBox', '0 0 16 16'); |
| 126 | + svg.setAttribute('aria-hidden', 'true'); |
| 127 | + svg.setAttribute('focusable', 'false'); |
| 128 | + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
| 129 | + path.setAttribute('d', 'M3 2.5h6.5A2.5 2.5 0 0 1 12 5v8.5H5.5A2.5 2.5 0 0 0 3 11V2.5zm0 0V11'); |
| 130 | + path.setAttribute('fill', 'none'); |
| 131 | + path.setAttribute('stroke', 'currentColor'); |
| 132 | + path.setAttribute('stroke-width', '1.5'); |
| 133 | + path.setAttribute('stroke-linecap', 'round'); |
| 134 | + path.setAttribute('stroke-linejoin', 'round'); |
| 135 | + svg.append(path); |
| 136 | + return svg; |
| 137 | +} |
| 138 | + |
| 139 | +function boot(): void { |
| 140 | + refresh(); |
| 141 | + const observer = new MutationObserver(schedule); |
| 142 | + observer.observe(document.documentElement, { childList: true, subtree: true }); |
| 143 | +} |
| 144 | + |
| 145 | +if (document.readyState === 'loading') { |
| 146 | + document.addEventListener('DOMContentLoaded', boot, { once: true }); |
| 147 | +} else { |
| 148 | + boot(); |
| 149 | +} |
0 commit comments