User:Jeeputer/specialRandomPage.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Jeeputer/specialRandomPage. |
/**
* Finds a random page in a selected namespace with preferred options
**/
mw.loader.using(["oojs-ui-core", "oojs-ui-windows", "oojs-ui-widgets"], function() {
var namespaces = [{
name: 'All',
ns: '0|6|10|14|118|828'
}, {
name: '(Article)',
ns: 0
}, {
name: 'File',
ns: 6
}, {
name: 'Template',
ns: 10
}, {
name: 'Category',
ns: 14
}, {
name: 'Draft',
ns: 118
}, {
name: 'Module',
ns: 828
}];
var SPRPFieldSet = new OO.ui.FieldsetLayout({
align: 'inline'
});
var dropDown = new OO.ui.DropdownWidget({
menu: {
items: namespaces.map(function(item) {
return new OO.ui.MenuOptionWidget({
data: item.ns,
label: item.name
});
})
}
});
dropDown.getMenu().selectItemByLabel(window.SPRPpreferredNamespace || 'All');
var redirect = new OO.ui.ToggleButtonWidget({
icon: 'articleRedirect',
title: 'Random redirect',
value: window.SPRPActivateRedirect || false
}),
edit = new OO.ui.ToggleButtonWidget({
icon: 'edit',
title: 'Open the edit form',
value: window.SPRPAlwaysEdit || false
}),
links = new OO.ui.ToggleButtonWidget({
icon: 'link',
title: 'See what links to the page',
value: window.SPRPActivateWLH || false
});
options = new OO.ui.ButtonGroupWidget({
items: [redirect, edit, links]
});
SPRPFieldSet.addItems([
new OO.ui.FieldLayout(dropDown, {
align: 'top'
}),
new OO.ui.FieldLayout(options, {
align: 'top'
})
]);
function SPRPProcessDialog(config) {
SPRPProcessDialog.super.call(this, config);
}
OO.inheritClass(SPRPProcessDialog, OO.ui.ProcessDialog);
SPRPProcessDialog.static.name = 'SPRPDialog';
SPRPProcessDialog.static.title = 'Random page';
SPRPProcessDialog.static.actions = [{
action: 'save',
label: 'Go',
flags: 'primary'
}, {
label: 'Cancel',
flags: 'safe'
}];
SPRPProcessDialog.prototype.initialize = function() {
SPRPProcessDialog.super.prototype.initialize.apply(this, arguments);
this.content = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
this.content.$element.append(SPRPFieldSet.$element);
this.$body.append(this.content.$element);
};
SPRPProcessDialog.prototype.getActionProcess = function(action) {
var dialog = this;
if (action) {
mw.notify('Finding a random page...');
var actionParam, WLH = false,
redir = false;
if (redirect.getValue()) {
redir = true;
}
if (links.getValue()) {
WLH = true;
actionParam = 'view';
} else {
actionParam = edit.getValue() ? 'edit' : 'view';
}
return new OO.ui.Process(function() {
var ns = dropDown.getMenu().findSelectedItem();
var r = redir ? 'redirects' : 'nonredirects';
new mw.Api().get({
action: 'query',
format: 'json',
list: 'random',
rnlimit: '1',
rnnamespace: ns.data,
rnfilterredir: r
}).done(function(data) {
var title = data.query.random[0].title;
var url = mw.config.get('wgServer');
if (WLH) {
url += mw.util.getUrl('Special:WhatLinksHere/' + title);
} else {
url += mw.util.getUrl(title, {
redirect: redir ? 'no' : 'yes',
action: actionParam
})
if (actionParam == 'edit') {
url += '#editform'
}
}
window.location.href = url;
});
dialog.close({
action: action
});
});
}
return SPRPProcessDialog.super.prototype.getActionProcess.call(this, action);
};
var windowManager = new OO.ui.WindowManager();
$(mw.util.addPortletLink('p-cactions', '#', 'Random Page', 'ca-sprp', 'Go to a random page in a selected namespace', window.SPRPAccesskey || 'z')).on('click', function(e) {
e.preventDefault();
$(document.body).append(windowManager.$element);
var dialog = new SPRPProcessDialog();
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);
});
});