User:Dixtosa/editAndGo.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// documentation: [[Wiktionary:Grease_pit/2016/April#Editable title]]

$(function() {
    if (mw.config.values.wgAction != "view") return;
    $("#firstHeading").prop("contenteditable", "true");
    $('#firstHeading').keypress(function(e) {
        var key = e.which;
        let newTitle = $('#firstHeading').contents().get(0).textContent;
        if (key == 13) {
            window.location = "/w/index.php?search=" + newTitle + "&title=Special:Search&go=Go";
            return false;
        }
    });
});

mw.loader.using(['jquery.ui'], function() {
    var ctrl = $("#firstHeading");
    ctrl.autocomplete({
        source: function(request, response) {
        	var prefix = ctrl.contents().get(0).textContent;
            new mw.Api().get({
                action: 'opensearch',
                search: prefix
            }).done(function(data) {
                response(data[1]);
            });
        },
        focus: function() {
            return false;
        },
        select: function(e, ui) {
            window.location.href = "/wiki/" + ui.item.value;
            return false;
        },
        open: function() {
            $(".ui-autocomplete")
                .css({})
                .position({
                    my: "right top",
                    at: "right bottom",
                    of: ctrl,
                    offset: "0",
                    collision: 'none fit'
                })
                .find('li').css({});
        }
    });
});