User:Harej/common.js

From Librarybase
< User:Harej
Revision as of 00:16, 8 July 2023 by Harej (talk | contribs) (Created page with "// 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: 'Wikidata item' } ), { label: 'Wikidata item', al...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
// 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: 'Wikidata item'
        } ), {
            label: 'Wikidata item',
            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').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({ "text": url, "type": "url" })
            } ).done( function ( data ) {
                // If successful
                location.reload(); // Refresh the page
            } ).fail( function () {
                // If failed
                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: true, expanded: false } );
        formPanel.$element.append( fieldset.$element );

        // Add the form to the page
        $( '#mw-content-text' ).prepend( formPanel.$element );
    });
}