changes I needed to make: (and not to make on wikidata yet)
made the code a bit better, using EntityInitializer and data model js, and adjusted the css selectors for the gadget to work with both the existing deployed code and master. (e.g. the gadget could be updated now)
I do have some concerns though that the gadget is (via api request) finding all (of the referenced properties) that have P1630, to construct PROPERTIES array. It might be better performance wise if this list was cached (maybe maintained as json on wiki by a bot).
https://www.wikidata.org/w/index.php?title=User:Aude/authority.js&oldid=200606764 are the minimal changes needed.
this leaves the issue there of "references and qualifiers were not being linked unless the given reference property or qualifier property was already used somewhere in the item as a main snak property" but think that would be better solved by having a bot maintain the PROPERTIES array, cached on js/json page on wiki that the gadget can use.