User Details on Hover

Show user details on hover

< 腳本User Details on Hover的回應

提問/評論

§
發表於:2023-11-13

Hello! I am a developer for an open-source project called ListenBrainz (https://github.com/metabrainz/listenbrainz-server). We have recently seen some strange errors accumulating in our error reporting software, with "/userscript.html?name=User-Details-on-Hover.user.js" showing as the source.

After some searching I found this userscript to be the source of the error. If I understand correctly this userscript is meant to be used in conjunction with Lemmy, and the first thing the script does is check whether it should apply to the current page. However, the check in question throws an error which is then reported by our reporting software.

Running the unmodified const isLemmy = document.head.querySelector("[name~=Description][content]").content === "Lemmy"; on a random page causes Uncaught TypeError: Cannot read properties of null (reading 'content').

However, using the optional chaining ?. before trying to access .content ensures the code doesn't throw an error:

const isLemmy = document.head.querySelector("[name~=Description][content]")?.content === "Lemmy";

Alternatively, if the optional chaining operator is too recent a feature, the boolean can be broken down like so:

const targetElement = document.head.querySelector("[name~=Description][content]");
const isLemmy = targetElement && targetElement.content === "Lemmy";

發表回覆

登入以回覆