MediaWiki:Common.js: Difference between revisions
(console logging) |
(more specific selector) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 24: | Line 24: | ||
buttonWidget.on( 'click', function () { | buttonWidget.on( 'click', function () { | ||
var wikidataId = $('#wikidataInput').val().trim(); | var wikidataId = $('#wikidataInput input').val().trim(); | ||
var url = "http://www.wikidata.org/entity/" + wikidataId; | var url = "http://www.wikidata.org/entity/" + wikidataId; | ||
Line 34: | Line 34: | ||
property: 'P1', | property: 'P1', | ||
snaktype: 'value', | snaktype: 'value', | ||
value: JSON.stringify( | value: JSON.stringify(url) | ||
} ).done( function ( data ) { | } ).done( function ( data ) { | ||
// If successful | // If successful | ||
location.reload(); // Refresh the page | location.reload(); // Refresh the page | ||
} ).fail( function () { | } ).fail( function ( jqXHR, textStatus, errorThrown ) { | ||
// If failed | // If failed | ||
console.log(jqXHR); | console.log(jqXHR); |
Latest revision as of 01:08, 8 July 2023
// Quick-add Wikidata ID
// Check if we're on an Item: page
if (mw.config.get('wgNamespaceNumber') === 120) {
// Import OOUI
mw.loader.using( 'oojs-ui' ).done( function () {
var fieldLayout, formPanel, fieldset;
// Create a FieldLayout
fieldLayout = new OO.ui.FieldLayout( new OO.ui.TextInputWidget( {
id: 'wikidataInput',
placeholder: 'Q123'
} ), {
label: 'Quick-add Wikidata ID',
align: 'top'
} );
// Create a button widget
var buttonWidget = new OO.ui.ButtonWidget( {
label: 'Submit',
icon: 'check',
flags: [ 'primary', 'progressive' ],
} );
buttonWidget.on( 'click', function () {
var wikidataId = $('#wikidataInput input').val().trim();
var url = "http://www.wikidata.org/entity/" + wikidataId;
// Post action
new mw.Api().postWithToken( 'csrf', {
action: 'wbcreateclaim',
format: 'json',
entity: mw.config.get('wgTitle'),
property: 'P1',
snaktype: 'value',
value: JSON.stringify(url)
} ).done( function ( data ) {
// If successful
location.reload(); // Refresh the page
} ).fail( function ( jqXHR, textStatus, errorThrown ) {
// If failed
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
alert( 'Failed to create a new statement.' );
} );
} );
// Add the field layout and button to a fieldset
fieldset = new OO.ui.FieldsetLayout();
fieldset.addItems( [ fieldLayout, buttonWidget ] );
// Add the fieldset to a panel
formPanel = new OO.ui.PanelLayout( { padded: false, expanded: false } );
formPanel.$element.append( fieldset.$element );
// Add the form to the page
$( '.wikibase-entitytermsview-entitytermsforlanguagelistview' ).append( formPanel.$element );
});
}