User:Lectrician1/AddStatement.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.
// This script allows you to automatically add a statement manually through the interface. That way you don't need to refresh the page after making an API call instead to add a statement. The script is not completely done and can only add statements whose properties have an Item value. I'm was working on other datatypes like MonolingualText but I'm a bit stuck and am working on User:Lectrician1/discographies.js right now instead.

// To use the script, import the script into your userscript by adding `importScript('User:Lectrician1/AddStatement.js')` and call the function addStatement wherever you need it.

function addStatment(propertyID, valueID) {
    // If property is already on page
    if ($(`#${propertyID}`).length) {
        $(`#${propertyID} > .wikibase-statementlistview`).statementlistview('enterNewItem').then(() => {
            // Observe snakview value since this element exists before value input is loaded
            const targetNodes = $('.wikibase-snakview-value');

            // Options for the observer (which mutations to observe)
            const config = { attributes: false, childList: true, subtree: true };

            // Callback function to execute when mutations are observed
            const callback = function (mutationList, observer) {
                // Use traditional 'for loops' for IE 11
                for (const mutation of mutationList) {

                    if (mutation.type === 'childList' && $('.valueview-expert-wikibaseitem-input').length) {

                        observer.disconnect()
                        var valueInput = $('.valueview-expert-wikibaseitem-input').eq(0)

                        valueInput.val(valueID)

                        valueInput.entityselector('search').then(() => {

                            var valueSearchList = $('.ui-ooMenu').eq(1);
                            var valueTopResult = valueSearchList.children('.ui-ooMenu-item').eq(0)

                            $('.valueview').on('valueviewchange', () => {
                                $('.wikibase-toolbar-button-save > a').trigger('click')
                            })

                            valueSearchList.ooMenu('activate', valueTopResult)
                            valueSearchList.ooMenu('select')

                        })

                    }
                }
            };

            // Create an observer instance linked to the callback function
            const observer = new MutationObserver(callback);

            // Start observing the target node for configured mutations
            targetNodes.each(function () {
                observer.observe(this, config);
            });
        })
    }
    else {
        $('.wikibase-statementgrouplistview').eq(0).statementgrouplistview('enterNewItem').then(() => {
            var propertyInput = $('.wikibase-snakview-property > .ui-suggester-input');

            propertyInput.val(propertyID)

            propertyInput.entityselector('search').then(() => {
                var propertySearchList = $('.ui-ooMenu:not(.wikibase-entitysearch-list)');
                var propertyTopResult = $('.ui-ooMenu:not(.wikibase-entitysearch-list) > .ui-ooMenu-item').eq(0)

                propertySearchList.ooMenu('activate', propertyTopResult)
                propertySearchList.ooMenu('select')

                // Observe snakview value since this element exists before value input is loaded
                const targetNodes = $('.wikibase-snakview-value');

                // Options for the observer (which mutations to observe)
                const config = { attributes: false, childList: true, subtree: true };

                // Callback function to execute when mutations are observed
                const callback = function (mutationList, observer) {
                    // Use traditional 'for loops' for IE 11
                    for (const mutation of mutationList) {

                        if (mutation.type === 'childList' && $('.valueview-expert-wikibaseitem-input').length) {

                            observer.disconnect()
                            var valueInput = $('.valueview-expert-wikibaseitem-input').eq(0)

                            valueInput.val(valueID)

                            valueInput.entityselector('search').then(() => {

                                var valueSearchList = $('.ui-ooMenu:not(.wikibase-entitysearch-list)').eq(1);
                                var valueTopResult = $('.ui-ooMenu:not(.wikibase-entitysearch-list)').eq(1).children('.ui-ooMenu-item').eq(0)

                                $('.valueview').on('valueviewchange', () => {
                                    $('.wikibase-toolbar-button-save > a').trigger('click')
                                })

                                valueSearchList.ooMenu('activate', valueTopResult)
                                valueSearchList.ooMenu('select')

                            })

                        }
                    }
                };

                // Create an observer instance linked to the callback function
                const observer = new MutationObserver(callback);

                // Start observing the target node for configured mutations
                targetNodes.each(function () {
                    observer.observe(this, config);
                });


            })
        })
    }
}