Skip to content

Commit

Permalink
Update worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Adonis142857 authored Sep 17, 2024
1 parent 1097bec commit dfba964
Showing 1 changed file with 127 additions and 21 deletions.
148 changes: 127 additions & 21 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function handleRequest(request) {
const country = searchParams.get('country') || getRandomCountry()
let address, name, gender, phone

for (let i = 0; i < 20; i++) {
for (let i = 0; i < 100; i++) {
const location = getRandomLocationInCountry(country)
const apiUrl = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${location.lat}&lon=${location.lng}&zoom=18&addressdetails=1`

Expand Down Expand Up @@ -126,9 +126,33 @@ const html = `
display: none;
}
.subtitle-small {
font-size: 1.2em; /* 可以根据需要调整大小 */
font-size: 1.2em;
margin-bottom: 20px;
}
}
.saved-addresses {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.saved-addresses th, .saved-addresses td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
.saved-addresses th {
background-color: #f2f2f2;
}
.delete-btn {
padding: 5px 10px;
background-color: #dc3545;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
.delete-btn:hover {
background-color: #c82333;
}
</style>
</head>
<body>
Expand All @@ -142,6 +166,7 @@ const html = `
<div class="phone" onclick="copyToClipboard('${phone.replace(/[()\s-]/g, '')}')">${phone}</div>
<div class="address" onclick="copyToClipboard('${address}')">${address}</div>
<button class="refresh-btn" onclick="window.location.reload();">Get Another Address 获取新地址</button>
<button class="refresh-btn" onclick="saveAddress();">Save Address 保存地址</button>
<div class="country-select">
<label for="country">Select country, new address will be generated automatically after checking the box</label><br>
<span>选择国家,在勾选后将自动生成新地址</span>
Expand All @@ -150,7 +175,22 @@ const html = `
</select>
</div>
<iframe class="map" src="https://www.google.com/maps?q=${encodeURIComponent(address)}&output=embed"></iframe>
</div>
<table class="saved-addresses" id="savedAddressesTable">
<thead>
<tr>
<th>操作 Operation</th>
<th>备注 Notes</th>
<th>姓名 Name</th>
<th>性别 Gender</th>
<th>电话号码 Phone number</th>
<th>地址 Address</th>
</tr>
</thead>
<tbody>
<!-- 动态生成的内容 -->
</tbody>
</table>
</div>
<div class="footer">
Original version by chatgpt.org.uk, modified by Adonis142857 | <a href="https://github.com/Adonis142857/Real-Address-Generator" target="_blank"><img src="https://pic.imgdb.cn/item/66e7ab36d9c307b7e9cefd24.png" alt="GitHub" style="width: 20px; height: 20px; vertical-align: middle; position: relative; top: -3px;"></a>
</div>
Expand All @@ -169,6 +209,59 @@ const html = `
function changeCountry(country) {
window.location.href = \`?country=\${country}\`
}
function saveAddress() {
const note = prompt('请输入备注(可以留空)| Please enter a note (can be left blank)') || '';
const savedAddresses = JSON.parse(localStorage.getItem('savedAddresses') || '[]');
const newEntry = {
note: note,
name: '${name}',
gender: '${gender}',
phone: '${phone.replace(/[()\\s-]/g, '')}',
address: '${address}'
};
savedAddresses.push(newEntry);
localStorage.setItem('savedAddresses', JSON.stringify(savedAddresses));
renderSavedAddresses();
}
// 渲染保存的地址
function renderSavedAddresses() {
const savedAddresses = JSON.parse(localStorage.getItem('savedAddresses') || '[]');
const tbody = document.getElementById('savedAddressesTable').getElementsByTagName('tbody')[0];
tbody.innerHTML = '';
savedAddresses.forEach((entry, index) => {
const row = tbody.insertRow();
const deleteCell = row.insertCell();
const noteCell = row.insertCell();
const nameCell = row.insertCell();
const genderCell = row.insertCell();
const phoneCell = row.insertCell();
const addressCell = row.insertCell();
// 删除按钮
const deleteBtn = document.createElement('button');
deleteBtn.textContent = '删除 Delete';
deleteBtn.className = 'delete-btn';
deleteBtn.onclick = () => {
savedAddresses.splice(index, 1);
localStorage.setItem('savedAddresses', JSON.stringify(savedAddresses));
renderSavedAddresses();
};
deleteCell.appendChild(deleteBtn);
noteCell.textContent = entry.note;
nameCell.textContent = entry.name;
genderCell.textContent = entry.gender;
phoneCell.textContent = entry.phone;
addressCell.textContent = entry.address;
});
}
// 页面加载时渲染已保存的地址
window.onload = function() {
renderSavedAddresses();
};
</script>
</body>
</html>
Expand All @@ -188,23 +281,25 @@ function getRandomLocationInCountry(country) {
"FR": [{ lat: 48.8566, lng: 2.3522 }, { lat: 45.7640, lng: 4.8357 }],
"DE": [{ lat: 52.5200, lng: 13.4050 }, { lat: 48.1351, lng: 11.5820 }],
"CN": [{ lat: 39.9042, lng: 116.4074 }, { lat: 31.2304, lng: 121.4737 }],
"TW": [{ lat: 25.0330, lng: 121.5654 }, { lat: 22.6273, lng: 120.3014 }],
"HK": [{ lat: 22.3193, lng: 114.1694 },{ lat: 22.3964, lng: 114.1095 }],
"JP": [{ lat: 35.6895, lng: 139.6917 }, { lat: 34.6937, lng: 135.5023 }],
"IN": [{ lat: 28.6139, lng: 77.2090 }, { lat: 19.0760, lng: 72.8777 }],
"AU": [{ lat: -33.8688, lng: 151.2093 }, { lat: -37.8136, lng: 144.9631 }], // Australia 澳大利亚
"BR": [{ lat: -23.5505, lng: -46.6333 }, { lat: -22.9068, lng: -43.1729 }], // Brazil 巴西
"CA": [{ lat: 43.651070, lng: -79.347015 }, { lat: 45.501690, lng: -73.567253 }], // Canada 加拿大
"RU": [{ lat: 55.7558, lng: 37.6173 }, { lat: 59.9343, lng: 30.3351 }], // Russia 俄罗斯
"ZA": [{ lat: -33.9249, lng: 18.4241 }, { lat: -26.2041, lng: 28.0473 }], // South Africa 南非
"MX": [{ lat: 19.4326, lng: -99.1332 }, { lat: 20.6597, lng: -103.3496 }], // Mexico 墨西哥
"KR": [{ lat: 37.5665, lng: 126.9780 }, { lat: 35.1796, lng: 129.0756 }], // South Korea 韩国
"IT": [{ lat: 41.9028, lng: 12.4964 }, { lat: 45.4642, lng: 9.1900 }], // Italy 意大利
"ES": [{ lat: 40.4168, lng: -3.7038 }, { lat: 41.3851, lng: 2.1734 }], // Spain 西班牙
"TR": [{ lat: 41.0082, lng: 28.9784 }, { lat: 39.9334, lng: 32.8597 }], // Turkey 土耳其
"SA": [{ lat: 24.7136, lng: 46.6753 }, { lat: 21.3891, lng: 39.8579 }], // Saudi Arabia 沙特阿拉伯
"AR": [{ lat: -34.6037, lng: -58.3816 }, { lat: -31.4201, lng: -64.1888 }], // Argentina 阿根廷
"EG": [{ lat: 30.0444, lng: 31.2357 }, { lat: 31.2156, lng: 29.9553 }], // Egypt 埃及
"NG": [{ lat: 6.5244, lng: 3.3792 }, { lat: 9.0579, lng: 7.4951 }], // Nigeria 尼日利亚
"ID": [{ lat: -6.2088, lng: 106.8456 }, { lat: -7.7956, lng: 110.3695 }] // Indonesia 印度尼西亚
"AU": [{ lat: -33.8688, lng: 151.2093 }, { lat: -37.8136, lng: 144.9631 }],
"BR": [{ lat: -23.5505, lng: -46.6333 }, { lat: -22.9068, lng: -43.1729 }],
"CA": [{ lat: 43.651070, lng: -79.347015 }, { lat: 45.501690, lng: -73.567253 }],
"RU": [{ lat: 55.7558, lng: 37.6173 }, { lat: 59.9343, lng: 30.3351 }],
"ZA": [{ lat: -33.9249, lng: 18.4241 }, { lat: -26.2041, lng: 28.0473 }],
"MX": [{ lat: 19.4326, lng: -99.1332 }, { lat: 20.6597, lng: -103.3496 }],
"KR": [{ lat: 37.5665, lng: 126.9780 }, { lat: 35.1796, lng: 129.0756 }],
"IT": [{ lat: 41.9028, lng: 12.4964 }, { lat: 45.4642, lng: 9.1900 }],
"ES": [{ lat: 40.4168, lng: -3.7038 }, { lat: 41.3851, lng: 2.1734 }],
"TR": [{ lat: 41.0082, lng: 28.9784 }, { lat: 39.9334, lng: 32.8597 }],
"SA": [{ lat: 24.7136, lng: 46.6753 }, { lat: 21.3891, lng: 39.8579 }],
"AR": [{ lat: -34.6037, lng: -58.3816 }, { lat: -31.4201, lng: -64.1888 }],
"EG": [{ lat: 30.0444, lng: 31.2357 }, { lat: 31.2156, lng: 29.9553 }],
"NG": [{ lat: 6.5244, lng: 3.3792 }, { lat: 9.0579, lng: 7.4951 }],
"ID": [{ lat: -6.2088, lng: 106.8456 }, { lat: -7.7956, lng: 110.3695 }]
}
const coordsArray = countryCoordinates[country]
const randomCity = coordsArray[Math.floor(Math.random() * coordsArray.length)]
Expand All @@ -214,9 +309,10 @@ function getRandomLocationInCountry(country) {
}

function formatAddress(address, country) {
return `${address.house_number} ${address.road}, ${address.city || address.town}, ${address.postcode}, ${country}`
return `${address.house_number} ${address.road}, ${address.city || address.town || address.village}, ${address.postcode || ''}, ${country}`;
}


function getRandomPhoneNumber(country) {
const phoneFormats = {
"US": () => {
Expand Down Expand Up @@ -245,6 +341,14 @@ function getRandomPhoneNumber(country) {
const number = Array.from({ length: 8 }, () => Math.floor(Math.random() * 10)).join('')
return `+86 ${prefix} ${number}`
},
"TW": () => {
const number = Array.from({ length: 8 }, () => Math.floor(Math.random() * 10)).join('');
return `+886 9${number}`;
},
"HK": () => {
const number = Array.from({ length: 8 }, () => Math.floor(Math.random() * 10)).join('');
return `+852 ${number}`;
},
"JP": () => {
const areaCode = Math.floor(10 + Math.random() * 90).toString()
const number = Array.from({ length: 8 }, () => Math.floor(Math.random() * 10)).join('')
Expand Down Expand Up @@ -336,7 +440,7 @@ function getRandomPhoneNumber(country) {
}

function getRandomCountry() {
const countries = ["US", "UK", "FR", "DE", "CN", "JP", "IN", "AU", "BR", "CA", "RU", "ZA", "MX", "KR", "IT", "ES", "TR", "SA", "AR", "EG", "NG", "ID"]
const countries = ["US", "UK", "FR", "DE", "CN", "TW", "HK", "JP", "IN", "AU", "BR", "CA", "RU", "ZA", "MX", "KR", "IT", "ES", "TR", "SA", "AR", "EG", "NG", "ID"]
return countries[Math.floor(Math.random() * countries.length)]
}

Expand All @@ -347,6 +451,8 @@ function getCountryOptions(selectedCountry) {
{ name: "France 法国", code: "FR" },
{ name: "Germany 德国", code: "DE" },
{ name: "China 中国", code: "CN" },
{ name: "Taiwan 中国台湾", code: "TW" },
{ name: "Hong Kong 中国香港", code: "HK" },
{ name: "Japan 日本", code: "JP" },
{ name: "India 印度", code: "IN" },
{ name: "Australia 澳大利亚", code: "AU" },
Expand Down

0 comments on commit dfba964

Please sign in to comment.