Chrome Autofill covers Autocomplete for Google Maps API v3

后端 未结 21 1052
我在风中等你
我在风中等你 2021-02-01 00:57

I am using Google Maps Javascript v3 to setup autocomplete on an HTML input field like so:

http://imgur.com/Rm6X2FI.png - (w/out autofill)

The issue I\'m having

21条回答
  •  粉色の甜心
    2021-02-01 01:24

    None of the above answers worked for me (Chrome 64.0.3282.186, x64 windows).

    TL;DR: The Google Maps code is changing the autocomplete attribute to off, so even if you set it to new-password, you'll still get autofill. The hack I'm using is to listen for mutations on that attribute and then override it. Note: simply changing the attribute after calling into Google Maps does not work.

    Set up a MutationObserver before initializing Google Maps Autocomplete that immediately stops listening for mutations and then sets the attribute to new-password.

        var autocompleteInput = document.getElementById("id-of-element");
    
        var observerHack = new MutationObserver(function() {
            observerHack.disconnect();
            $("#id-of-element").attr("autocomplete", "new-password");
        });
    
        observerHack.observe(autocompleteInput, {
            attributes: true,
            attributeFilter: ['autocomplete']
        });
    

提交回复
热议问题