Inserting a comma into text copied from a website

Viewed 241

I often encounter an annoying phenomenon. I select an address in the browser (Firefox):

enter image description here
Example from yelp.com

Copy it into my adress bar, using the !maps DuckDuckGo bang:

enter image description here

We immediately see the problem: house number and zip code do not get separated, and more often than not Google Maps then fails to find the address (unsurprisingly).

The code in this case is:

<address>
    Hans-Sachs-Str. 8<br>80469 Munich<br>Germany
</address>

I can imagine similar code with <p> or <div> instead of address; the relevant part seems to be how <br> will be copy-pasted.

The following is also conceivable:

<tr><td>Hans-Sachs-Str. 8</td></tr>
<tr><td>80469 Munich</td></tr>

Which copies in just the same way.

So if I put an address on my website (or write a user style), how can I make addresses copy-paste in a helpful way, e.g. with a comma between the individual fields/lines?

Answers with HTML5 and CSS3 are fine, and current browsers can be required; I don't care about backwards compatibility. Answers without Javascript are preferred.


I tried br::before { content: ", "; } but that didn't work.

3 Answers

I could only do it with JS, you will need to inject with greasemonkey or some other extension.

var commaHtml = '<span class="insertedComma">, </span>';
document.querySelectorAll('address').forEach(function(a){
  a.querySelectorAll('br').forEach(function(b){
    b.insertAdjacentHTML('beforebegin', commaHtml);
  });
});
.insertedComma {
   color: red;
   /*opacity: 0; uncomment this line to make red commas disappear*/
}
<address>
    Hans-Sachs-Str. 8<br>
    80469 Munich<br>
    Germany
</address>

Hack #1: Include an invisible comma.

<address>
      Hans-Sachs-Str. 8<span style="opacity: 0;">,</span><br>
      80469    Munich
</address>

This is actually quite unintrusive since it won't even show up in the selection, only in the pasted text.

Disadvantage: can't insert this automatically into existing websites without JS. (Or can I?)

If you insert presentational content using CSS pseudo-elements (::before and ::after), you will not be able to copy that content onto the clipboard.

So, I think you will have to insert any commas using javascript and DOM manipulation.

Here is an alternative javascript approach:

var address = document.getElementsByTagName('address')[0];
var addressLines = address.querySelectorAll('.strasse, .stadt, .land');

for (var i = 0; i < (addressLines.length - 1); i++) {

    var comma = document.createElement('span');
    comma.classList.add('comma');
    comma.textContent = ',';
    address.insertBefore(comma, addressLines[i]);
    address.insertBefore(addressLines[i], comma);

}
span {
display: inline-block;
float: left;
}

.comma + span {
clear: left;
}
<address>
<span class="strasse">Hans-Sachs-Str. 8</span>
<span class="stadt">80469 Munich</span>
<span class="land">Germany</span>
</address>

Related