Skip to content

Commit

Permalink
[wikidata] Fix ISNI functions crash
Browse files Browse the repository at this point in the history
fillISNI function
-----------------

> Uncaught TypeError: Cannot read properties of undefined (reading 'parentElement')

edit-artist.isni_codes-template apparently no longer exists.


fillFormFromISNI function
-------------------------

> Uncaught TypeError: Cannot read properties of null (reading 'length')

When regex does not match, it is null.

---

Bugs reported by DenizC and diskotechjam in
https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099

---

Bonus: Use only UTC time functions everywhere, for consistency
  • Loading branch information
jesus2099 committed Oct 24, 2024
1 parent ab70378 commit 3598809
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions mb-edit-create_from_wikidata.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @name MusicBrainz edit: Create entity or fill data from wikipedia / wikidata / VIAF / ISNI
// @namespace mbz-loujine
// @author loujine
// @version 2023.3.11
// @version 2024.10.24
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-create_from_wikidata.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-edit-create_from_wikidata.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
Expand Down Expand Up @@ -248,7 +248,7 @@ class WikiDataHelpers {
}
return;
}
setValue(prefix + '.year', date.getFullYear());
setValue(prefix + '.year', date.getUTCFullYear());
const yearInput = document.getElementById(prefix + '.year');
if (!yearInput) {
return;
Expand Down Expand Up @@ -350,8 +350,8 @@ function setValue(nodeId, value, callback) {

function fillISNI(isni) {
const existing_isni = [];
const isniBlock = document.getElementsByClassName(
'edit-artist.isni_codes-template')[0].parentElement;
const isniBlock = document.querySelector(
'input[name="edit-artist.isni_codes.0"]').closest('.row');
const fields = isniBlock.getElementsByTagName('input');
for (const input of fields) {
existing_isni.push(input.value.split(' ').join(''));
Expand Down Expand Up @@ -652,7 +652,7 @@ function fillFormFromISNI(isniURL) {
onload: resp => {
fillISNI(isniURL.split('/')[3]);
let rgx = new RegExp(`href="(.*?musicbrainz.org.*?)"`).exec(resp.responseText);
if (rgx.length) {
if (rgx) {
// eslint-disable-next-line no-alert
if (window.confirm(
'An entity already exists linked to this ISNI id, ' +
Expand All @@ -665,15 +665,15 @@ function fillFormFromISNI(isniURL) {
'catalogue.bnf.fr', 'd-nb.info', 'wikidata.org', 'id.loc.gov', 'viaf.org'
]) {
rgx = new RegExp(`href="(.*?${site}.*?)"`).exec(resp.responseText);
if (rgx.length) {
if (rgx) {
fillExternalLinks(rgx[1]);
}
}

rgx = new RegExp(
/Name:.*?<psi:text>(.*?)<\/psi:text>/
).exec(resp.responseText.replace(/\n/g, ''));
if (rgx.length) {
if (rgx) {
_fillEntityName(rgx[1], entityType);
}
},
Expand Down

0 comments on commit 3598809

Please sign in to comment.