-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGoogle Maps POI Collector (Better Status & UI).user.js
More file actions
171 lines (147 loc) · 5.79 KB
/
Copy pathGoogle Maps POI Collector (Better Status & UI).user.js
File metadata and controls
171 lines (147 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ==UserScript==
// @name Google Maps POI Collector (Better Status & UI)
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Collect POIs with persistent storage, visual status card, and safer UI buttons on Google Maps.
// @author rix4uni
// @match https://www.google.com/maps/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=googlemaps.com
// @grant none
// ==/UserScript==
(function () {
'use strict';
let lastUrl = location.href;
const STORAGE_KEY = 'poi_csv_data';
const csvHeader = ['Image', 'Coordinates', 'Name', 'HName', 'Stars', 'Reviews', 'Category', 'Address', 'Timings'];
const getStoredData = () => {
const json = localStorage.getItem(STORAGE_KEY);
return json ? new Map(JSON.parse(json)) : new Map();
};
const saveDataToStorage = (dataMap) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify([...dataMap.entries()]));
};
let collectedData = getStoredData();
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const generateKey = (name, address) => `${name.trim()} | ${address.trim()}`;
const updateCard = (isSaved) => {
const card = document.getElementById('poi-status-card');
if (!card) return;
const label = card.querySelector('#poi-status-label');
label.innerText = isSaved ? '✅ Already Saved' : '❌ Not Saved Yet';
label.style.color = isSaved ? '#0a9b41' : '#d93025';
const count = collectedData.size;
card.querySelector('#csv-download-btn').innerText = `⬇️ Download All CSV (${count})`;
};
const createStatusCard = () => {
if (document.getElementById('poi-status-card')) return;
const card = document.createElement('div');
card.id = 'poi-status-card';
card.innerHTML = `
<div id="poi-status-label" style="margin-bottom: 12px; font-weight: bold;">❌ Not Saved Yet</div>
<button id="csv-download-btn">⬇️ Download All CSV (0)</button>
<button id="clear-csv-btn">🗑️ Delete All Data</button>
`;
Object.assign(card.style, {
position: 'fixed',
bottom: '20px',
right: '20px',
zIndex: '9999',
background: '#fff',
padding: '16px',
borderRadius: '12px',
boxShadow: '0 4px 12px rgba(0,0,0,0.3)',
width: '240px',
fontFamily: 'system-ui, sans-serif',
fontSize: '14px',
color: '#333',
display: 'flex',
flexDirection: 'column',
gap: '10px'
});
const downloadBtn = card.querySelector('#csv-download-btn');
Object.assign(downloadBtn.style, {
background: '#1a73e8',
color: 'white',
border: 'none',
padding: '10px',
borderRadius: '6px',
cursor: 'pointer',
fontWeight: 'bold'
});
downloadBtn.onclick = () => {
const csvRows = [csvHeader.join(',')];
for (let row of collectedData.values()) {
csvRows.push(row.map(field => `"${field}"`).join(','));
}
const blob = new Blob([csvRows.join('\n')], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'GoogleMaps_POIs.csv';
a.click();
URL.revokeObjectURL(url);
};
const clearBtn = card.querySelector('#clear-csv-btn');
Object.assign(clearBtn.style, {
background: '#d93025',
color: 'white',
border: 'none',
padding: '10px',
borderRadius: '6px',
cursor: 'pointer',
fontWeight: 'bold'
});
clearBtn.onclick = () => {
if (confirm('Are you sure you want to delete all saved POI data?')) {
collectedData.clear();
localStorage.removeItem(STORAGE_KEY);
updateCard(false);
alert('✅ All POI data deleted!');
}
};
document.body.appendChild(card);
};
const collectData = async () => {
await wait(100); // Give time for POI to load
const name = document.querySelector('h1.DUwDvf.lfPIob')?.innerText || '';
const address = document.querySelector('.Io6YTe.fontBodyMedium.kR99db.fdkmkc')?.innerText || '';
const key = generateKey(name, address);
const alreadySaved = collectedData.has(key);
updateCard(alreadySaved); // Always update status FIRST
if (!name || !address || alreadySaved) return;
try {
const image = document.querySelector('button.aoRNLd.kn2E5e.NMjTrf.lvtCsd img')?.src || '';
const coordinates = location.href.match(/@([-.\d]+),([-.\d]+)/)?.slice(1, 3).join(',') || '';
const hName = document.querySelector('h2.bwoZTb.fontBodyMedium')?.innerText || '';
const stars = document.querySelector('.F7nice span span[aria-hidden="true"]')?.innerText || '';
const reviews = document.querySelector('.F7nice span span span[aria-label$="reviews"]')?.innerText.replace(/[()]/g, '') || '';
const category = document.querySelector('button.DkEaL')?.innerText || '';
const timings = [...document.querySelectorAll("table.eK4R0e tr")]
.map(row => {
const day = row.querySelector("td.ylH6lf div")?.innerText?.trim();
const hours = row.querySelector("td.mxowUb li")?.innerText?.trim();
return day && hours ? `${day}: ${hours}` : null;
})
.filter(Boolean)
.join('\\n');
const row = [image, coordinates, name, hName, stars, reviews, category, address, timings];
collectedData.set(key, row);
saveDataToStorage(collectedData);
updateCard(true);
console.log(`✅ Saved: ${name} (${address})`);
} catch (err) {
console.error('❌ Failed to collect POI:', err);
}
};
// Detect URL changes and collect data
setInterval(() => {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
if (/\/place\//.test(currentUrl)) {
createStatusCard();
collectData();
}
}
}, 100);
})();