User:Dclemens1971/BLARtool.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:Dclemens1971/BLARtool. |
// <nowiki>
(function () {
if (mw.config.get('wgNamespaceNumber') !== 0) return;
const config = {
defaultNotification: "prompt", // "A" = Uw-blar, "B" = friendly, "prompt" = ask each time
autoLog: true,
autoWatchlist: true
};
mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
$(function () {
mw.util.addPortletLink(
'p-cactions',
'#',
'BLAR',
'ca-blar',
'Blank and redirect with {{R with history}}'
);
$('#ca-blar').on('click', async function (e) {
e.preventDefault();
if (!$('#blar-warning').length) {
$('#contentSub').prepend(`
<div id="blar-warning" style="background:#fff3cd;border:1px solid #ffeeba;padding:10px;margin:10px 0;color:#856404;">
⚠️ <strong>Please ensure you are familiar with the process at
<a href="/wiki/WP:BLAR" target="_blank">WP:BLAR</a> before using this script.</strong><br>
Do not blank and redirect a page if this action has already been done and contested; instead, discuss on the article talk page or open a
<a href="/wiki/WP:AFD" target="_blank">WP:AFD</a> discussion.<br>
You are responsible for any actions you take using this script.
</div>
`);
}
const isRedirect = mw.config.get('wgIsRedirect');
if (isRedirect) {
alert('This page is already a redirect. BLAR aborted.');
return;
}
let target = prompt('Enter the target page for the redirect (case-sensitive):');
if (target === null) return;
target = target.trim();
if (!target) return;
const pageName = mw.config.get('wgPageName');
const userName = mw.config.get('wgUserName');
const api = new mw.Api();
try {
// Fetch CSRF token
const token = await api.get({
action: 'query',
meta: 'tokens'
}).then(res => res.query.tokens.csrftoken);
// Get the page creator
const creator = await api.get({
action: 'query',
prop: 'revisions',
titles: pageName,
rvlimit: 1,
rvdir: 'newer',
rvprop: 'user'
}).then(res => {
const pages = res.query.pages;
return Object.values(pages)[0].revisions[0].user;
});
// Redirect target check
const redirectCheck = await api.get({
action: 'query',
titles: target,
redirects: 1
});
const redirectTo = (() => {
const pages = redirectCheck.query.pages;
const page = pages[Object.keys(pages)[0]];
return page.redirects ? page.redirects[0].title : null;
})();
if (redirectTo) {
const confirmRedirect = confirm(`The target page "${target}" is a redirect. Do you want to instead redirect to "${redirectTo}"?`);
if (!confirmRedirect) return;
target = redirectTo;
}
const redirectText = `#REDIRECT [[${target}]]\n{{R with history|${target}}}`;
const summary = `[[WP:BLAR]] to [[${target}]].`;
// Perform the page redirect
await api.postWithToken('csrf', {
action: 'edit',
title: pageName,
text: redirectText,
summary: summary,
minor: false,
nocreate: true
});
// Target talk page - add template before TOC
const talkText = await api.get({
action: 'parse',
page: `Talk:${target}`,
prop: 'wikitext'
}).then(res => res.parse.wikitext['*']);
const tocPos = talkText.indexOf('__TOC__');
const newTalkText = tocPos !== -1
? talkText.slice(0, tocPos) + `\n{{Blank and redirect notice|${pageName}}}\n` + talkText.slice(tocPos)
: talkText + `\n{{Blank and redirect notice|${pageName}}}\n`;
await api.postWithToken('csrf', {
action: 'edit',
title: `Talk:${target}`,
text: newTalkText,
summary: `Notifying of redirect from [[${pageName}]]`
});
// Notify page creator
let notifChoice = config.defaultNotification;
if (notifChoice === 'prompt') {
notifChoice = prompt(
'Notify page creator?\nA = Use {{Uw-blar}}\nB = Use friendly message\n(Leave blank to skip)',
'A'
);
if (notifChoice === null) return;
}
if (notifChoice.toUpperCase() === 'A' || notifChoice.toUpperCase() === 'B') {
const userTalk = `User talk:${creator}`;
let message = '';
if (notifChoice.toUpperCase() === 'A') {
message = '{{Uw-blar}}';
} else {
const defaultFriendly = `== BLAR notice ==\nHi there. While reviewing new pages, I noticed that a page you created, [[${pageName.replace(/_/g, ' ')}]], does not appear to meet Wikipedia's notability guidelines as a standalone article. As an alternative to deletion, I've redirected it to [[${target}]]. If you disagree, feel free to revert my redirect and we can proceed to a deletion discussion at [[Wikipedia:Articles for deletion]]. (If you reply to me here, please ping me as I am not watching this page.) Thanks! [[User:Dclemens1971|Dclemens1971]] ([[User talk:Dclemens1971|talk]]) 02:28, 11 April 2025 (UTC)`;
message = prompt('Edit the friendly message below before sending:', defaultFriendly);
if (message === null) return;
}
await api.postWithToken('csrf', {
action: 'edit',
title: userTalk,
appendtext: `\n${message}`,
summary: `Notifying about redirect of [[${pageName}]]`
});
}
// Log to user BLAR log page
if (config.autoLog) {
const logTitle = `User:${userName}/BLAR log`;
let existingLogText = '';
try {
const logData = await api.get({
action: 'query',
titles: logTitle,
prop: 'revisions',
rvprop: 'content',
rvslots: 'main'
});
const pages = logData.query.pages;
const page = pages[Object.keys(pages)[0]];
if (page.revisions && page.revisions.length > 0) {
existingLogText = page.revisions[0].slots.main['*'];
}
const updatedLog = `${existingLogText}\n* [[${pageName}]] → [[${target}]] — 02:28, 11 April 2025 (UTC)`;
await api.postWithToken('csrf', {
action: 'edit',
title: logTitle,
text: updatedLog,
summary: `Logged BLAR of [[${pageName}]]`,
createonly: false
});
} catch (logErr) {
console.warn('Could not update BLAR log:', logErr);
}
}
if (config.autoWatchlist) {
const expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
const expiryStr = expiry.toISOString();
await api.postWithToken('csrf', {
action: 'watch',
titles: `${pageName}|Talk:${target}`,
expiry: expiryStr
});
}
// Success popup
const popup = document.createElement('div');
popup.innerHTML = `
<div id="blar-success-popup" style="
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #f0fff4;
border: 2px solid #38a169;
box-shadow: 0 0 20px rgba(0,0,0,0.2);
padding: 2em;
border-radius: 12px;
z-index: 9999;
max-width: 400px;
text-align: center;
font-family: sans-serif;
">
<h2 style="color: #2f855a;">✅ Success!</h2>
<p><strong>${pageName}</strong> was successfully redirected to <strong>${target}</strong>.</p>
<p style="margin-top: 1em;">
<a href="/wiki/${pageName}" target="_blank">View page</a> •
<a href="/wiki/${target}" target="_blank">Target</a> •
<a href="/wiki/User:${userName}/BLAR log" target="_blank">BLAR log</a>
</p>
<button id="blar-success-dismiss" style="
margin-top: 1.5em;
padding: 6px 12px;
background: #38a169;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
">Dismiss</button>
</div>
`;
document.body.appendChild(popup);
setTimeout(() => popup.remove(), 5000);
} catch (error) {
console.error("BLAR script error:", error);
alert("An error occurred. Check the console for details.");
}
});
});
});
})();
// </nowiki>