Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wikidata] Fix ISNI crashing and WP parsing #103

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions mb-edit-create_from_wikidata.user.js
Original file line number Diff line number Diff line change
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