Skip to content

Commit

Permalink
[wikidata] Fix ISNI crashing and WP parsing (#103)
Browse files Browse the repository at this point in the history
Crash bugs (2) reported and pin-pointed by DenizC and diskotechjam in

https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099

---

- WD is Wikidata
- WP is Wikipedia

---

fillISNI function crash
-----------------------

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

`edit-artist.isni_codes-template` no longer exists in react version of
artist edit page.

fillFormFromISNI function crash
-------------------------------

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

When regex does not match, it is `null`.

Fill ISNI code from WD
----------------------

This does not work on the new react version of the artist edit page.

Reported by @zas

Fetch WD page from WP URL
-------------------------

It does no longer find WD page from WP URL.
Probably WP pages have changed.

Reported by diskotechjam

---

Bonus: Use only UTC time functions everywhere, for consistency
  • Loading branch information
jesus2099 authored and loujine committed Nov 26, 2024
1 parent 5e1cc20 commit 37df091
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 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 2024.11.25
// @version 2024.11.26
// @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 @@ -260,7 +260,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 @@ -362,29 +362,35 @@ function setValue(nodeId, value, callback) {

function fillISNI(isni) {
const existing_isni = [];
const isniBlock = document.getElementById(
'add-isni-code').parentElement.parentElement;
const fields = isniBlock.getElementsByTagName('input');
for (const input of fields) {
existing_isni.push(input.value.split(' ').join(''));
}
existing_isni.splice(0, 1); // skip template
if (existing_isni.includes(isni.split(' ').join(''))) {
return;
}
if (existing_isni.length === 1 && existing_isni[0] === '') {
document.getElementsByName('edit-artist.isni_codes.0')[0].value = isni;
const isni_fields = document.querySelectorAll('input[name^="edit-artist.isni_codes."]');
for (const input of isni_fields) {
if (input.value) {
existing_isni.push(input.value.split(' ').join(''));
}
}
if (existing_isni.length === 0) {
(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(isni_fields[0]), 'value').set).call(isni_fields[0], isni);
isni_fields[0].dispatchEvent(new Event('input', {bubbles: true}));
$('#newFields').append(
$('<dt>', {'text': 'New ISNI code added:'})
).append(
$('<dd>', {'text': isni}).css('color', 'green')
);
} else {
isniBlock.getElementsByClassName('form-row-add')[0]
.getElementsByTagName('button')[0].click();
document.getElementsByName(
`edit-artist.isni_codes.${existing_isni.length}`)[0].value = isni;
$('#newFields').append(
$('<dt>', {'text': `Fields "ISNI codes":`})
);
if (existing_isni.includes(isni.split(' ').join(''))) {
$('#newFields').append(
$('<dd>', {'text': `Kept "${isni}"`})
);
} else {
$('#newFields').append(
$('<dd>', {'text': `Different value "${isni}" suggested`}
).css('color', 'red')
);
}
}
$('#newFields').append(
$('<dt>', {'text': 'New ISNI code added:'})
).append(
$('<dd>', {'text': isni}).css('color', 'green')
);
}


Expand Down Expand Up @@ -664,7 +670,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 @@ -677,15 +683,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 Expand Up @@ -730,11 +736,9 @@ $(document).ready(function () {
GM_xmlhttpRequest({
method: "GET",
url: node.value,
timeout: 1000,
onload: function(resp) {
const parser = new DOMParser();
const doc = parser.parseFromString(resp.responseText, 'text/html');
const link = doc.querySelector('#p-tb a[href*="www.wikidata.org"]');
const doc = (new DOMParser()).parseFromString(resp.responseText, 'text/html');
const link = doc.querySelector('li > a[href^="https://www.wikidata.org/wiki/Special:EntityPage/Q"]');
fillExternalLinks(link.href);
fillFormFromWikidata(link.href.match(/\/(Q\d+)\b/)[1]);
}
Expand Down

0 comments on commit 37df091

Please sign in to comment.