From 00f5b768e23ed469d4c55638135196a2886aa4db Mon Sep 17 00:00:00 2001 From: jesus2099 Date: Wed, 27 Nov 2024 00:08:46 +0100 Subject: [PATCH] [wikidata] Fix ISNI crashing and WP parsing (#103) 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 --- mb-edit-create_from_wikidata.user.js | 62 +++++++++++++++------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/mb-edit-create_from_wikidata.user.js b/mb-edit-create_from_wikidata.user.js index 05f7344..7f4303d 100644 --- a/mb-edit-create_from_wikidata.user.js +++ b/mb-edit-create_from_wikidata.user.js @@ -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; @@ -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( + $('
', {'text': 'New ISNI code added:'}) + ).append( + $('
', {'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( + $('
', {'text': `Fields "ISNI codes":`}) + ); + if (existing_isni.includes(isni.split(' ').join(''))) { + $('#newFields').append( + $('
', {'text': `Kept "${isni}"`}) + ); + } else { + $('#newFields').append( + $('
', {'text': `Different value "${isni}" suggested`} + ).css('color', 'red') + ); + } } - $('#newFields').append( - $('
', {'text': 'New ISNI code added:'}) - ).append( - $('
', {'text': isni}).css('color', 'green') - ); } @@ -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, ' + @@ -677,7 +683,7 @@ 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]); } } @@ -685,7 +691,7 @@ function fillFormFromISNI(isniURL) { rgx = new RegExp( /Name:.*?(.*?)<\/psi:text>/ ).exec(resp.responseText.replace(/\n/g, '')); - if (rgx.length) { + if (rgx) { _fillEntityName(rgx[1], entityType); } }, @@ -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]); }