|
44 | 44 | }; |
45 | 45 |
|
46 | 46 | const copyCID = () => { |
| 47 | + event.stopImmediatePropagation(); |
47 | 48 | const textarea = document.createElement('textarea'); |
48 | 49 | textarea.style.position = 'fixed'; |
49 | 50 | textarea.style.left = '-100%'; |
|
56 | 57 | }; |
57 | 58 |
|
58 | 59 | const copyCIDasHTML = () => { |
| 60 | + event.stopImmediatePropagation(); |
59 | 61 | const textarea = document.createElement('textarea'); |
60 | 62 | textarea.style.position = 'fixed'; |
61 | 63 | textarea.style.left = '-100%'; |
|
107 | 109 | if (hexcid && hexcid[1]) { |
108 | 110 | cid = BigInt(hexcid[1]).toString(10); |
109 | 111 | } else { |
| 112 | + console.log('No CID'); |
110 | 113 | return; |
111 | 114 | } |
112 | 115 | } |
|
122 | 125 | } |
123 | 126 | } |
124 | 127 |
|
125 | | - // Want to add a 'CID' button above the 'Your Maps activity' button. |
| 128 | + // Want to add a 'CID' button below the 'Address' button. |
126 | 129 | // CID button should have the same appearance and behavior as the phone number and plus code, |
127 | 130 | // but the class name for this may be obfuscated, so let's duplicate the dynamically appearing DOM. |
128 | 131 | // The reason for duplicating the DOM for addresses is that phone number or plus code may not be present. |
129 | | - const addressButton = document.body.querySelector('button[data-item-id="address"]'); |
130 | | - const addressBox = addressButton?.parentNode; |
| 132 | + |
| 133 | + // Find Address icon |
| 134 | + const mainElements = document.body.querySelectorAll('div[role="main"] *'); |
| 135 | + const addressIcon = Array.from(mainElements).filter(element => element.textContent === ""); // pin icon |
| 136 | + if (!addressIcon || addressIcon.length < 1) { |
| 137 | + // console.log('Address icon not found'); |
| 138 | + return; |
| 139 | + } |
| 140 | + // Find Address box and place info container |
| 141 | + let addressButton = addressIcon[0].parentElement; |
| 142 | + while (addressButton) { |
| 143 | + if (addressButton.tagName == 'BUTTON' || addressButton.getAttribute('role') == "button") { |
| 144 | + break; |
| 145 | + } |
| 146 | + addressButton = addressButton.parentElement; |
| 147 | + } |
| 148 | + const addressBox = addressButton?.parentElement; |
131 | 149 | const placeInfoDiv = addressBox?.parentNode; |
132 | | - const historyButton = placeInfoDiv?.querySelector('button[data-item-id="history"]'); |
133 | | - const historyBox = historyButton?.parentNode; |
134 | | - if (!historyBox) { |
| 150 | + if (!placeInfoDiv) { |
| 151 | + // if (!addressButton) { |
| 152 | + // console.log('Address button not found'); |
| 153 | + // } else if (!addressBox) { |
| 154 | + // console.log('Address box not found'); |
| 155 | + // } else { |
| 156 | + // console.log('Place info container not found'); |
| 157 | + // } |
135 | 158 | return; |
136 | 159 | } |
137 | 160 | // If the CID button has already been added, then do nothing. |
138 | | - if (placeInfoDiv.querySelector('#cidbox')) { |
| 161 | + if (placeInfoDiv?.querySelector('#cidbox')) { |
| 162 | + // console.log('CID box already exists'); |
139 | 163 | return; |
140 | 164 | } |
141 | 165 | // Build CID button. |
142 | 166 | cidButtonBox = addressBox.cloneNode(true); |
143 | 167 | cidButtonBox.setAttribute('id', 'cidbox'); |
144 | | - |
145 | | - const cidButton = cidButtonBox.querySelector('button:first-of-type'); |
| 168 | + let cidButtonIcon = null; |
| 169 | + let cidButtonText = null; |
| 170 | + let subbuttonsContainer = null; |
| 171 | + let style = ""; |
| 172 | + let cidButton = cidButtonBox.querySelector('button[data-item-id="address"]'); |
| 173 | + if (cidButton) { |
| 174 | + style = "spot"; |
| 175 | + cidButtonIcon = cidButton.querySelector('div>div>span.google-symbols'); |
| 176 | + cidButtonText = cidButton.querySelector('div>div>div.fontBodyMedium'); |
| 177 | + subbuttonsContainer = cidButton.nextSibling?.firstChild; |
| 178 | + } else { |
| 179 | + style = "building"; |
| 180 | + cidButton = cidButtonBox.querySelector('div[role="button"]'); |
| 181 | + cidButtonIcon = cidButton.querySelector('div>span.google-symbols'); |
| 182 | + cidButtonText = cidButton.querySelector('div>span>span'); |
| 183 | + subbuttonsContainer = cidButton.querySelector('div:last-child>span:last-child'); |
| 184 | + } |
146 | 185 | cidButton.removeAttribute('jsaction'); |
147 | 186 | cidButton.removeAttribute('jslog'); |
148 | 187 | cidButton.removeAttribute('aria-label'); |
149 | 188 | cidButton.removeAttribute('data-item-id'); |
| 189 | + cidButton.removeAttribute('data-section-id'); |
150 | 190 | cidButton.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_CID')); |
151 | 191 | cidButton.onclick = copyCID; |
152 | 192 | cidButton.addEventListener('mouseenter', mouseEnter); |
153 | 193 | cidButton.addEventListener('mouseleave', mouseLeave); |
154 | | - |
155 | | - const cidButtonIcon = cidButton.querySelector('div:first-of-type > div:nth-of-type(1) > span:first-of-type'); |
156 | | - cidButtonIcon.innerHTML = 'CID'; |
157 | | - cidButtonIcon.setAttribute('style', 'font-family: roboto; font-size: 15px; font-weight: bold; line-height: 24px; width: 24px; text-align: center;'); |
158 | | - |
159 | | - const cidButtonText = cidButton.querySelector('div:first-of-type > div:nth-of-type(2) > div:first-of-type'); |
160 | | - cidButtonText.innerHTML = cid; |
161 | | - cidButtonText.setAttribute('style', 'line-height: 24px;'); |
162 | | - |
| 194 | + if (cidButtonIcon) { |
| 195 | + cidButtonIcon.innerHTML = 'CID'; |
| 196 | + cidButtonIcon.setAttribute('style', 'font-family: roboto; font-size: 15px; font-weight: bold; line-height: 24px; width: 24px; text-align: center;'); |
| 197 | + } |
| 198 | + if (cidButtonText) { |
| 199 | + cidButtonText.innerHTML = cid; |
| 200 | + cidButtonText.setAttribute('style', 'line-height: 24px;'); |
| 201 | + } |
163 | 202 | // Add more sub-buttons. |
164 | | - const subbuttonsContainer = cidButton.nextSibling?.firstChild; |
165 | 203 | if (subbuttonsContainer) { |
166 | 204 | // For the first sub-button, assume it is a copy button and reuse it. |
167 | | - const button1Container = subbuttonsContainer.firstChild; |
168 | | - const button1 = button1Container?.firstChild; |
169 | | - if (button1) { |
170 | | - button1.setAttribute('jsaction', 'focus:pane.focusTooltip;blur:pane.blurTooltip'); |
171 | | - button1.removeAttribute('jsaction'); |
172 | | - button1.removeAttribute('jslog'); |
173 | | - button1.removeAttribute('aria-label'); |
174 | | - button1.removeAttribute('data-item-id'); |
175 | | - button1.removeAttribute('data-value'); |
176 | | - button1.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_CID')); |
177 | | - button1.onclick = copyCID; |
178 | | - button1.addEventListener('mouseenter', mouseEnter); |
179 | | - button1.addEventListener('mouseleave', mouseLeave); |
180 | | - } |
181 | | - // Remove the second and subsequent ones. |
182 | | - let bc = button1Container; |
183 | | - while ((bc = bc.nextSibling) != null) { |
184 | | - subbuttonsContainer.removeChild(bc); |
| 205 | + const button1Box = subbuttonsContainer.firstChild; |
| 206 | + let button1 = null; |
| 207 | + let button2 = null; |
| 208 | + if (button1Box) { |
| 209 | + button1 = style == "spot" ? button1Box.querySelector('button') : button1Box; |
| 210 | + if (button1) { |
| 211 | + button1.removeAttribute('jsaction'); |
| 212 | + button1.removeAttribute('jslog'); |
| 213 | + button1.removeAttribute('aria-label'); |
| 214 | + button1.removeAttribute('data-item-id'); |
| 215 | + button1.removeAttribute('data-section-id'); |
| 216 | + button1.removeAttribute('data-value'); |
| 217 | + button1.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_CID')); |
| 218 | + button1.onclick = copyCID; |
| 219 | + button1.addEventListener('mouseenter', mouseEnter); |
| 220 | + button1.addEventListener('mouseleave', mouseLeave); |
| 221 | + } |
| 222 | + // Remove the second and subsequent ones. |
| 223 | + let bc = button1Box; |
| 224 | + while ((bc = bc.nextSibling) != null) { |
| 225 | + subbuttonsContainer.removeChild(bc); |
| 226 | + } |
185 | 227 | } |
186 | 228 | // Add 'Copy as HTML code' button. |
187 | | - const button2Container = button1Container.cloneNode(true); |
188 | | - const button2 = button2Container?.firstChild; |
189 | | - if (button2) { |
190 | | - const button2Icon = button2.firstChild?.querySelector('span:first-of-type'); |
191 | | - if (button2Icon) { |
192 | | - button2Icon.innerHTML = 'URL'; |
193 | | - button2Icon.setAttribute('style', 'font-size: 10px; line-height: 18px; width: 18px; height: 18px;'); |
| 229 | + const button2Box = button1Box.cloneNode(true); |
| 230 | + if (button2Box) { |
| 231 | + let button2Icon = null; |
| 232 | + if (style == "spot") { |
| 233 | + button2 = button2Box.querySelector('button'); |
| 234 | + button2Icon = button2.firstChild; |
| 235 | + } else { |
| 236 | + button2 = button2Box; |
| 237 | + button2Icon = button2; |
| 238 | + } |
| 239 | + if (button2) { |
| 240 | + button2Icon = button2Icon?.querySelector('span:first-of-type'); |
| 241 | + if (button2Icon) { |
| 242 | + button2Icon.innerHTML = 'URL'; |
| 243 | + button2Icon.setAttribute('style', 'font-size: 10px; line-height: 18px; width: 18px; height: 18px;'); |
| 244 | + } |
| 245 | + button2.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_URL')); |
| 246 | + button2.onclick = copyCIDasHTML; |
| 247 | + button2.addEventListener('mouseenter', mouseEnter); |
| 248 | + button2.addEventListener('mouseleave', mouseLeave); |
194 | 249 | } |
195 | | - button2.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_URL')); |
196 | | - button2.onclick = copyCIDasHTML; |
197 | | - button2.addEventListener('mouseenter', mouseEnter); |
198 | | - button2.addEventListener('mouseleave', mouseLeave); |
| 250 | + subbuttonsContainer.insertBefore(button2Box, null); |
| 251 | + } |
| 252 | + // Fix Google glitch. |
| 253 | + if (style == "building") { |
| 254 | + subbuttonsContainer.setAttribute('style', 'direction: rtl; margin-right: 20px;'); |
| 255 | + button1?.setAttribute('style', 'padding: 0;'); |
| 256 | + button2?.setAttribute('style', 'padding: 0;'); |
199 | 257 | } |
200 | | - subbuttonsContainer.insertBefore(button2Container, null); |
201 | 258 | } |
202 | | - |
203 | 259 | // Add the CID button to the page. |
204 | | - placeInfoDiv.insertBefore(cidButtonBox, historyBox); |
| 260 | + placeInfoDiv.insertBefore(cidButtonBox, addressBox.nextElementSibling); |
205 | 261 | }; |
206 | 262 |
|
207 | 263 | /* |
|
0 commit comments