Skip to content

Commit 0408a54

Browse files
committed
Extend CID display to buildings etc.
1 parent 0eb534d commit 0408a54

2 files changed

Lines changed: 108 additions & 52 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "__MSG_name__",
55
"description": "__MSG_desc__",
6-
"version": "1.1.1",
6+
"version": "1.1.2",
77
"homepage_url": "https://github.com/itagagaki/CopyCID",
88
"author": "Itagaki Fumihiko",
99
"default_locale": "en",

page.js

Lines changed: 107 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
};
4545

4646
const copyCID = () => {
47+
event.stopImmediatePropagation();
4748
const textarea = document.createElement('textarea');
4849
textarea.style.position = 'fixed';
4950
textarea.style.left = '-100%';
@@ -56,6 +57,7 @@
5657
};
5758

5859
const copyCIDasHTML = () => {
60+
event.stopImmediatePropagation();
5961
const textarea = document.createElement('textarea');
6062
textarea.style.position = 'fixed';
6163
textarea.style.left = '-100%';
@@ -107,6 +109,7 @@
107109
if (hexcid && hexcid[1]) {
108110
cid = BigInt(hexcid[1]).toString(10);
109111
} else {
112+
console.log('No CID');
110113
return;
111114
}
112115
}
@@ -122,86 +125,139 @@
122125
}
123126
}
124127

125-
// Want to add a 'CID' button above the 'Your Maps activity' button.
128+
// Want to add a 'CID' button below the 'Address' button.
126129
// CID button should have the same appearance and behavior as the phone number and plus code,
127130
// but the class name for this may be obfuscated, so let's duplicate the dynamically appearing DOM.
128131
// 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;
131149
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+
// }
135158
return;
136159
}
137160
// 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');
139163
return;
140164
}
141165
// Build CID button.
142166
cidButtonBox = addressBox.cloneNode(true);
143167
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+
}
146185
cidButton.removeAttribute('jsaction');
147186
cidButton.removeAttribute('jslog');
148187
cidButton.removeAttribute('aria-label');
149188
cidButton.removeAttribute('data-item-id');
189+
cidButton.removeAttribute('data-section-id');
150190
cidButton.setAttribute('data-tooltip', chrome.i18n.getMessage('copy_CID'));
151191
cidButton.onclick = copyCID;
152192
cidButton.addEventListener('mouseenter', mouseEnter);
153193
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+
}
163202
// Add more sub-buttons.
164-
const subbuttonsContainer = cidButton.nextSibling?.firstChild;
165203
if (subbuttonsContainer) {
166204
// 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+
}
185227
}
186228
// 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);
194249
}
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;');
199257
}
200-
subbuttonsContainer.insertBefore(button2Container, null);
201258
}
202-
203259
// Add the CID button to the page.
204-
placeInfoDiv.insertBefore(cidButtonBox, historyBox);
260+
placeInfoDiv.insertBefore(cidButtonBox, addressBox.nextElementSibling);
205261
};
206262

207263
/*

0 commit comments

Comments
 (0)