What I'd like to do is to create a userscript to convert different GPS coordinates to a hyperlink taking to Google Maps.
我想要做的是创建一个用户脚本,将不同的GPS坐标转换为一个带到谷歌地图的超链接。
Example:
I have a page with 24.197611 N, 120.780512 E
as text. I want to be able to click that to open another page leading to http://maps.google.com/maps?q=24.197611,120.780512
我有一个页面,其中包含24.197611 N,120.780512 E作为文本。我希望能够点击它以打开另一个页面,该页面指向http://maps.google.com/maps?q=24.197611,120.780512
Since the coordinates are inside brackets (14.495569 N, 9.139927 E), I used something like this:
由于坐标在括号内(14.495569 N,9.139927 E),我使用了这样的东西:
$("div").html(function(i, html) {
return html.replace(/\((.+?)\)/g, "<a href='http://maps.google.com/maps?q=#$1'>$1</a>");
});
But the link also gets the bold part.
但链接也是大胆的部分。
2 个解决方案
#1
0
I suggest that you use the following regex to match the coordinates in the format that you use:
我建议您使用以下正则表达式匹配您使用的格式的坐标:
\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)
In this case you have three capturing groups. Capturing group #2 matches the coordinates in the format Google Maps hyperlink uses.
在这种情况下,您有三个捕获组。捕获组#2匹配Google Maps超链接使用格式的坐标。
Here is how it would work with the code that you provided:
以下是它如何与您提供的代码一起使用:
$("div").html(function(i, html) {
return html.replace(/\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)/g, "<a href='http://maps.google.com/maps?q=#$2'>$2</a>");
});
#2
0
So, at last I used something like this whick works
所以,最后我使用了类似这样的东西
function myFunction() {
$("div").each(function(i, html) {
var $this = $(this);
$this.html($this.html().replace(/((\d+\.\d+) [N], (\d+\.\d+) [E])/g, "<a href='http://maps.google.com/maps?q=$1' target='_blank'>$1</a>"));
});
}
$(document).ready(myFunction);
setTimeout(myFunction, 2000);
Even if it seems to slowdown things
即使它似乎放慢了事情
#1
0
I suggest that you use the following regex to match the coordinates in the format that you use:
我建议您使用以下正则表达式匹配您使用的格式的坐标:
\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)
In this case you have three capturing groups. Capturing group #2 matches the coordinates in the format Google Maps hyperlink uses.
在这种情况下,您有三个捕获组。捕获组#2匹配Google Maps超链接使用格式的坐标。
Here is how it would work with the code that you provided:
以下是它如何与您提供的代码一起使用:
$("div").html(function(i, html) {
return html.replace(/\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)/g, "<a href='http://maps.google.com/maps?q=#$2'>$2</a>");
});
#2
0
So, at last I used something like this whick works
所以,最后我使用了类似这样的东西
function myFunction() {
$("div").each(function(i, html) {
var $this = $(this);
$this.html($this.html().replace(/((\d+\.\d+) [N], (\d+\.\d+) [E])/g, "<a href='http://maps.google.com/maps?q=$1' target='_blank'>$1</a>"));
});
}
$(document).ready(myFunction);
setTimeout(myFunction, 2000);
Even if it seems to slowdown things
即使它似乎放慢了事情