ವಿಷಯಕ್ಕೆ ಹೋಗು

ಮೀಡಿಯವಿಕಿ:Gadget-NewWordGadget.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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Check if the script is already loaded
if (!window.NewWordGadgetLoaded) {
    window.NewWordGadgetLoaded = true;

    $(function () {
        // Append the input box and create button to the content area
        $('#content').prepend(`
            <div id="new-word-tool" style="margin: 1em 0; padding: 1em; border: 1px solid #ddd;">
                <label for="new-word-input"><b>ಹೊಸ ಪುಟವನ್ನು ರಚಿಸಿ:</b></label>
                <input type="text" id="new-word-input" style="margin-left: 10px; padding: 5px;" placeholder="ಹೊಸ ಪದವನ್ನು ಟೈಪ್ ಮಾಡಿ" />
                <button id="new-word-create" style="margin-left: 10px;">ರಚಿಸಿ</button>
                <div id="new-word-suggestions" style="margin-top: 10px;"></div>
            </div>
        `);

        // Add autocomplete functionality
        $('#new-word-input').on('input', function () {
            const query = $(this).val();
            if (query.length > 1) {
                $.getJSON(mw.util.wikiScript('api'), {
                    action: 'query',
                    list: 'allpages',
                    apprefix: query,
                    aplimit: 10,
                    format: 'json'
                }).done(function (data) {
                    const suggestions = data.query.allpages.map(page => page.title);
                    if (suggestions.length > 0) {
                        const suggestionsHTML = suggestions
                            .map(title => `<a href="${mw.util.getUrl(title)}" target="_blank">${title}</a>`)
                            .join(', ');
                        $('#new-word-suggestions').html(`ಅಸ್ತಿತ್ವದಲ್ಲಿರುವ ಪದಗಳು: ${suggestionsHTML}`);
                    } else {
                        $('#new-word-suggestions').html('ಅಸ್ತಿತ್ವದಲ್ಲಿರುವ ಪದಗಳು ಕಂಡುಬಂದಿಲ್ಲ.');
                    }
                });
            } else {
                $('#new-word-suggestions').html('');
            }
        });

        // Create button click handler
        $('#new-word-create').on('click', function () {
            const newWord = $('#new-word-input').val().trim();
            if (newWord) {
                // Redirect to the new page with preload and editintro parameters
                const preloadTemplate = 'Template:WordPagePreload'; // Preload template
                const editIntroTemplate = 'Template:WordPagePreload/documentation/1'; // Edit intro template
                const url = mw.util.getUrl(newWord, {
                    action: 'edit',
                    preload: preloadTemplate,
                    editintro: editIntroTemplate // Add editintro parameter
                });
                window.location.href = url;
            } else {
                alert('ದಯವಿಟ್ಟು ಪದವನ್ನು ನಮೂದಿಸಿ.');
            }
        });
    });
}