User:Abbe98/AutoIdentifierInput.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
if (typeof ($) === 'undefined') $ = jQuery;
mw.loader.using('oojs-ui-core').done(function () {
mw.hook('wikibase.entityPage.entityLoaded').add(function (entity) {
console.log('it\'s alive!');
console.log(entity);
if (mw.config.get('wgNamespaceNumber') !== 0 || !mw.config.exists('wbEntityId') || !mw.config.get('wbIsEditView')) {
return;
}
console.log('passed test');
initiate(entity);
});
});
function initiate(entity) {
// The "abbe" prefix is there to avoid identifier collisions
var fieldset = new OO.ui.FieldsetLayout({
label: 'Auto Identifier Input'
});
fieldset.addItems([
new OO.ui.ActionFieldLayout(
new OO.ui.TextInputWidget({
placeholder: 'https://example.com/external-identifier',
inputId: 'abbe_identifier_input'
}),
new OO.ui.ButtonWidget({
label: 'Add',
id: 'abbe_submit_identifier',
flags: [
'primary',
'progressive'
]
}),
{
label: 'Input the full URL for a given identifier.',
align: 'top'
}
)
]);
$('#bodyContent').before(fieldset.$element);
$('#abbe_submit_identifier').click(function () { submitIdentifier(entity) });
$('#abbe_identifier_input').keydown(function (e) {
if (e.which == 13) {
submitIdentifier(entity);
}
});
}
function submitIdentifier(entity) {
const props = [
['YouTube', 'https://www.youtube.com/channel/', 'P2397'],
['LinkedIn', 'https://www.linkedin.com/company/', 'P4264'],
['Facebook', 'https://www.facebook.com/', 'P2013'],
['Twitter', 'https://twitter.com/', 'P2002'],
['Instagram', 'https://www.instagram.com/', 'P2003'],
['Github', 'https://github.com/', 'P2037'],
['Vimeo', 'https://vimeo.com/', 'P4015'],
['Flickr', 'https://www.flickr.com/photos/', 'P3267'],
['BnF', 'https://catalogue.bnf.fr/ark:/12148/cb', 'P268'],
['IdRef', 'https://www.idref.fr/', 'P269'],
['Gallica', 'https://gallica.bnf.fr/ark:/12148/', 'P4258'],
['SUDOC', 'https://www.sudoc.fr/', 'P1025'],
['MusicBrainz work', 'https://musicbrainz.org/work/', 'P435'],
];
var inputValue = $('#abbe_identifier_input').val();
if (!inputValue) return;
// check for input match
var matchedProp;
props.forEach(function (prop) {
if (inputValue.startsWith(prop[1])) {
matchedProp = prop;
}
});
if (!matchedProp) {
mw.notify('Failed to match the URL to a property.');
return;
}
// check if prop is present on item already
if (entity.claims[matchedProp[2]]) {
mw.notify('A value for ' + matchedProp[0] + ' is already present.');
return;
}
// clean input (remove formatter url, trim whitespace, remove trailing slash)
finalValue = inputValue.replace(matchedProp[1], '').trim().replace(/\/$/, '');
var baseRevID = entity.lastrevid;
var api = new mw.Api();
var summary = '';
var change = {
'claims': [
{
'mainsnak':
{
'snaktype': 'value',
'property': matchedProp[2],
'datavalue': {
'value': finalValue,
'type': 'string'
}
},
'type': 'statement',
'rank': 'normal'
}
]
};
api.postWithToken('csrf', {
action: 'wbeditentity',
baserevid: baseRevID,
id: entity.id,
data: JSON.stringify(change),
summary: summary
}).done(function (data) {
if (data.success === 1) {
mw.notify('Added ' + matchedProp[0] + ' identifier!', { type: 'success' });
$('#abbe_identifier_input').val('');
return;
} else {
mw.notify('Faild to add statement.', { type: 'error' });
console.log(data);
}
}).fail(function (data) {
mw.notify('Faild to add statement.', { type: 'error' });
console.log(data);
});
}